0 votes

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 by (170 points)

1 Answer

0 votes
Best answer

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()

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 by



Try this:

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

Lol... sometimes it's simple.

Will that help?

  • Mike

you can even do it much simpler.

var MyArray = range(1,16)

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!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.