0

How do i generate 6 random but unique numbers using C# in WinForms?

I have the following code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LottoGenerator
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Random rnd = new Random();
            int randnum = rnd.Next(1, 49); // creates a number between 1 and 49

            MessageBox.Show(Convert.ToString(randnum));
        }
    }
}

I would like to make sure the random number generated is not not a duplicate random number. How can i write logic to check if the number generated is the SAME as another previously generated number? and IF it is, then generate a new number.

Make sense?

PriceCheaperton
  • 5,071
  • 17
  • 52
  • 94
  • Store a list of the numbers as they are generated and keep generating until you get a number that isn't already in the list. – Ant P Apr 22 '14 at 18:06
  • Use an array to store the previous numbers that were generated, then see if the new number is already in that array. – BonzaiThePenguin Apr 22 '14 at 18:06
  • possible duplicate of [How to create an array of non repeating random numbers](http://stackoverflow.com/questions/22737687/how-to-create-an-array-of-non-repeating-random-numbers) – Pierre-Luc Pineault Apr 22 '14 at 18:19

3 Answers3

5

Generate list with [1;49] values, shuffle it and take elements one-by-one.

private List<int> list = null;

public Form1()
{
    InitializeComponent();
    Random rnd = new Random();
    list = Enumerable.Range(1, 49).OrderBy(r => rnd.Next()).ToList();
}    

private void button1_Click(object sender, EventArgs e)
{
    if (list.Count == 0)
        throw new InvalidOperationException();
    int randnum = list[list.Count - 1];
    list.RemoveAt(list.Count - 1);
    MessageBox.Show(randnum.ToString());
}

If you just need 6 random numbers out of 49, you can add .Take() method call.

list = Enumerable.Range(1, 49).OrderBy(r => rnd.Next()).Take(6).ToList();
Ulugbek Umirov
  • 12,719
  • 3
  • 23
  • 31
3

You need to keep your generated numbers in memory and then compare the newly generated number with the collection in memory.

You can use List<int> defined at class level, store the random number in the list and check if it already exist in the list.

List<int> list = new List<int>(); //defined at class level

private void button1_Click(object sender, EventArgs e)
{
    Random rnd = new Random();
    int randnum = rnd.Next(1, 49); // creates a number between 1 and 49

    if (!list.Contains(randnum))
    {
        list.Add(randnum);
    }
    else
    {
        //duplicate number
    }

    MessageBox.Show(Convert.ToString(randnum));
}

If you want 6 numbers then you can do:

Random rnd = new Random();
while (list.Count < 6)
{
    int randnum = rnd.Next(1, 49); // creates a number between 1 and 49

    if (!list.Contains(randnum))
    {
        list.Add(randnum);
    }
    else
    {
        //duplicate number
    }
}
Habib
  • 219,104
  • 29
  • 407
  • 436
2

Add a new container to store already generated values. And then while generating your number check to see if its in your list. If it is, continue to generating until its not in the list. Though since you can only have 49 distinct values you'd have to do some sort of other checking once you've generated all possible values.

List<int> lstRan = new List<int>();

private void button1_Click(object sender, EventArgs e)
        {
        Random rnd = new Random();
        int randnum = rnd.Next(1, 49); // creates a number between 1 and 49
        while (lstRan.Contains(randnum)) 
               randnum = rnd.Next(1,49);

        lstRan.Add(randnum);

        MessageBox.Show(Convert.ToString(randnum));
    }
pinkfloydx33
  • 11,863
  • 3
  • 46
  • 63