News : The level of daily SPAM has reached insane proportions, all registrations are now manual. I ask you to send me an e-mail (john (at) murga (dot) org), to confirm that you want me to create an account for you.


Post Reply  Post Thread 
math.random(what?)
Author Message
mikshaw
Senior Member
****


Posts: 522
Group: Registered
Joined: Apr 2007
Status: Offline
Reputation: 5
Post: #1
Question  math.random(what?)

I was debating whether to start a general "please help mik with everything he doesn't understand" thread, since I think there will be a lot in the coming months, but I guess while the forums are still relatively slow I'll stick with one thread per subject.

The current thing I can't figure out is the math.random function, which I assume is supposed to return a pseudo-random number. However, it seems to return the same number every time.
If I use no argument the result is always 0.84018771715471
math.random(6) or math.random(1,6) always returns 6
math.random(1,12) always returns 11
...and on and on with these seemingy illogical results

Does this function not work the way it should, or am I just not understanding the Lua documentation?

07-27-2007 09:08 AM
Find all posts by this user Quote this message in a reply
iGame3D
Moderator
***


Posts: 231
Group: Moderators
Joined: Apr 2007
Status: Offline
Reputation: 0
Post: #2
RE: math.random(what?)

ok first you want to set the random seed so that the machine actually initializes a sorta psuedo randomness.

Code:
math.randomseed(os.time())

Now you should get a decently random number, especially when its called more than once since the seed will change, once is usually enough.

Random numbers were the very first thing I tried in Lua since I
used tons and tons of random-icity back 1983 with BASIC
and then later with my very many strange iGame3D experiments.

This post was last modified: 07-27-2007 10:22 AM by iGame3D.

07-27-2007 10:06 AM
Visit this user's website Find all posts by this user Quote this message in a reply
mikshaw
Senior Member
****


Posts: 522
Group: Registered
Joined: Apr 2007
Status: Offline
Reputation: 5
Post: #3
RE: math.random(what?)

woot!

Thanks very much.
The problem now is that I have no idea why that works. I've seen the term "random seed" used in many other places, but never understood what it means. And why does the random seed apparently need to be different each time?

07-27-2007 01:37 PM
Find all posts by this user Quote this message in a reply
iGame3D
Moderator
***


Posts: 231
Group: Moderators
Joined: Apr 2007
Status: Offline
Reputation: 0
Post: #4
RE: math.random(what?)

Not sure I can explain it.

Basically computers can not create true randomness since everything they do is based on a calculation which can only done with given parameters.

By seeding it with a value that is not its default it has a new parameter to play with.

The random seed does not need to constantly change, it only needs to be called once, but by updating the seed its more likely to give you the appearance of greater randomness.

Although other programmers argued against my technique,
I just feel its more random if I use two random numbers:

If random number A is greater than some value, then roll random number B and use that, else just use random number A.

I guess they had a point that I was using an extra cycle or something.
But I doubt my gigaflops calculating computer is going to care.

07-27-2007 03:16 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites

Forum Jump: