Random numbers not so random?

Discuss about anything related to the Irrlicht Engine, or read announcements about any significant features or usage changes.
Post Reply
Tyn
Posts: 932
Joined: Thu Nov 20, 2003 7:53 pm
Location: England
Contact:

Random numbers not so random?

Post by Tyn »

Hey all

I'm trying to do some AI. Basically I need some random numbers to generate the positions they move to ( it's pretty basic ATM ) but the random numbers aren't random at all. The class generated the same positions every time which was a bit strange. I added a random call to the drawing loop and printed it to the console just to see it changing. The numbers now change and the positions are always different. What's going on there then? Doesn't seem to random to me. I need a number between 0 and 39, so I am using:

rand()%39

Anyone else every heard of this?
uzik
Posts: 24
Joined: Wed Sep 22, 2004 4:56 pm

plant the seeds baby

Post by uzik »

rand() doesn't generate random numbers. No computer code really
does, except for some that sample the audio noise from your sound
card for it. They're all 'psuedo random numbers' which look sort of
random but aren't completely. They will return the same series
of numbers when they use the same seed. If you don't change the
seed value the numbers will be the same each time you run.
The best way to get a new seed it to use the number of milliseconds
since your computer was turned on as the seed. That's easy to
get from the irrlicht timer.

// get the timer object pointer
pTimer = device->getTimer();

// seed the random number generator from the clock
srand( (unsigned int) pTimer->getTime() );
Tyn
Posts: 932
Joined: Thu Nov 20, 2003 7:53 pm
Location: England
Contact:

Post by Tyn »

You mean that the random number generator doesn't even bother to manipulate the most basic thing on a microprocessor, the clock circuit? It is the only unexpected variable that isn't influenced by the user and so I don't understand why it wouldn't make use of it. I'd bet it wouldn't even take me long to find out how to count the pulses sent out by it.

Also, would you expect the same random number each time you call the function inside a self contained class?
uzik
Posts: 24
Joined: Wed Sep 22, 2004 4:56 pm

Post by uzik »

The random number isn't tied to the clock for a good reason. A lot
of people need random numbers that repeat. It makes it much easier
to debug your program if you can repeat the bug. If you got new
random numbers each time you run then you couldn't repeat the
conditions that caused the bug.

Some microprocessors have a clock register built in to count time, but
most don't. The clock on PC clones is done in hardware on the motherboard.

If the seed isn't changed, then you should expect to get the same
sequence of random numbers each time you run the program. Doesn't
make any difference about a class, since it still calls the same code
underneath.

While testing leave the seed the same, when you're pretty sure your
code is working then set the seed from the clock. It takes one extra
line of code to use the timer to set the seed.

Oh, and if you want a number from 0-39, you need rand()%40.
Modulus always returns a number between 0 and (n-1).
Tyn
Posts: 932
Joined: Thu Nov 20, 2003 7:53 pm
Location: England
Contact:

Post by Tyn »

Some microprocessors have a clock register built in to count time, but most don't. The clock on PC clones is done in hardware on the motherboard.
The hardware clock is what I was referring to. Count the pulses from any set time in the program and you can generate a random number from that. The data is going to be as random as you could get I recon if set from a set time on the computer.

I thought time on computers used this constant pulse signal as a source of time, IIRC it uses the same crystal that watches use to keep time. I could be wrong, I only know bits about it really so I could well be wrong about it.

Didn't realise that I could use rand()40% and still get between 0 and 39, cool, Cheers.
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

look here for some info about random numbers:
http://www.cprogramming.com/tutorial/random.html
Post Reply