Click here to Skip to main content
15,922,407 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
Hello
I have a problem with random array. I need to have many randomize numbers but they have to be all dissimilar. If program generates 12, it must generate another number next time (not 12)!!!

I wrote this code:

C#
List<int> rn = new List<int>();
Random r = new Random();
bool br = true;

for (int p = 0; p < 10; p++)
{
    int i=0;
    while (true)
    {
        i = r.Next(1, 11);
        foreach (int a in rn)
        {
            if (a == i)
                br = false;
        }
        if (br == true)
            break;
    }
    rn.Add(i);
}


but after 4 or 5 numbers it is prolonging very bad.
help me please.:confused:
Posted

You should set
br=true;
before the
foreach (int a in rn)
loop

addendum
Try this :

for (int p = 0; p < 10; p++)
{
    int i=0;
    while (true)
    {
        i = r.Next(1, 11);
        if (!rn.Contains(i))
        {
            rn.Add(i);
            break;
        }
    }
}


Cheers
 
Share this answer
 
v2
Comments
JosephineBarry 13-Feb-11 5:02am    
Yes. I did it and it became much better thank you. but it is still prolonging with big numbers. example 10000

List<int> rn = new List<int>();Random r = new Random();bool br = true;

for (int p = 0; p < 10000; p++){ int i=0; while (true) {

i = r.Next(1, 10001); br = true;foreach (int a in rn)

{ if (a == i) br = false; }

if (br == true) break; } rn.Add(i);}but thank you.
Estys 13-Feb-11 5:07am    
If you mean by "prolongs" "it takes a long time", see my updated answer.
JosephineBarry 13-Feb-11 5:16am    
thanks
but it still takes a long time with big numbers.
sorry. :rose:
Shahin Khorshidnia 13-Feb-11 8:19am    
Vote 5
(perspicacity)
Hi,
I've written a tip and I think it can help.

How to generate many random various numbers?[^]

briefly:

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 requisite for using the method, to have a random variable and a list of numbers, for example:

//C#
Random random = new Random();
List<int> numbers = new List<int>();
for (int i = 0; i <= 1000; i++)
{
    numbers.Add(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));
Console.ReadKey();


And I think you'll see a good performance.
 
Share this answer
 
Comments
JosephineBarry 13-Feb-11 5:06am    
Thank you shahin
I did it and it works better than mine
thank you
Sandeep Mewara 13-Feb-11 5:58am    
Good answer.
Shahin Khorshidnia 13-Feb-11 7:18am    
Thank you guys

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900