Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Algorithm for random function

Status
Not open for further replies.

eie9912

Programmer
Apr 16, 2003
1
IN
Hi,
Can any one tell me that how a logical language like C generates random numbers?What is the algorithm of this random function?
 
I can give you the very very simplified answer. Maybe someone else will give the mathematically correct answer. You could also look in numerous books, not necessarily just the deep cryptological books. Knuth for instance has a section on it.

Simply, a random number function is a function that changes a number into another number, cycling round a whole set of numbers. For instance
a = (a+1) mod something
is a rule that cycles through a whole set of number 0, 1, 2, 3, ... (something-1), 0, 1, 2....
The only problem with this rule is that its output doesn't look very random. So a good random number generator produces numbers which are very hard to guess from their predecessor, but cover all possible numbers 0-n with no gaps (gaps are inefficiency).

You'll notice that there is nothing random about this at all, and a super intelligent martian wouldn't even see the numbers as random; s/he would see the sequence in the same way as we see the sequence 1..2..3..4 etc. Hence these should be called pseudorandom numbers.

When you set the random number seed, you set the number that is cycling. It cycles once every time you then read a random number. The number you read is derived from the seed number in some (often simple) way.

It is possible to get true random numbers by hardware (electronic white noise generator etc), but this is not an easy task! It is hard to get a random signal entirely free of systematic noise of some less-than-random form.

To avoid getting the same random sequence every time you start a program it is normal to set the seed, once at the start of the program, according to something that is not the same every time the program is started (e.g. system clock). But you can also set it to something derived from a password (for instance) if you are using it to encode a text (for example).

Hope that helps!

 
Google

There are many pseudo-random number algorithms. A lot of the simpler ones are on the whole rather useless for proper statistical or cryptographic use.

Generally, you need to read the background on each algorithm and its intended uses, and use it in that manner.
 
... but beware of over-perfectionism. There are plenty of professionals, especially in cryptographic stuff, who make a living by working on a completely different planet to most of us. Even very simple algorithms are probably going to deter people from reading my e-mails. And sometimes speed is more important than security.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top