Random number generation
Hi every one out there!!
I have a big problem here- I want to generate random integers between 0 and 15, 0, 15 included.
but there is a condition, i donot want the numbers to repeat, it should be like:
(10,15,0,14,13,1,9,6,5,7,3,12,2,4,8,11)its an example!
can any one help me please?
I tried this but did not work, it repeats some integers
System.Random r = new Random();for (int i = 0; i < 15; i++){ System.Console.WriteLine(r.Next(0, 15));}
SolitairePosted Aug 24, 2005, 11:24 PM
What you want to use is a shuffle sort algorithm. Here is a sample Console Application program code using numbers from 1 to 20:
System;using
namespace
ShuffleSort_CS{
class Class1{
static void Main(string[] args){
int x, mix, temp, N = 20; string entry; int[] rndarray = new int[N + 1];Random randnum =
new Random(); for (x = 1; x <= N; x++)rndarray[x] = x;
Console.Write("{0} random numbers w/o duplicates. ", N);
Console.WriteLine("Enter to repeat, any key to stop.\n");
do{
for (x = 1; x <= N; x++){
mix = randnum.Next(1, N + 1);
temp = rndarray[mix];
rndarray[mix] = rndarray[x];
rndarray[x] = temp;
}
for (x = 1; x <= N; x++)Console.Write("{0} ", rndarray[x]);
Console.WriteLine("\n");
entry = Console.ReadLine();
}
while (entry == "");}
}
}
mr.storkPosted Aug 24, 2005, 6:21 AM
If it has been used, take a new one until Array[14] != 0;
mr.storkPosted Aug 24, 2005, 6:19 AM
System.Random r = new Random();
for (int i = 0; i < 15; i++)
{
System.Console.WriteLine(r.Next(0, 15));
}
ReddyPosted Aug 24, 2005, 4:19 AM