why randi wont work properly?

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

why randi number wont work properly?
at first everything work randomly like i want, then everything not random anymore,

$player/hair.set_texture(load("res://player/female/hair/Fhair"+str(randi() % 6 + 1)+".png"))
red = $hairRGB/R/Red_scrollbar.set_value(randi() % 255 + 1)
green = $hairRGB/G/Green_scrollbar.set_value(randi() % 255 + 1)
blue = $hairRGB/B/Blue_scrollbar.set_value(randi() % 255 + 1)

now everything become fix number every time i play scene
hair become 5
red 229
green 186
blue 121

how should i fix it? i already delete other code and node just to check if i make mistake but, number still fix.

sorry for my broken eng, and thank for any help.

:bust_in_silhouette: Reply From: MysteryGM

You need to call randomize() somewhere in your game once. Only once is needed.

omg!!, thank you very much, i spent 3~5 hour try to fix it but never work. thanks once again.

so i dont even need to call it again in other scene with different node and script ?

potatobanana | 2019-01-07 17:47

:bust_in_silhouette: Reply From: p7f

Random number generation usually its implemented as what is known as pseudo-random. Its indeed a too long sequence of numbers, so long that it seems there is no sequence, but not random really (idk if its the way implemented in godot, but results apply). If you run randi every time you start again the game, it will generate the same sequence. What you need is to give a “seed”. A seed is like an index that tells in which part of the pseudo-random sequence start the sequence. As the sequence is so long, different seeds always generate what looks like random numbers.

Godot has a function randomize() that selects that seed for you… Just add randomize() at the beggining of your _ready() function and that should do the trick.

thankyou for explanation
so i dont even need to call it again in other scene with different node and script ?

potatobanana | 2019-01-07 18:00

no, as @MysteryGM answered, just need it once! randomize() selects the seed, and then randi starts following the sequence from that point. Any time you run the game, randomize will select different seed. Once the seed is selected, as the sequence it’s so long, it does not matter if you change it… it will look random.

p7f | 2019-01-07 18:04

thank you very much

potatobanana | 2019-01-07 18:10