## RandomLua Library

TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

### Re: RandomLua Library

Using this library and a common seed will produce the same results on all systems right?

linux-man
Citizen
Posts: 67
Joined: Thu Apr 28, 2011 4:51 pm

### Re: RandomLua Library

I hope so...
Updated again to correct a bug in mwc:randomseed. I was not reseting a variable parameter.

T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

### Re: RandomLua Library

What are the benefits in comparison to math.random()?

kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

### Re: RandomLua Library

If I've understood it correctly, math.random returns different values with the same seed depending on the platform (mac, at least). This might be inconvenient if you want the exact same results in all platforms; But I might be mistaken.
When I write def I mean function.

bmelts
Party member
Posts: 380
Joined: Fri Jan 30, 2009 3:16 am
Location: Wiscönsin
Contact:

### Re: RandomLua Library

Yeah, math.random's results are platform-dependent, even with identical seeds. The distribution is, too - some implementations of math.random() are better at generating a pseudorandom distribution of numbers than others.

On the other hand, it is faster than a pure Lua implementation, because the function it calls is part of the C standard library. As you can see from linux-man's benchmarks, some algorithms are slower than others, but all of them are slower than they would be in C/asm form.

slime
Solid Snayke
Posts: 2918
Joined: Mon Aug 23, 2010 6:45 am
Contact:

### Re: RandomLua Library

You could probably get a (relatively) wicked fast RNG using LuaJIT and its FFI.

problem, bartbes?

linux-man
Citizen
Posts: 67
Joined: Thu Apr 28, 2011 4:51 pm

### Re: RandomLua Library

I'm using mwc and quite happy with the results. It's fast enough for casual use. Beside, from what I saw, mwc is as good as math.random() on linux and way better than the Mac implementation (I wonder why).

genericdave
Citizen
Posts: 53
Joined: Tue Dec 15, 2009 9:08 am

### Re: RandomLua Library

It seems to be completely broken on osx. Here's my output from your example:

Code: Select all

6 3 4 10
1 5 5 1
6 7 9 1
1 9 7 1
6 9 5 4
1 3 3 1
6 1 5 10
1 3 9 10
6 9 9 1
1 3 1 3

0
38011930
2.22129e+09
0
50726607
3.00238e+09
0
4194433
3.83448e+09
0
Totally different from your results and not random in the slightest. I ran into a similar problem when attempting to make my own random number generator. The variables end up being too big at one point and lua sometimes rounds them off. Ideally, a decent generator should be part of Love itself.

linux-man
Citizen
Posts: 67
Joined: Thu Apr 28, 2011 4:51 pm

### Re: RandomLua Library

Many thanks for your report. I now had a little time for testing and found that twister code was broken. To correct it I needed to round all the calculations to 31 bits. I guess the code is now an "adapted Mersenne Twister" and maybe the randomness sucks now (but I don't really think that).
I updated the lib with a new test and added a love test file.
I don't have a Mac right now, but the results are the same on Linux 32/64 and Windows XP. Can't imagine how they can be broken on Mac. Can you please test again?

genericdave
Citizen
Posts: 53
Joined: Tue Dec 15, 2009 9:08 am

### Re: RandomLua Library

Alright, I figured out what the problem was. It works just fine in the .love file you posted. Before, however, I was using a non-love lua interpreter (specifically, AGen). I guess the way that different interpreters deal with variable sizes can cause different results. Love seems to play nicely with the lib on all systems, but certain interpreter environments totally break things.

### Who is online

Users browsing this forum: Nixola and 18 guests