Onlin
Onlin

Reputation: 7

In C# arrays, how not to create duplicated random numbers?

I'm a beginner in C#, trying to make a lottery form applicaton. There are types, first when you have 5 tips ( otos bool ) and 5 tips ( hatos bool ). And there are many types of how many numbers will be raffled (tiz, harminc, kilencven, negyvenot). I tried to scan the numbers after the raffle with Array.Equals with this code:

for (int i = 0; i <= 4; i++)
{ 
    for (int y = 0; y <= 4; y++)
    {
        if (Array.Equals(lottoszamok[i], lottoszamok[y]))
            lottoszamok[i] = r.Next (1, ?);
    }
}

but at this the number will be scanned with itself too, so it will be always equal.

here is my code by the way:

if (otos == true)
{
    for (int i = 0; i <= 5; i++)
    {
        if (tiz == true)
        {
            lottoszamok[i] = r.Next(1, 10);
        }
        else if (harminc == true)
        {
            lottoszamok[i] = r.Next(1, 30);
        }
        else if (kilencven == true)
        {
            lottoszamok[i] = r.Next(1, 90);
        }
        else if (negyvenot == true)
        {
            lottoszamok[i] = r.Next(1, 45);
        }
        else if (egyeni == true)
        {
            lottoszamok[i] = r.Next(1, (egyeniertek + 1));
        }
    }
}

if (hatos == true)
{
    for (int i = 0; i <= 6; i++)
    {
        if (tiz == true)
        {
            lottoszamok[i] = r.Next(1, 10);
        }
        else if (harminc == true)
        {
            lottoszamok[i] = r.Next(1, 30);

        }
        else if (kilencven == true)
        {
            lottoszamok[i] = r.Next(1, 90);
        }
        else if (negyvenot == true)
        {
            lottoszamok[i] = r.Next(1, 45);
        }
        else if (egyeni == true)
        {
            lottoszamok[i] = r.Next(1, (egyeniertek + 1));
        }
    }
}

Upvotes: 0

Views: 1957

Answers (4)

Ayman Sammoudi
Ayman Sammoudi

Reputation: 77

I spend many time to get this, but i believe i can do it, now it's done, By the Easier way in the word, this kill every think about Random not duplicate,very simply code without any philosophy or difficulty of Developers made ... (welcome to my work) that (BEST OF THE BEST):

Numbers between (1-10) without any duplicate, 1- MY WORK in C#

private void TenNumbersRandomly()
    {
       int[] a = new int[10];
       Random r = new Random();
       int x;
       for (int i = 0; i < 10; i++)
       {
          x= r.Next(1, 11);
           for (int j = 0; j <= i ; j++)
           {
              while (a[j] == x)
               {
                   x = r.Next(1, 11);
                  j = 0;
               }
           }
           a[i] = x;
           tb1.Text += a[i]+"\n";
       }
    }

2- in VB some Different i also have it :

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim x As Integer, i As Integer, j As Integer
    x = Int(Rnd() * 10) + 1
    Label1.Text = ""
    Dim a(9) As Integer
    For i = 0 To 9
        x = Int(Rnd() * 10) + 1
        For j = 0 To i
            While (a(j) = x)
                x = Int(Rnd() * 10) + 1
                j = 0
            End While
        Next j
        a(i) = x
        Label1.Text += a(i).ToString() + "  "
     Next i

Upvotes: 0

Ilian Grekov
Ilian Grekov

Reputation: 51

I made it this way, if you want you could put swapping like method.

static void SwapInts(int[] array, int position1, int position2)
{
    // Swaps elements in an array.

    int temp = array[position1]; // Copy the first position's element

    array[position1] = array[position2]; // Assign to the second element

    array[position2] = temp; // Assign to the first element
}
static void Main()
{                                                
    Random rng = new Random();

    int n = int.Parse(Console.ReadLine());

    int[] intarray = new int[n];

    for (int i = 0; i < n; i++)
    {
        // Initialize array
        intarray[i] = i + 1;
    }
    // Exchange resultArray[i] with random element in resultArray[i..n-1]
    for (int i = 0; i < n; i++)
    {
        int positionSwapElement1 = i + rng.Next(0, n - i);
        SwapInts(intarray, i, positionSwapElement1);
    }
    for (int i = 0; i < n; i++)
    {
        Console.Write(intarray[i] + " ");
    }
}

}

Upvotes: 0

Rawling
Rawling

Reputation: 50104

If you're trying to pick numbers from a range 1..n without repetitions, you need to "shuffle" the numbers out:

int[] allPossibleNumbers = Enumerable.Range(1, maxNumber).ToArray();
int[] picked = new int[numberToPick];
for (int i = 0; i < numberToPick; i++)
{
    int index = r.Next(i, maxNumber);
    picked[i] = allPossibleNumbers[index];
    allPossibleNumbers[index] = allPossibleNumbers[i];
}

where numberToPick is 5 if otos or 6 if hatos, and maxNumber depends on tiz, harminc, kilencven, negyvenot, egyeni and egyeniertek.

If your maxNumber is huge and you only want to pick a few numbers, the following doesn't require the whole range to be in memory at once:

Dictionary<int, int> outOfPlace = new Dictionary<int,int>();
int[] picked = new int[numberToPick];
for (int i = 0; i < numberToPick; i++)
{
    int shuffleOut = outOfPlace.ContainsKey(i) ? outOfPlace[i] : i;
    int index = r.Next(i, maxNumber);
    picked[i] = 1 + (outOfPlace.ContainsKey(index) ? outOfPlace[index] : index);
    outOfPlace[index] = shuffleOut;
    outOfPlace.Remove(i);
}

Upvotes: 2

Prasanna
Prasanna

Reputation: 4703

Try this one!

if (i!=y && Array.Equals(lottoszamok[i], lottoszamok[y]))

Upvotes: 1

Related Questions