This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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.

in Engine by (429 points)

2 Answers

+1 vote
Best answer

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.

by (3,505 points)
selected by

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

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.

thank you very much

+2 votes

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

by (1,492 points)

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 ?

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 Frequently asked questions and 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 [email protected] with your username.