Get better random floats (SOLVED)

Hey there,

this is my first question, so please dont kill me if I miss something ;).

I would like to spin a wheel (some kind of “wheel of fortune”) a random times each click on a button. To archive that I need to generate a random float between 0.10000000000000 and 0.30000000000000 (used as velocity).

Right now I am trying to get these values with the following code

function randomFloat(lower, greater)
	math.randomseed(os.time())
    return lower + math.random()  * (greater - lower);
end

That works fine except of a little thing. That function is used up to 10 times a minute and with math.randomseed(os.time()) i do not really get different values.

Example with randomFloat(0.1, 0.3) :
1: 0.249560546875
2: 0.2498779296875
3: 0.25011596679687
4: 0.25037841796875

imho os.time() might be a bad choice if you need to generate multiple random floats within a short time and I would be really thankfull if somebody might help me to get better random floats.

As an idea to improve this I was thinking about tracking the mousemovement of the last x seconds as the randomseed. But that would not be very usefull because the game should be used on android/ios.

thank you in advance,

clarix

You don’t need to seed the random number generator every time - try doing it just once.

3 Likes

Exactly what @benjames171 said. This should in most cases be done only once.

2 Likes

Confirmed. That did the trick for me.

Thanks a lot!

2 Likes