Testing randomly generated numbers to prevent duplicates

Hi there,

I’m making a little project which generates random numbers in a range and pulls an entry from a string table based on the number. However I’m wanting to ensure that it never pulls a duplicate entry and am having difficulty setting that up.

I had attempted the following (generate random number, test against array, repeat function if it matches, add if it doesn’t) but I received an error for an infinite loop when attempting to use it:

There’s likely something very obvious I’m missing, but any help would be greatly appreciated.

Thanks in advance!

Dont do the add in the loop… you can use a bool var ie ‘bArrayContainsNumber’ to flag whether its in the array, then add after. Use a for each with break and break on find so you dont run the full array.

Alternatively, you probably want to use AddUnique in this case, and just use the output from it to determine whether you have the number or not… if it’s INDEX_NONE then you have it, if it returns >=0 then you didn’t have the number. Doing it this way won’t require the loop.

2 Likes

The easiest way to do this, is to start with an array that contains all the elements, and then shuffle it randomly, and then draw “the last item” from the array (removing it,) until the array is empty. Then re-fill the array and shuffle again.

3 Likes

That’s awesome, the add unique node works perfectly for this situation. Thank you so much for your help!

1 Like