#Randomly pair numbers from 2 arrays with no repeats until all unique sets are created.

1 messages · Page 1 of 1 (latest)

sterile sluice
#

Hello, I currently have a system that chooses a random range of numbers (for simplicity let's say [1,2,3]) and adds them into the "Primary array". I would like to use a preset secondary array consisting of "secondary numbers" [4,5,6,7,8] and create a unique combination for each chosen primary element. I can use pick_random(primary_array) to get my "current_primary_number" (let's say it chose 2). I would like to use pick_random(secondary_array) (let's say it chose 5) and match the random value with my current_primary_number and print ("2,5"). It would then go on to choose another random primary number and do the same with it (let's say it got ("1,3")). However I would not like any unique combination to repeat (for instance if I got ("2,5") again) and stop when each primary element has paried with each secondary element. (1,4)(1,5)(1,6)(1,7)(1,8), (2,4)(2,5)(2,6)(2,7)(2,8), (3,4)(3,5)(3,6)(3,7)(3,8). Of course the outputs would be completely random order since the numbers are mixed up each time. But still only consist of the 15 unique combinations possible. I don't believe I can create arrays as the game is running and I don't know how I would avoid repeating combinations if I used duplicated arrays. I doubt arrays are even the right way to go about this, I would love to look into an alternative feature if that would be easier. If you need more information please ask.

wild mirage
#

Let's ignore the random for a bit
Can you make that 15 combinations by order ?

#

If yes, then make that
Then you'll only need to randomize the order

sterile sluice
wild mirage
#

you need to do it two steps

#

first, save the combinations in an array

#

then you randomize the order of this new array

sterile sluice
sterile sluice
wild mirage
#

maybe something like this

    #FIRST WE MAKE THE ARRAY THAT CONTAINS ALL COMBINATIONS
    var res = []
    for a in array1:
        var str_a = str(a)
        for b in array2:
            var str_res = str_a + "," + b
            res.append(str_res)
            
    print(res)
            
    #NOW WE RANDOMIZE THE RESULT
    for i in res.size():
        var switch_with = randi_range(0, res.size()-1)
        var tmp = res[i]
        res[i] = res[switch_with]
        res[switch_with] = tmp
        
    print(res)
        
velvet pike
#

there

#

so no need to manually shuffle it