How to Generate N Different Random Numbers

By eHow Education Editor

Rate: (0 Ratings)

Random numbers have a range of applications in computer programs. Technically, computers can't generate a truly random number since they are deterministic machines. Computers can, however, generate pseudo-random numbers that are random by many statistical measures. The best way to generate a set of N random numbers is by using a built-in (or add-on) function in your programming language.

Instructions

Difficulty: Moderately Easy

Step1
Find the random number generator function for your programming language. This function may or may not require a "seed" number to start the random number generation process. You may have to add code for a random number generator function.
Step2
Choose a seed value, if required, and enter it in the appropriate argument location for the function call. You may have to specify the seed number in a separate function. For some random number functions, there is a default seed number or a default method for generating a seed number.
Step3
Call the random number generator function N times using a loop if N is large. If you chose a set seed number (like five or six) in Step 2, then you may get the same sequence of N numbers each time you run the program (this depends on the characteristics of the generator and any other parameters that may be involved). If the seed number is distinct each time the program runs, you will get a distinct sequence.
Step4
Check at least a portion of the sequence to be sure you chose a good seed number. If you know how the random number generator works and are sure of a good seed number choice, you may not need to do this step.

Tips & Warnings

  • In C++, for example, there are two standard functions: "rand()" and "srand()." The "srand()" function specifies the seed value according to the argument, which is an unsigned integer. The "rand()" function produces a random number between zero and one using the seed value specified with the "srand()" function.
  • The more distinctive the seed number, the more apparent randomness the generated sequence of numbers will have. One convenient way to choose a distinct seed number is to use the current time, which is different each time the program runs.
  • A poor choice of a seed number (depending on the algorithm used in the random number generator) can lead to a repetitive (and, as a result, non-random) sequence. If you want to be sure exactly how the random number generator works, check the documentation for your programming language.

Post a Comment

POST A COMMENT

Request a New How-To Article

Looking for more How To information? Chances are there’s an eHow member who knows how to do what you’re looking to do. Submit an article request now!

eHow Article:  How to Generate N Different Random Numbers

eHow Education Editor

eHow Education Editor

Category: Education

Articles: See my other articles

Related Ads

Education

Schwengel
Meet Kurt Schwengel eHow’s Education Expert.