Click here to Skip to main content
13,257,667 members (75,569 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


18 bookmarked
Posted 26 Jan 2011

How to generate many random various numbers?

, 28 Jan 2011
Rate this:
Please Sign up or sign in to vote.
How to generate many random various numbers?
1. We need an array of random various numbers between x[0] and x[n] like shuffling the cards.

For example: 8, 10, 5, 4, 7, 6, 3, 1, 9, 2 (between 1 and 10)

The following code is a method (with an overload) and generates an array of random various integer numbers:

public static int[] RandomNumbers(int min, int max)
    return RandomNumbers(min, max, 2);
public static int[] RandomNumbers(int min, int max, int derangement)
    if (min > max)
        throw new Exception("The first parameter must be less (or equal) than the second.");
    Random random = new Random();
    int count = max - min; ;
    int[] tempList = new int[count + 1];
    int counter = 0;
    for (int i = min; i <= max; i++)
        tempList[counter] = i;
    for (int i = 0; i < derangement; i++)
        for (int j = 0; j < count; j++)
            int k = random.Next(0, count + 1);
            int l = random.Next(0, count + 1);
            if (k != l)
		    //Swap TempList[k] with TempList[l]
                tempList[k] += tempList[l];
                tempList[l] = tempList[k] - tempList[l];
                tempList[k] = tempList[k] - tempList[l];
    return tempList;

Note: derangement is an integer variable to define the chance of the numbers derangement, but there is an inversely relationship between the performance and the value of the variable. The overload default is 2.

Now, It's ready to use the method:

int[] m = RandomNumbers(1, 56, 5);
foreach (int i in m)
    Console.Write("{0}, ",i);      

2. We need to generate a random number which has not been generated since runtime.

There has been a generated random number previously, but need to have a different one:

public static int RandomNumber(ref List<int> numbers, Random random)
    int count = numbers.Count;            
    int randomIndex = random.Next(0, count);
    int returnedNumber = numbers[randomIndex];
    return returnedNumber;

It's requisite for using the method, to have a random variable and a list of numbers, for example:

Random random = new Random();
List<int> numbers = new List<int>();
for (int i = 0; i <= 1000; i++)
//Ok, For each using the method, there is a new and defferent random number:
Console.WriteLine(RandomNumber(ref numbers, random));
Console.WriteLine(RandomNumber(ref numbers, random));
Console.WriteLine(RandomNumber(ref numbers, random));
Console.WriteLine(RandomNumber(ref numbers, random));
Console.WriteLine(RandomNumber(ref numbers, random));

The second approach has a better performance.

Good Luck!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Shahin Khorshidnia
Software Developer (Senior)
Iran (Islamic Republic of) Iran (Islamic Republic of)
Microsoft Certified Technology Specialist (MCTS)

"شاهین خورشیدنیا"

Nobody is perfect and neither am I, but I am trying to be more professional. In my humble opinion, you have to develop your skills, as long as you are responsible for developing even a tiny part of a sustainable software. That makes this job so special to me.

You may also be interested in...

Comments and Discussions

GeneralThanks Pin
kevininstructor9-Oct-14 11:14
memberkevininstructor9-Oct-14 11:14 
GeneralRe: Because it gives you a uniform distribution (which your meth... Pin
Henry.Ayoola2-Sep-11 5:31
memberHenry.Ayoola2-Sep-11 5:31 
GeneralReason for my vote of 5 Good tip. Pin
ProEnggSoft24-Feb-12 22:03
memberProEnggSoft24-Feb-12 22:03 
GeneralRe: Thank you very much. Pin
Shahin Khorshidnia25-Feb-12 4:39
memberShahin Khorshidnia25-Feb-12 4:39 
GeneralReason for my vote of 5 A genuine tip...Good work bro.. Pin
Pravin Patil, Mumbai13-Sep-11 1:29
memberPravin Patil, Mumbai13-Sep-11 1:29 
GeneralRe: Thank you. Pin
Shahin Khorshidnia14-Sep-11 22:35
memberShahin Khorshidnia14-Sep-11 22:35 
GeneralReason for my vote of 1 A better way of doing this has been ... Pin
Henry.Ayoola31-Aug-11 22:46
memberHenry.Ayoola31-Aug-11 22:46 
GeneralRe: Thank you. And would you explain why that way is better? Pin
Shahin Khorshidnia1-Sep-11 11:34
memberShahin Khorshidnia1-Sep-11 11:34 
GeneralNice article... Pin
Pradip_Bobhate31-Aug-11 5:52
memberPradip_Bobhate31-Aug-11 5:52 
GeneralReason for my vote of 5 Usefull... Some people couldn't prob... Pin
Toli Cuturicu13-Feb-11 12:44
memberToli Cuturicu13-Feb-11 12:44 
GeneralRe: Thank you Toli Pin
Shahin Khorshidnia14-Feb-11 1:15
memberShahin Khorshidnia14-Feb-11 1:15 
GeneralGood!!! Pin
shakil030400331-Jan-11 23:10
membershakil030400331-Jan-11 23:10 
GeneralRe: Thank you shakil Pin
Shahin Khorshidnia2-Feb-11 0:30
memberShahin Khorshidnia2-Feb-11 0:30 
GeneralReason for my vote of 5 Good trick. thank you Pin
Arahs28-Jan-11 3:40
memberArahs28-Jan-11 3:40 
GeneralRe: Thank you my friend. Pin
Shahin Khorshidnia28-Jan-11 22:48
memberShahin Khorshidnia28-Jan-11 22:48 
GeneralThank you for Voting, Russel In any case, we have performanc... Pin
Shahin Khorshidnia27-Jan-11 13:51
memberShahin Khorshidnia27-Jan-11 13:51 
GeneralReason for my vote of 5 Good But try RandomNumbers(1,100000... Pin
RusselSSC27-Jan-11 13:32
memberRusselSSC27-Jan-11 13:32 
GeneralRe: Thank you for Voting, Russel In any case, we have performan... Pin
Shahin Khorshidnia27-Jan-11 13:52
memberShahin Khorshidnia27-Jan-11 13:52 
GeneralReason for my vote of 5 nice - have 5 Pin
Pranay Rana26-Jan-11 21:48
memberPranay Rana26-Jan-11 21:48 
GeneralRe: Thank you Panay Rana Pin
Shahin Khorshidnia26-Jan-11 21:55
memberShahin Khorshidnia26-Jan-11 21:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 28 Jan 2011
Article Copyright 2011 by Shahin Khorshidnia
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid