• 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!

Legacy GM Shuffle multiple DS Lists in the same order?

TheBroman90

Member
Is it possible to shuffle multiple ds_lists in the same order?
I need a random order, but the lists need their values to be synced with each other.
 

chamaeleon

Member
Is it possible to shuffle multiple ds_lists in the same order?
I need a random order, but the lists need their values to be synced with each other.
Have an order list that you shuffle that contains the indices for your two lists and shuffle it, and use the shuffled indices to rearrange the content into two temporary lists (too lazy to figure out a smart way to avoid the temporary duplication)
Code:
var l1 = ds_list_create();
var l2 = ds_list_create();
var order = ds_list_create();

ds_list_add(l1, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100);
ds_list_add(l2, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101);

for (var i = 0; i < ds_list_size(l1); i++) {
    ds_list_add(order, i);
}

ds_list_shuffle(order);

var res1 = ds_list_create();
var res2 = ds_list_create();

for (var i = 0; i < ds_list_size(order); i++) {
    res1[| i] = l1[| order[| i]];
    res2[| i] = l2[| order[| i]];
}

ds_list_copy(l1, res1);
ds_list_copy(l2, res2);

ds_list_destroy(res1);
ds_list_destroy(res2);

for (var i = 0; i < ds_list_size(l1); i++) {
    show_debug_message(string(l1[| i]) + ", " + string(l2[| i]));
}
Code:
40, 41
80, 81
90, 91
20, 21
10, 11
60, 61
70, 71
50, 51
100, 101
30, 31
Or don't bother with the changing of the original lists, if you only need the rearranged order temporarily, and just use the order list to transform some given index into the two lists into its "random" order position like I did with the initialization of res1 and res2.
 
H

Homunculus

Guest
An alternative would be to use a ds_grid instead, with every column holding the values of a list. You can then shuffle the grid. Since this operation shuffles only the rows, you are essentially applying the same shuffle order to every list
 

chamaeleon

Member
Although I don't like the idea of calling randomize() more than once in a program (because I'm uncertain if it might skew the randomness of generated numbers in any way to do so if it happens a fair number of times) another way of doing it is to capture the seed and reseed the random number generator between shuffling the lists.
Code:
var l1 = ds_list_create();
var l2 = ds_list_create();

ds_list_add(l1, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100);
ds_list_add(l2, 11, 21, 31, 41, 51, 61, 71, 81, 91, 101);

var seed = randomize();
ds_list_shuffle(l1);
random_set_seed(seed);
ds_list_shuffle(l2);

for (var i = 0; i < ds_list_size(l1); i++) {
    show_debug_message(string(l1[| i]) + ", " + string(l2[| i]));
}
Code:
90, 91
30, 31
10, 11
80, 81
40, 41
20, 21
50, 51
60, 61
100, 101
70, 71
 

TheBroman90

Member
An alternative would be to use a ds_grid instead, with every column holding the values of a list. You can then shuffle the grid. Since this operation shuffles only the rows, you are essentially applying the same shuffle order to every list
Yeah, it might be easier to use a ds_grid instead.
 
Top