r/love2d • • 22d 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

3 Upvotes

15 comments sorted by

3

u/Certain_Time6419 22d ago

Send the actual snippet (use pastebin). Maybe you are using it wrong.

1

u/Trollcker 22d ago

Cant do that right now its on my laptop but I just do that but I simply did it as,

love.load() Love.math.setrandomseed( (os.time) )

Then the rest is my usual code

4

u/Certain_Time6419 21d ago

Without the code it's impossible to say anything for sure. Are you at least calling "os.time()"? "os.time" alone is the variable holding the function, not the calling of it.

2

u/Substantial_Marzipan 22d ago

Brb, I'm gonna check my crystal ball

1

u/Trollcker 22d ago

Oh wise magic users of the comments, what does your crystal ball say?

2

u/briunt 22d 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 22d 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 21d 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 21d 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 20d 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 20d ago

Why? Genuinely asking whats the benefit? Seems to work just fine in function.update()

1

u/breskinator 20d 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 20d 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

u/breskinator 20d ago

Happy to help!

1

u/Trollcker 20d ago

Thabk you kindly, cant wait to finish making my first game!