0

I want to generate random numbers and put them in an array, but they should only appear once in this array. It's like a mini lotto game. This is the code I have right now:

int[] arrA = new int[10];
Random random = new Random();

for (int i = 0; i <= arrA.Length -1; i++)
{           
    arrA[i] = random.Next(1, 15);
    Console.WriteLine(arrA[i]);
}

Console.ReadKey();

Random numbers are generated and put in this Array. I only need to know how it's possible to program that they only appeare once.

canton7
  • 37,633
  • 3
  • 64
  • 77
Nicole
  • 127
  • 7

3 Answers3

1

Use HashSet<T>. A set is a collection that contains no duplicate elements, and whose elements are in no particular order.

Something like this:

using System;
using System.Collections.Generic;

HashSet<int> numbers = new HashSet<int>();
for (int i = 0; i < 10; i++)
{
    // Start with a random number
    //
    int value = random.Next(1,15);

    // Check whether you already have that number
    // Keep trying until you get a unique
    //
    while (numbers.Contains(value)) {
        value = random.Next(1,15);
    }

    // Add the unique number to the set
    numbers.Add(value);
}
foreach (int i in numbers)
{
    Console.Write(" {0}", i);
}
John Jesus
  • 2,284
  • 16
  • 18
  • 1
    You could add a while loop to ensure `numbers` always has 10 unique random values. – Aars93 Feb 21 '19 at 14:30
  • 1
    the hashset will not containt the 10 elements if the generated number exists already. the count = 10 - duplicated numbers – gandalf Feb 21 '19 at 14:31
  • Ah, right, thanks. I thought the same thing as soon as I posted. Edited to add the check. – John Jesus Feb 21 '19 at 14:39
  • You can simplify your code by using a return value from [Add](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1.add?view=netframework-4.7.2#returns). Something like `for (var i=0;i<10;++i) while (!numbers.Add(random.Next(1, 15)));` – Aleks Andreev Feb 21 '19 at 14:55
0
  int[] arrA = new int[10];
    Random random = new Random();

    for (int i = 0; i <= arrA.Length - 1; i++)
    {
        var number = random.Next(1, 15);
        while (arrA.Any(n => n == number))
        {
            number = random.Next(1, 15);
        }

        arrA[i] = number;
        Console.WriteLine(arrA[i]);
    }


    Console.ReadKey();
gandalf
  • 451
  • 5
  • 18
  • Thank you so much! But could you may explain to me how this 'while' works? – Nicole Feb 21 '19 at 14:15
  • while (arrA.Any(n => n == number)) means that if the generated random number exists in the array generate an other and so on, its a loop that finish only if the random number does't exist in the array. take a look on Microsoft documentation for more information and exemple – gandalf Feb 21 '19 at 14:18
  • Documentation: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/while – Jaskier Feb 21 '19 at 14:18
0

Everytime you generate a new number, instead of putting it in the array right away check the array from the start to the current position (using a for-loop). If it's not there insert the generated number, otherwise generate a new one and repeat the process.

texdade
  • 72
  • 10