randi and rand_range do not return random number

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By maer

Hello,

I am trying to generate random number but the following code returns always the same numbers after every restart:

for i in range(3):
    print( randi()%8 )
#1
#1
#4

or

for i in range(3):
     print(rand_range(0, 8))
#5.668026
#7.775277
#2.76658

I tried to restart application 10 times with the same result.

Is it possible to make totally random numbers in some range?

Thank you,

:bust_in_silhouette: Reply From: RenenerG

Hello maer,

sounds like that you could try to call randomize() in the _ready() function, in the script, where you try to generate random numbers.

func _ready( ):
    randomize()

This should generate a new seed for the random number generator. Therefore every new run should have other numbers.

see randomize for more information

Thanks, work like a charm.

maer | 2019-02-20 20:24

Thanks, I’ve got the same problem and now my game will change every match. I was thinking “Why the bubble always Spam in the same place??” and now I understand all. Thanks, thanks, thanks!

Dani Ximple Apps | 2020-12-19 18:42