| You are here: | About>Computing & Technology>C / C++ / C#> Getting Started> What is a Random Number? |
![]() | C / C++ / C# |
What is a Random Number?What is a Random Number?: Just a number generated randomly, like throwing a dice. Computers are not very good at generating truly random numbers. Unless there is some sort of random process- such as atoms decaying in radioactive isotopes that can be measured, the random number generators in languages like C, C++ and C# are psuedo random. It's quite common to use a linear congruential generator algorithm. This generates a sequence of numbers within a spcified range. Because of the modulus artithmetic involved this will generate every number in the range once before repeating, What is Modulus Arithmetic: Modulus arithmetic is sometimes called clock arithmetic. Think of a 12 hour clock at 1.00 O'Clock. (AM/PM doesn't matter). Now add 7. You get 8 O'Clock. Now add 7 hours again and you get 3 O'Clock then 10,5,12,7,2,9,4,11,6 and eventually back to 7. We've "generated" every integer in the range 1-12. With linear congruential generators the range of numbers is much larger but they work on a similar principle. What is a Linear Congruential Generator?: Despite the intimidating name, its just a simple formula. NewValue = (A x OldValue + B) % MWhere A, B and M are generator-specific integer constants. M is a very large number and this is the cycle of the generator. There are certain other mathematical conditions that must be true for this to work- a search on the web or wikipedia will give more detail. Of course the first value oldValue is important and this is seeded from a time based calculation. If It Starts at the Same Point...?: You will get the same sequence of numbers. This is handy with an infrequently occurring bug in your program, you can rerun it with the same sequence of numbers. Of course you want it to start at a random position in your application, so some way of "seeding" your random number generator is needed. Using a system timer for this is acceptable as you don't know when the application is going to run. Seeding the generator: Modern CPUs have counters that can count from 0 to billions in a second, so using one of those as a seed is reasonable. It's tempting to think you could use that same counter as the main generator but not a good idea- your code takes a certain length of time to execute and you get "coincidences" in the numbers picked. Enough to disqualify it as a totally random number. Then there is the problem of the distribution... What is the problem with the Distribution?: Roll a single dice, the chance of getting each face is 1 in 6. If you roll three dice and add them, you get values from 3 to 18. But the chances of getting a total of 3 are not the same as getting 6. There is only 1 chance in 216 of rolling three 1s. But there are a number of ways of rolling a total of 6. You can roll 114, 141, 411,123,231,213,312,321. In a generator we normally want every possibility to have the same chance. Here is some code to roll 10 million sets of dice.
What are Random Number Generators used for?: Computers generate random numbers for gambling, encryption and even measuring irregular areas (Its called Monte Carlo- the mathematical equivalent of pinning an odd shape to a dart board and throwing millions of darts at the dartboard. Then you just count how many hit the target, and how many missed and you've got your area. But are the Random Numbers definitely Random?: Ensuring that the generators do generate truly random numbers is very important. There are statistical methods such as chi-square that can be used to determine if the series is showing any bias or uneven distribution. If you have a computer that generates Lotto numbers and someone works out how the sequence is generated, it could be very expensive. A better algorithm than a Linear congruential Generator is a Mersenne Twister. This uses certain prime numbers called Mersenne Primes where the value of the prime Mn is defined like this. Mn = 2n-1 When Random Numbers aren't random...:
I was told that a Keno machine started generating repeated sequences in Canada and someone spotted it and guessed the winning numbers. When the machine was investigated it was discovered that the chip which generated the initial seed was absent. So every time it was switched on, it started from the same value! If you want to test a random number generator, there is now C Source code to measure randomness.
|
Las Vegas on a BudgetFind a BargainHotel DealsCheap EatsFree AttractionsEntertainment for Less |
All Topics | Email Article | Print this Page | | ![]() |
| Advertising Info | News & Events | Work at About | SiteMap | Reprints | Help | Our Story | Be a Guide |
| User Agreement | Ethics Policy | Patent Info. | Privacy Policy | ©2008 About, Inc., A part of The New York Times Company. All rights reserved. |


