godot: randi() returns 0 for FIRST number after seed()
CODE:
seed(0)
print(randi())
print(randi())
seed(1)
print(randi())
print(randi())
OUTPUT (Godot 2.1.4):
719435623
1221583151
16807
282475249
OUTPUT (Godot 3.0.2):
Godot 3.0.2
0
1613493245
0
3114030964
If randomize() is used, the same issue doesn’t seem to occur.
Is this expected behaviour?
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 5
- Comments: 19 (11 by maintainers)
I’ve been looking into this in more detail and it seems that the first value returned after a call to seed is almost always garbage essentially. It is consistently lower than 10 for seeds less than 2^30. And above that it slowly increases based on the number passed into seed. Although this may be expected behavior to the people who implemented the RNG system it will come as a surprise to any user that the size of the first returned value is heavily based on the size of the seed.
Documenting this behavior is a good first step, but realistically most users don’t care about the implementation, they just want good values returned every time, currently you have to call
randi()once after your call to seed so you can get acceptable values. What we should look into long term is either calling it once internally for the user or coming up with a better RNG.What makes this even worse is that
randf()callsrand()internally, so for most seeds you are getting a value that is essentially zero.Thanks for the explanation, this just needs a mention in the documentation of
seed()for users which might be confused by this implementation detail then.Oh geez, that’s pretty terrible. XD We’ll have to fix that…