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.
#Randomly pair numbers from 2 arrays with no repeats until all unique sets are created.
1 messages · Page 1 of 1 (latest)
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
I guess I can use, func _choose_equation(): for current_element in p_list: for current_secondary_element in s_list: print(str(current_element) + "," +str(current_secondary_element)) However making this randomized would likely require a completely different approach.
you need to do it two steps
first, save the combinations in an array
then you randomize the order of this new array
That has come to my mind, is there a way I can access each primary and secondary element seperately. (I would like to multiply each pair together and find a product for each unique set)
Could I add both values of the equation into seperate arrays and match them by index. I could then randomly choose and index from the first array and match it with the second one and erase both from their arrays?
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)
Thanks for all the help!
there
's a shuffle function btw: https://docs.godotengine.org/en/stable/classes/class_array.html#class-array-method-shuffle
A built-in data structure that holds a sequence of elements. Description: An array data structure that can contain a sequence of elements of any Variant type. Elements are accessed by a numerical i...
so no need to manually shuffle it