# How to Generate N Different Random Numbers

Author Info

Updated: August 17, 2019

If you're a programmer, especially if you like creating games, you'll need a way to generate random numbers that are different from each other. For example, if you have a deck of cards and you need 3 different cards, you'll need 3 different random numbers. 3 random numbers will not do, as they may be random but identical. In this article, you will use pseudo-code; the instructions should be viable for any programming language.

## Steps

1. 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.
2. 2
Seed the random number generator first, so that you don't get the same list of numbers each time you run it. 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.
3. 3
Make it an integer by multiplying the result by the top value and rounding out the result. For example, to get a random number between 0 and 50 type: random() % 50.
4. 4
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, int maxlimit) {
int ret_array[howmany] // 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, rnd)){
ret_array.push(rnd) // You're going to store only those numbers that don't belong to the array
}}
return ret_array
}
```
5. 5
Program that "includes" function:
```function includes(int array[], int number){
for(int i = 0; i < array.size(); i  ){
if(array[i] == number){
return true
}}
return false
}
```
• Iterate as many times as needed in order to fill the array with different numbers. 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.

## Community Q&A

200 characters left

## Tips

• There are lots of ways to make this faster, but it depends on the programming language you use. For instance, in the .NET Framework you have a Sorted List class. You can store the numbers there, and then make the "includes" function using a binary search, which is much faster.
Thanks!

Submit a Tip
All tip submissions are carefully reviewed before being published
Thanks for submitting a tip for review!

## Warnings

• The numbers generated by random() n are not truly random, they are pseudo random. As such, they are not appropriate for secure applications.
Thanks!
• Make sure that a list of unique pseudo random numbers is really what you need. For instance, if you are shuffling a deck of cards, there are algorithms written specifically for this (and may do a better job).
Thanks!
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}