• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker random numbers using ds_list_shuffle()

S

Sahibjot Cheema

Guest
I'm want to receive a value between 1 and 6 but if I've already received the value, i'll get a different value.

I want to do this by adding the numbers in a ds_list and then using ds_list_shuffle() to switch up the numbers. Then i can just use the first numbers in the ds_list which will be random.

I've tried making this expect the ds_list_shuffle won't really do anything. Anyone know of a fix to the code or a better way to do this would be greatly appreciated.
Code:
//Creates a random list
yy = 0;
randomizer = 1;
test = ds_list_create();
var ii = 1;
repeat(tiles_ui.height - 1) {
    test[# 0, ii] = ii;
    ii++;
}
ds_list_shuffle(test);

while (randomizer != 4) {
    yy = irandom_range(0, 8);
    
    if (ds_tiles[# 0, yy] = 0) {
        ds_tiles[# 0, yy] = test[# 0, randomizer];
        ds_tiles[# 1, yy] = 2;
        randomizer++;
    }
}
 
S

Sahibjot Cheema

Guest
Why are you using the grid accessor on a list?
just ignore all that stuff, it's part of the other code. I just want to know if theirs a way to choose random numbers and have them not be duplicates.
 

2Dcube

Member
Create
Code:
numbers = ds_list_create();
Some Event or Script
Code:
while(true)
{
  var num = floor(random(99999));
  if ds_list_index(numbers, num) = -1
  {
     ds_list_add(numbers, num);
     break;
  }
}
The above code checks if the number is already in the list, and keeps looping until it finds a number that isn't.
 
just ignore all that stuff, it's part of the other code. I just want to know if theirs a way to choose random numbers and have them not be duplicates.
You can't ignore that stuff, because you can't use a ds_grid accessor on a ds_list.

Its not possible to start improving your code until you fix this.

Using the grid accessor on a ds_list will erroneously cause gms to access whatever grid has the same index as that list(if one even exists). Meaning your code is not even adding numbers to your ds_list, they are being added to a grid if at all.
 
S

Sahibjot Cheema

Guest
I decided to use this:

Code:
//Creates a random list
yy = 0;
randomizer = 1;
test = ds_list_create();
var ii = 1;
repeat(tiles_ui.height - 1) {
    test[# 0, ii] = ii;
    ii++;
}

//Adds 3 UI tiles
ii = 1;
while (ii != 4) {
    yy = irandom_range(0, 8);
    
    if (ds_tiles[# 0, yy] = 0) {
        randomizer = test[# 0, irandom_range(1, tiles_ui.height-1)]
        show_message(randomizer);
        if (randomizer != 0) {
            ds_tiles[# 0, yy] = randomizer;
            test[# 0, randomizer] = 0;
            ds_tiles[# 1, yy] = 2;
            ii++;
        }
    }
}
It's not the best way to do it cause it will repeat 1 or 2 extra times to make sure it isn't a duplicate but this hopefully won't make much difference.
 

2Dcube

Member
If this isn't something that happens every step, repeating a few times shouldn't make a difference.
 

NeoShade

Member
But you've still got the same problem in that you're using a grid accessor ( test[# ...] ) for a list.
You should be using a list accessor ( test[| ...] ).


Also, if you're doing anything with random numbers, you need to make sure to randomize() somewhere in your game, otherwise you will just keep getting the same results.
 

2Dcube

Member
Is a ds_list internally actually a ds_grid with 1 row?
If that's the case I suppose you could use a grid accessor, although it seems like more work to type and makes your code less clear.
 
Is a ds_list internally actually a ds_grid with 1 row?
If that's the case I suppose you could use a grid accessor, although it seems like more work to type and makes your code less clear.
Definitely cannot use a grid accessor on a ds list. Can demonstrate this with a simple test if you want to verify.

Example : Create a brand new project, pop this code in the create event of a test object:

Code:
test = ds_list_create()
test[# 0, 0] = 66;
NOTE : This is exactly the same code (effectively) as the OP is using.

Gives this error on run:

Code:
Data structure with index does not exist.
 at gml_Object_oControl_Create_0 (line 6) - test[# 0, 0] = 66;
So the OP code will fail, unless by chance they have created a grid with the same index as the ds_list.

When you create ds_grids or ds_lists etc..., what you get back from the create function is an index ( simply an integer number ).

NOTE : There is a separate list of indexes for each type of data structure, so you have ds_lists [0..N], ds_grids[0..N] etc...so you have to be careful not to mix the datatypes as they can have the same index.

E.g.
Code:
list_A = ds_list_create() // list_A is 0
list_B = ds_list_create() // list_B is 1

ds_list_destroy(list_A) // This frees up the 0 index

list_C = ds_list_create() // list_C is 0 (reuse of 0 index)

grid_A = ds_grid_create(1,1) // grid_A is 0
grid_B = ds_grid_create(1,1) // grid_B is 1  
grid_C = ds_grid_create(1,1) // grid_C is 2

// Now for the tricky part:

list_B[# 0, 0] = 44; // NOTE : This does not cause an error!
This line:
Code:
list_B[# 0, 0] = 44; // NOTE : This does not cause an error!
...will not cause an error, perhaps leading to the assumption that you can use the grid accessor on a list.

However, what is happening is, GMS sees the grid accessor (#) and says:

"I say old chap, look here, this code wants to access a grid, OK, I will look up my list of grids and give access to the grid with the index that matches whatever number has been placed before the (#) grid accessor."

So follow closely : list_B holds a value of 1, but you use a grid accessor, so GMS tries to access grid index 1 instead. By coincidence, there is a grid with an index of 1, so GMS does not throw an error, just returns the value in the grid.

So the coder may be thinking they are accessing the ds_list because they used the variable list_B, but GMS is not behaving this way.
 

2Dcube

Member
Thanks for clearing that up.
So it is quite dangerous to use the wrong accessor, and could be disastrous when you use it on save data...
 

FrostyCat

Redemption Seeker
Let me tell a personal anecdote from a recent library visit, that explains why the grid accessor problem should not be ignored.

As is usual every month, I went to my local library in April and checked out self-improvement, programming and financial management books at the automated counter. And just when I was about to put the PhoneGap book into my bag, the screen suddenly showed that I've taken out an additional Bleach comic book. It wasn't on the pile of books I was about to take out, it wasn't even in the branch I was visiting. So I called in staff to take it off my record, so that I won't get a due notice for it later.

It turned out that in my local library system, the barcodes on books and on patron cards are the same format. The barcode on my card is the same as the barcode on the Bleach comic book. So when I nudged my card while putting away the PhoneGap book, it picked up my card as though it was a book.

I guess if original poster was in the same situation I was, he'd be hasty enough to let that Bleach comic book slip, and "ignore all that stuff" until the late fines pile up later.
 
S

Sahibjot Cheema

Guest
Whoa so many comments about that #, ya it was a mistake and i removed it. But it wasn't really effecting anything and had completely nothing to do with the question i had. But I did notice and removed it long ago.
 

NeoShade

Member
But it wasn't really effecting anything and had completely nothing to do with the question i had.
Except that it had everything to do with your question. You were asking about shuffling a list, but then using the wrong code to try to access that list.
Don't get the wrong idea and think we're attacking you because you made a mistake. We're just trying to help solve the problem.

Think of it like this: If you were trying to draw an accurate picture of a dog and asked a group of people "I'm having trouble with this drawing of a dog... what's wrong with its ears?", you shouldn't be surprised when people say "It's ears are too long, but also, why does it have 9 legs?". In order to help you become a better game developer, people won't solely address the issue that you've asked about, but they'll also point out other things they see which might be causing issues either now or somewhere down the track.
 
Also, if future readers happen to stumble upon this thread, they will hopefully get the correct information out of it.

So it is quite dangerous to use the wrong accessor, and could be disastrous when you use it on save data...
It could cause some hard to find bugs, that's for sure.
 
Top