0

I am trying to make a loto number generator program that outputs 6 unique random numbers. I have bone this before with java using the collections.shuffle method. I cannot find such methods in c#. the only method I see available is the Random method. I tried to use this method in my code but it does not produce unique random numbers. sometimes is produces duplicat numbers in my loto number program. I have put a copy of my code here for you to have a look and have commented it for you so you can see what i am trying to do. Thanks.

    static void Main(string[] args)
    {
        Random number = new Random();// object to randomize the arraylist ,LIST.
        ArrayList list = new ArrayList();
        for (int i = 1; i <= 49; i++) // Loop to populate the arraylist with int values from 1 to 49.
        {
            list.Add(i);
            Console.WriteLine(number.Next(i)); // Output the contents of the arraylist in a randome order. (This produces duplicat numbers.
            // I am trying to produce a loto number generator program. No duplicat numbers. Each random number must be unique.
        }
        for (int i = 1; i <= 6; i++)// This loop was going to be used to trip the ammount of random numbers displayed.
        {
Gav
  • 1
  • 1
  • 1
  • There are many solution, how to reoder numbers in list or array. You can: generate list of numbers 1..49, reorder collection, take first 6 six. My extension method: http://stackoverflow.com/questions/25106133/re-ordering-arbitrary-an-array-of-integers/25106237#25106237 –  Aug 24 '14 at 19:10
  • 1
    [Fisher-Yates shuffle](http://en.wikipedia.org/wiki/Fisher-Yates_shuffle) http://stackoverflow.com/questions/9557883/random-plot-algorithm – L.B Aug 24 '14 at 19:11
  • http://stackoverflow.com/questions/1150646/card-shuffling-in-c-sharp seems very close. – Eugene Podskal Aug 24 '14 at 19:11

2 Answers2

-1

You can shuffle an array with the Fisher-Yates shuffling algorithm and a random number generator:

static void Shuffle(ref ArrayList list)
{
    Random rng = new Random();

    for (int i = list.Count - 1; i >= 0; i--)
    {
        // Note: It's important to only select a number into each index
        // once. Otherwise you'll get bias toward some numbers over others.
        int number = rng.Next(i); // Choose an index to swap here
        Swap(ref list[i], ref list[number]) // Swap it
    }
}

static void Swap(ref int first, ref int second)
{
    int temp = first;
    first = second;
    second = temp;
}

static void Main(string[] args)
{
    ArrayList list = new ArrayList();
    for(int i = 1; i <= 49; i++)
    {
        list.Add(i);
    }
    Shuffle(ref list);

    for (int i = 1; i <= 6; i++)
    {
        // Use the shuffled list
    }
}

If you just want to select six unique random numbers though, you don't need to shuffle an array. You can just keep track of previously generated numbers and regenerate the next one if you get a duplicate.

static ArrayList GetUniqueRandomNumbers(int min, int max, int count)
{
    Random rng = new Random();
    ArrayList result = new ArrayList();

    while(ArrayList.Count < count)
    {
        int number = rng.Next(min, max);

        // Only add if it hasn't been generated yet
        if (!result.Contains(number))
        {
            result.Add(number);
        }
    }
}

static void Main(string[] args)
{
    ArrayList loto = GetUniqueRandomNumbers(1, 49, 6);

    for (int i = 1; i <= 6; i++)
    {
        // Use the generated numbers
    }
}
Mirinth
  • 349
  • 2
  • 6
-2

Check if random number contains in ArrayList

    var rnd = new Random();

    var list = new ArrayList();

    while (list.Count <= 6)
    {
        int t = rnd.Next(1, 49);
        while (list.Contains(t))
        {
            t = rnd.Next(1, 49);
        }

        list.Add(t);
    }
Vlad
  • 256
  • 3
  • 14
  • +1 for noticing there might be a better way than shuffling. You might want to be more clear about the fact that you're taking a non-shuffling approach to the same problem though. That's probably getting you the downvotes. – Mirinth Aug 24 '14 at 19:39