15,121,540 members
Articles / Programming Languages / C#
Tip/Trick
Posted 26 Jan 2011

65.6K views
18 bookmarked

How to Generate Many Random Various Numbers?

Rate me:
28 Jan 2011CPOL
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:

C#
```//C#
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;
counter++;
}

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 inverse relationship between the performance and the value of the variable. The overload default is `2`.

Now, it's ready to use the method:

C#
```//C#
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 we need to have a different one:

C#
```//C#
public static int RandomNumber(ref List<int> numbers, Random random)
{
int count = numbers.Count;
int randomIndex = random.Next(0, count);
int returnedNumber = numbers[randomIndex];
numbers.RemoveAt(randomIndex);
return returnedNumber;
}```

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

C#
```//C#
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 different 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 better performance.

Good luck!

Share

 Software Developer (Senior) 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.

 First Prev Next
 Thanks karenpayne9-Oct-14 11:14 karenpayne 9-Oct-14 11:14
 Re: Because it gives you a uniform distribution (which your meth... Henry.Ayoola2-Sep-11 5:31 Henry.Ayoola 2-Sep-11 5:31
 Reason for my vote of 5 Good tip. ProEnggSoft24-Feb-12 22:03 ProEnggSoft 24-Feb-12 22:03
 Re: Thank you very much. Shahin Khorshidnia25-Feb-12 4:39 Shahin Khorshidnia 25-Feb-12 4:39
 Reason for my vote of 5 A genuine tip...Good work bro.. Pravin Patil, Mumbai13-Sep-11 1:29 Pravin Patil, Mumbai 13-Sep-11 1:29
 Re: Thank you. Shahin Khorshidnia14-Sep-11 22:35 Shahin Khorshidnia 14-Sep-11 22:35
 Reason for my vote of 1 A better way of doing this has been ... Henry.Ayoola31-Aug-11 22:46 Henry.Ayoola 31-Aug-11 22:46
 Re: Thank you. And would you explain why that way is better? Shahin Khorshidnia1-Sep-11 11:34 Shahin Khorshidnia 1-Sep-11 11:34
 Reason for my vote of 5 Usefull... Some people couldn't prob... Toli Cuturicu13-Feb-11 12:44 Toli Cuturicu 13-Feb-11 12:44
 Reason for my vote of 5 Usefull... Some people couldn't probably do this.
 Re: Thank you Toli Shahin Khorshidnia14-Feb-11 1:15 Shahin Khorshidnia 14-Feb-11 1:15
 Good!!! shakil030400331-Jan-11 23:10 shakil0304003 31-Jan-11 23:10
 Re: Thank you shakil Shahin Khorshidnia2-Feb-11 0:30 Shahin Khorshidnia 2-Feb-11 0:30
 Reason for my vote of 5 Good trick. thank you Arash-Meh28-Jan-11 3:40 Arash-Meh 28-Jan-11 3:40
 Re: Thank you my friend. Shahin Khorshidnia28-Jan-11 22:48 Shahin Khorshidnia 28-Jan-11 22:48
 Thank you for Voting, Russel In any case, we have performanc... Shahin Khorshidnia27-Jan-11 13:51 Shahin Khorshidnia 27-Jan-11 13:51
 Reason for my vote of 5 Good But try RandomNumbers(1,100000... RusselSSC27-Jan-11 13:32 RusselSSC 27-Jan-11 13:32
 Re: Thank you for Voting, Russel In any case, we have performan... Shahin Khorshidnia27-Jan-11 13:52 Shahin Khorshidnia 27-Jan-11 13:52
 Reason for my vote of 5 nice - have 5 Pranay Rana26-Jan-11 21:48 Pranay Rana 26-Jan-11 21:48
 Re: Thank you Panay Rana Shahin Khorshidnia26-Jan-11 21:55 Shahin Khorshidnia 26-Jan-11 21:55
 Last Visit: 31-Dec-99 19:00     Last Update: 4-Dec-21 18:29 Refresh 1