1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice
  2. NOTICE: We will be applying a Xenforo update on Tuesday 25th of February. This means that from approximately 10:00 to 14:00 BST the forums will be offline (or possibly longer). Sorry for the inconvenience! Official Announcement here.

GM:S 1.4 Shuffle multiple DS Lists in the same order?

Discussion in 'Programming' started by TheBroman90, Jun 9, 2019.

  1. TheBroman90

    TheBroman90 Member

    Joined:
    Jun 20, 2016
    Posts:
    371
    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.
     
  2. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    1,112
    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.
     
    TheBroman90 and Homunculus like this.
  3. Homunculus

    Homunculus Member

    Joined:
    Jun 20, 2016
    Posts:
    856
    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 likes this.
  4. chamaeleon

    chamaeleon Member

    Joined:
    Jun 21, 2016
    Posts:
    1,112
    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
    
     
    TheouAegis likes this.
  5. TheBroman90

    TheBroman90 Member

    Joined:
    Jun 20, 2016
    Posts:
    371
    Yeah, it might be easier to use a ds_grid instead.
     

Share This Page