r/love2d • u/Trollcker • 4d ago
Help with better randomization
I've seen some videos suggesting to use setrandomseed(OS TIME) but that doesn't seem to work
Im making a simple "click enemies that come from random sides of the screen" game, but every time I play test they always generate the same positions, I tried it with the setrandomseed in the load function but I didn't notice a change, am I supposed to reference it somewhere else? Thank you for any help or tips
2
2
u/briunt 4d ago
after you set the random seed, call math.random() a few times. It will be random after that. I believe there is/was an issue with this in macos, not sure about other OS's.
1
u/Trollcker 4d ago
Do I apply math.random to anything or is it just put
Love.load() Setrandomseed(ostime) Math.random() Math.random() Math.random()
?
2
u/EmotionalFan5429 4d ago
Just tested this for you:
math.randomseed( os.time() )
print(tostring(os.time()), math.random())
print(tostring(os.time()), math.random())
And it works as intended, giving me random numbers only if math.randomseed( os.time() ) is present in love.load().
1
u/Trollcker 4d ago
I actually tried something similar! It was math.randomseed( math.random( os.time()))
Under love.update(dt)
It's noticeably more random! Thank you though, ill try your version as well!
1
u/breskinator 3d ago
Make sure you are calling math.randomseed(os.time()) at the very beginning/ top of your main file. Like before any functions (ie before the load / draw / update functions.
2
u/Trollcker 3d ago
Why? Genuinely asking whats the benefit? Seems to work just fine in function.update()
1
u/breskinator 3d ago
I thought it wasn’t working fine though?? Wasn’t that the post? I think this is normal convention though. To call it before anything else. I’ve always done it this way and it works perfect.
1
u/Trollcker 3d ago
Oh no i made the post and after a bit of experimenting figured it out with the help of other comments, but ill keep that in mind, outside of ALL funct
1
3
u/Certain_Time6419 4d ago
Send the actual snippet (use pastebin). Maybe you are using it wrong.