+1 vote

So im trying to make my variable a random number and ive used

func _randomise_my_variable(): a = randi()%10 +1

but this puts out numbers in exactly the same order every time

Is this just me or is something wrong

in Engine by (28 points)

2 Answers

0 votes

In the _ready() func put randomize() to randomize the seed, so that it generates a different value every time. Hope this helps!

by (64 points)
0 votes

Randi
For random number creation, you need two commands randi and randomize.
In the example, you will have code make a random number from 1 to 12.

func _ready():
       randomize()
       print( randi() % 12 + 1 )

Randi will always generate the same number, but with "randomize" generate become random.

randi()       # returns random integer between 0 and 2^32 – 1
randi() % 20  # returns random integer between 0 and 19

Enjoy in randomizing!

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