How to Generate N Different Random Numbers
Determine if your programming language has a random function as well as a function to seed the random number generator., You need to seed the random number generator first, so that you don't get the same list of numbers each time you run it., To...
Step-by-Step Guide
-
Step 1: Determine if your programming language has a random function as well as a function to seed the random number generator.
For example the function may be rand() or random() for generating random numbers and random() or srandom() for seeding the random number generator.
Most modern languages do and the rand() or random() functions usually return a rational between 0 and
1. , To seed the random number generator, you need to pass an integer to the srand function.
Example: 'srand( (unsigned int) time(0))'.
This would pass the number of seconds since Jan 1,
1970. , For example, to get a random number between 0 and 50 type: random() %
50. ,, For each random number, we check if it's already in the array.
If it isn't, then we push it in, otherwise, we just loop and try again with a different number. -
Step 2: You need to seed the random number generator first
-
Step 3: so that you don't get the same list of numbers each time you run it.
-
Step 4: To make it an integer you multiply the result by the top value and round out the result.
-
Step 5: Understand that to get all the numbers to be different you will create a loop until you get as many numbers as you might need and then store the results to check they're not repeated: function random_numbers(int howmany
-
Step 6: int maxlimit) { int ret_array// This is the array where you'll store the generated numbers srand( (unsigned int) time(0) ) //seed the random number generator while(ret_array.size() < howmany){ int rnd = random() % maxlimit // Generate an int random number between 0 and"maxlimit" if(!includes(ret_array
-
Step 7: rnd)){ ret_array.push(rnd) // You're going to store only those numbers that don't belong to the array }} return ret_array }
-
Step 8: if you don't have that "includes" function
-
Step 9: it's quite easy to program: function includes(int array[]
-
Step 10: int number){ for(int i = 0; i < array.size(); i ){ if(array== number){ return true }} return false } Iterate as many times as needed in order to fill the array with different numbers.
Detailed Guide
For example the function may be rand() or random() for generating random numbers and random() or srandom() for seeding the random number generator.
Most modern languages do and the rand() or random() functions usually return a rational between 0 and
1. , To seed the random number generator, you need to pass an integer to the srand function.
Example: 'srand( (unsigned int) time(0))'.
This would pass the number of seconds since Jan 1,
1970. , For example, to get a random number between 0 and 50 type: random() %
50. ,, For each random number, we check if it's already in the array.
If it isn't, then we push it in, otherwise, we just loop and try again with a different number.
About the Author
Jose Cook
Professional writer focused on creating easy-to-follow organization tutorials.
Rate This Guide
How helpful was this guide? Click to rate: