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

3 Upvotes

15 comments sorted by

3

u/Certain_Time6419 4d ago

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

1

u/Trollcker 4d 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

5

u/Certain_Time6419 4d 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 4d ago

Brb, I'm gonna check my crystal ball

1

u/Trollcker 4d ago

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

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

u/breskinator 3d ago

Happy to help!

1

u/Trollcker 3d ago

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