Good afternoon people;

I have a conceptual question that I cant solve; I have 15 Vars and each one generates a random number of 1 to 15 as well;
something like:

``````Var 1 = randi ()% 15 + 1
Var 2 = randi ()% 15 + 1
Var 3 = randi ()% 15 + 1
``````

...
And so succecutively.

However, I would like Var 1 generate a number between 1 and 15 and reserve this number so that Var 2 could generate numbers from 1 to 15 minus the number reserved to Var 1, and Var 3 would generate numbers from 1 to 15 minus the numbers generated for Var 1 and Var 2 and so on, until Var 15 can generate numbers from 1 to 15 minus the 14 previously generated numbers to other Vars.

I thought I would do this with a bunch of IF statements, comparing
```if Var 2 == Var 1, generate again, if Var 3 == Var 2 and Var 3 == 1, generate again```
and so on.

Is there a better way or is this IFs the best way?

Thank you guys

in Engine

Hi Lucas,

Would you be willing to use an array? Try this:

``````var MySetOfNumbers = []
for aNum in range(15): MySetOfNumbers.append(aNum+1)
var MyRandomUniqueNumbers = []
var anIndex = 0
while (MySetOfNumbers.size() > 0)
anIndex = randi()%MySetOfNumbers.size()
MyRandomUniqueNumbers.append(MySetOfNumbers[anIndex])
MySetOfNumbers.remove(anIndex)
print(MyRandomUniqueNumbers)
``````

You can use the array'ed numbers like this:

``````var Var1 = MyRandomUniqueNumbers[0]
var Var2 = MyRandomUniqueNumbers[1]
... etc...
``````

Hope that helps -

• Mike
by (85 points)
selected

WHOA!!!!!

Revision:

Try this:

var MyArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
MyArray.shuffle()

Lol... sometimes it's simple.

Will that help?

• Mike

you can even do it much simpler.

``````var MyArray = range(1,16)
MyArray.shuffle()
``````

That's why I love Godot and this forum; I started with a huge logic problem that I could not solve and I finished with a tiny code and discovered this brilliant `shuffle ()` command that I had never looked at in the documentation.
Many thanks, Mike!