How do I make a random number?

What would be a formula I could use to make a psuedo-random number, using basic math, and a running clock timer.

I can use the operations: +, -, /, *, ^, mod, abs, cos, exp, log, log10, pi, sin, sqr, and tan.

I have a running clock timer, between 1 and 1000, and I need to create a random number between 1 and 16, using only these operators (if it’s possible).

I recall something about using some decimal digits of pi for p-r numbers

Did you try the number “3”?

Psuedorandom number generators are not trivial. Knuth has an excellent discussion of them in one of his Art of Computer Programming books, but it gets somewhat technical. I would reccomend doing a google search on simple pseudorandom number algorithms.

Google search on simple pseudorandom number algorithms.

Can you store numbers (in a register or some kind of memory)? Most pseudorandom number generation algorithms you’ll find keep some kind of state between iterations. It might be difficult to generate a decent sequence without any local state.

freido is correct.
Properly generating pseudo-random numbers can be difficult.

One I have used in the past with good results is from the book,
“Simulation Methodology for Statisticians, Operatons Analysts, and Engineers” by Lewis and Orav.

The basic formula is…

U[sub]i+1[/sub]=(a*U[sub]i[/sub])mod(2[sup]31[/sup]-1)

Thre are several good choices for a:
950,706,376 or
742,938,285 or
1,226,874,159 (there are others but one of these should work fine for you)

Note: This formula generates pseudo-random variates from 0-1. You will have to convert the initial result to your required range but I’m sure you’ll be able to handle it.

How “random” does the number need to be? If this is for a game, something like 1 + mod(timer,16) might even be good enough. Also, are you sure there isn’t a built in rand() function already?

Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin. J. Von Neumann, 1951.

  1. Do not come up with your own algorithm. You will botch it badly. I mean, really, really, badly.

  2. There are a ton of psuedo-random number generators out there. Use one of them. Knuth is an excellent place. You don’t have to understand too much of the text if you just want to use some code. Look for “64-bit” (or higher) systems. Do not use 16-bit systems. I don’t care if you only want to generate numbers from 1 to 10, bigger is better.

  3. What ZenBeam said. A good rand function is so useful it’s built into nearly everything. It’s as important as sin and cos. If a Unix system, there will be several choices. Again, look for the newest, largest bit version.

Re: the Von Newmann quote: I wonder what the penance a Priest doles out for admitting this in confession?

I recommend dice.

If you have to generate a number by hand, the fastest way to do it would be to get a 10-sided die, and roll it for however many digits you need.

Computationally coming up with something is, as the others have said, very difficult. Even for a computer. There are plenty of bad random number generators out there, and the really serious guys go to lengths like reading voltage fluctuations on an AC line to ‘seed’ the RNG.