|
|
|
|
Attention |
Topic was automatically imported from the old Question2Answer platform. |
|
Asked By |
jarlowrey |
|
Old Version |
Published before Godot 3 was released. |
So if I understand randomize correctly, it needs to be called before every random number to ensure they’re not equal. So you’d need something like this:
randomize()
var x = rand_range(-100,100)
randomize()
var y = rand_range(-100,100)
get_node("RigidBody2D").set_linear_velocity(Vector2(x,y))
randomize()
set_rot(randf() * 2* PI)
Which is crazy. Of course you can make a function for that too. Is this right, and will this API remain similar in 3.0?
|
|
|
|
Reply From: |
volzhs |
you don’t have to call randomize()
for getting every single random number.
you just need to call it only once.
and randomize()
does just set a different random seed.
it does not ensure random number is different from previous random number.
this is very common for every program languages.
for i in range(10):
randomize()
print(randi() % 2)
do you expect 0,1,0,1,0,1,0,1,0,1 in this case?
Haha no of course not. So you’re saying randomize() only needs to be called once ever? Like on an autoloaded global node? When I was trying to use random calls without randomize I encountered a bunch of issues where numbers were always the same, and the answers I saw made it seem that randomize() needed to be called every time. Testing it now seems that once is enough, is that true?
jarlowrey | 2017-06-01 15:51
yes, it would be better to call randomize()
at starting game.
this will produce different number for each session.
volzhs | 2017-06-01 15:58
Cool. I tested only calling it once, and it seems to work fine. Thanks!
jarlowrey | 2017-06-01 16:01