Distribution of Lua random numbers?

Discussions on Modding of S.T.A.L.K.E.R. SoC & Clear Sky

Distribution of Lua random numbers?

Postby nandersen on 12 Oct 2008 09:56

I think that the math.random(a, b) gives me a very bad distribution.
I seem to get a or b a lot of times while values in between are not produced that often.
Is there a trick to get a better distribution when generating numbers between a and b?
User avatar
nandersen
Senior Resident
 
Posts: 212
Joined: 17 Sep 2008 17:24

Postby VINTAR on 13 Oct 2008 06:31

Saw this while reading up on LUA, dont know if it will help you :

math.randomseed(os.date("%d%h%m%s"))

The call to function os.date will return the concatenation of the current day, hour, minute, and second, therefore you will repeat your game scenario only if you start to play at the same second of the same day at least a month later.
Core2Quad Q8300 2.5 @ 3.6ghz
Cooler Master Hyper TX 2 CPU cooler
Gigabyte EP45 UD3R mobo
Nvidia GTS450
2 GIGS Transcend DDR2 800 pc6400 @ 960
Thermaltake Tough Power 650w PSU
Samsung Syncmaster P2350
-----------------------------------
Apartheid veteran
User avatar
VINTAR
Senior Resident
 
Posts: 441
Joined: 18 Sep 2008 15:07
Location: Durban, South Africa

Postby nandersen on 13 Oct 2008 09:20

Thanks VINTAR...
Now, I start to get a bit worried... how exactly is the random generator seeded? Is the seed saved by the game and used for synchronization between server and client? If so, it could be dangerous to mess around with explicitly seeding the generator outside the "correct" initialization code?
User avatar
nandersen
Senior Resident
 
Posts: 212
Joined: 17 Sep 2008 17:24

Postby VINTAR on 13 Oct 2008 11:59

Usually when a prgram starts, it initialises the generator with a fixed seed, so when you start the program it will generate the same set of random numbers. I read all this in this online book :

http://books.google.co.za/books?id=ZV5hXZ8QPKIC&pg=PA3&dq=programming+in+lua&source=gbs_toc_r&cad=0_0&sig=ACfU3U1dTzuvDoziASmnJ4Cbe_rfnHB9Fg#PPA155,M1

This was their solution for really randomizing numbers, so I doubt whether it is risky, but I know nothing about it so dont take my word for it, have a read but it doesnt say much more onthe subject.
Core2Quad Q8300 2.5 @ 3.6ghz
Cooler Master Hyper TX 2 CPU cooler
Gigabyte EP45 UD3R mobo
Nvidia GTS450
2 GIGS Transcend DDR2 800 pc6400 @ 960
Thermaltake Tough Power 650w PSU
Samsung Syncmaster P2350
-----------------------------------
Apartheid veteran
User avatar
VINTAR
Senior Resident
 
Posts: 441
Joined: 18 Sep 2008 15:07
Location: Durban, South Africa

Postby nandersen on 13 Oct 2008 14:42

Thanks again, VINTAR.

The reason why I'm cautious is because I remember from my Half-Life modding days that there were two parts of the game: server part and client part. In HL the game used a shared pseudorandom seed that was used to ensure that the code on client got the same result as the code on server.

Now, I have no idea if there is something similar in STALKER, maybe it's safe enough to do it the way you describe :)
User avatar
nandersen
Senior Resident
 
Posts: 212
Joined: 17 Sep 2008 17:24


Return to Modding Techniques

Who is online

Users browsing this forum: No registered users and 30 guests