random question for you programmers =p

radiocore

Golden Member
Aug 25, 2000
1,011
1
0
so...um....when you're programming and choose a random number, how does the computer come up with that number?
 

GL

Diamond Member
Oct 9, 1999
4,547
0
0
Using a pseudo-random number generating algorithm. Usually, a seemingly variable parameter is used as a "seed" to generate the pseudo-random number (i.e. the current time).

Edit: BTW, if you can program then experiment with this. You'll soon realize that things aren't as random as they might seem at first.
 

hydrobum

Senior member
Dec 1, 2000
302
0
0
...that's y it's called random :p

but usually, we take a random number calculated from the time.
 

radiocore

Golden Member
Aug 25, 2000
1,011
1
0
ah, not so random after all...from time huh?...that's interesting though....thanks for the insight. :)
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,591
5
0
When I need to seed a random number, I will take the current time and also a delta time between two events, one is usually a user input response. This helps to negate a preset time setup.
 

Gs400TRd

Member
Apr 18, 2001
142
0
0
this on win sys :
time in ms from 1970.
Added with the number of handles currently running on the PC.
Then divided by the proper number to make it fit the range.
works
 

Nevo

Banned
May 28, 2001
696
0
0
Programmers usually use the random number generator provided by the OS, so it's usually OS dependent.

In Windows, you provide a "seed" value, and the generator uses that as a basis for generating "pseudorandom" numbers. I was unable to find any details on the algorithm Windows uses.

If you use the same seed, you'll get the same sequence of numbers.

So the key is to use a "random" seed value. The system clock works nicely for this, as the combination of year/month/day/hour/second/millisecond provides what is essentially a "random" seed. Other methods for choosing a seed exist.

Other methods for generating random numbers would be a white noise generator, or an amlifier with enourmous gain that essentially amplifies noise on the inputs, or taking data from a radiotelescope tuned to an arbitrary frequency...

 

Haircut

Platinum Member
Apr 23, 2000
2,248
0
0
A well known way to produce a pseduo-random number is to use the following algorithm:
x(n+1)=(a*x(n)+b) mod m

For those who don't know mod is basically the remainder, so 19 mod 4 would be 3.

This is then repeated a number of times and a number produced at the end.
Obviously this number is dependent on the starting numbers a,b and m and also on the number of iterations.
The point is that there is no simple equation that will give you the end number from the starting conditions, if we change one of the numbers slightly the end value could be wildly different.
 

oLLie

Diamond Member
Jan 15, 2001
5,203
1
0
549


That is the random number I have supplied you with. Now go buy those lottery tickets for the 3 number things but please share with me when you win kthx

Ollie