2

Possible Duplicate:
Random number generator in C# - unique values

I'm trying to write a C# program that will generate a random number, check if this number is in my array, if it is, repeat generating the number, else insert this number into slot [i] of my array.

Here is my code so far:

        int check = 0;


        Random rand = new Random();

        int[] lotto = new int[6];



        for (int i = 0; i < lotto.Length; )
        {
            check = rand.Next(1, 41);
            while (!(lotto.Contains(check)))
            {

                lotto[i] = check;
                i++;
            }

            Console.WriteLine("slot " + i + " contains " + check);

        }
        Console.Read();
    }

UPDATE: Thanks figured it out, replaced the if with while :)

Community
  • 1
  • 1
  • Why is the C# tag here on SU? – Anirudh Ramanathan Sep 06 '12 at 06:23
  • Your question is not very clear. Can you please elaborate what you want? – Shakti Prakash Singh Sep 06 '12 at 06:30
  • `while (lotto.Contains(check)) check = rand.next(1, 41); lotto[i] = check;` should do it... – Thomas Sep 06 '12 at 06:33
  • @PriscillaKorostchuk this is quite easy to achieve, just before inserting new number into the array, check if it is already there. if it is - skip the number, and generate new one (make sure that you do not advance to the next position in the array). – Maciek Talaska Sep 06 '12 at 06:41
  • Shouldn't it be `if (!lotto.Contains(check))` with a `!`, I think? In C#, `!` means "not". – Jeppe Stig Nielsen Sep 06 '12 at 06:44
  • @PriscillaKorostchuk Your initial for loop was fine, it is the `if (lotto.Contains(check)) ...` which is "wrong". It checks if lotto contains check, and then adds it. – Thomas Sep 06 '12 at 06:45
  • I guess you can replace with `if(!lotto.Contains(check))` and in the `else` statement for the same if block, put `i--` – Shakti Prakash Singh Sep 06 '12 at 06:46
  • Argh damn you're right, forgot to add ! – Priscilla Korostchuk Sep 06 '12 at 06:48
  • Keep the code here so that in future if anybody lands on the question he/she find it useful. If the problem is solved you can accept an answer. Or answer your own question then accept it. Point is that here at SO its not just about me or you, its about the whole community. – Ankit Sep 06 '12 at 08:05

3 Answers3

1

I'm Guessing your question is what is not working, I am guessing that you have forgotten one ! and used an undeclared variable i:

if (!lotto.Contains(check)) // Ensure the number has not been chosen
{
    lotto[count] = check; // Set the number to its correct place
    count=count+1
}
Community
  • 1
  • 1
Spontifixus
  • 6,570
  • 9
  • 45
  • 63
0

You can try this:

for (int i = 0; i < lotto.Length;) 
{
  check = rand.Next(1, 41);
  Console.WriteLine("Random number to be checked is -> "+check);

  if (!lotto.Contains(check))
  {
     lotto[i] = check;
     i++;
  }

  Console.WriteLine("slot " + i + " contains " + check);
}

Notice that I have removed i++ from the for statement and has been put inside the if block.

You can try this with other loop constructs also, but this is to have the least amount of edit to your code.

Edit:

Well, I tried the code and seems to be working for me. Here is the complete code:

int check = 0;

int[] lotto = new int[6];

Random rand = new Random();

for (int i = 0; i < lotto.Length; )
{
    check = rand.Next(1, 41);
    Console.WriteLine("Random number to be checked is -> " + check);

    if (!lotto.Contains(check))
    {
        lotto[i] = check;
        i++;
    }

    Console.WriteLine("slot " + i + " contains " + check);
}

Console.ReadKey();

You can also use the while construct:

int check = 0;

int[] lotto = new int[6];

Random rand = new Random();

int i = 0;

while (i < lotto.Length)
{
    check = rand.Next(1, 41);
    Console.WriteLine("Random number to be checked is -> " + check);

    if (!lotto.Contains(check))
    {
        lotto[i] = check;
        i++;
    }

    Console.WriteLine("slot " + i + " contains " + check);
}

Console.ReadKey();

Basically, this is functions same as the previous code.

Shakti Prakash Singh
  • 2,414
  • 5
  • 35
  • 59
0

If you want to generate random numbers without repeats, use a FIPS 140-2 validated random number generator (see section 4.9.3 on page 36 of http://www.hhs.gov/ocr/privacy/hipaa/administrative/securityrule/fips1402.pdf).

If this is being used for any serious gaming purpose, as your variable naming suggests, I would recommend something with better randomness than Random.

akton
  • 14,148
  • 3
  • 43
  • 47