在C#数组中,如何不创建重复的随机数?

时间:2012-10-11 09:25:27

标签: c# random duplicates

我是C#的初学者,试图以彩票形式申请。 有类型,首先你有5个提示(otos bool)和5个提示(hatos bool)。 并且有很多类型的抽奖数量(tiz,harminc,kilencven,negyvenot)。 我尝试使用此代码Array.Equals扫描抽奖后的数字:

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, ?);
    }
}

但是在这个时候,这个数字也将被自己扫描,所以它总是相等的。

顺便说一句,这是我的代码:

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));
        }
    }
}

4 个答案:

答案 0 :(得分:2)

如果您尝试从1..n 范围中选择数字而不重复,则需要将这些数字“洗牌”出来:

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];
}

其中numberToPick 5 otos6 hatosmaxNumber取决于tiz,{{ 1}},harminckilencvennegyvenotegyeni

如果您的egyeniertek很大而且您只想选择一些数字,则以下内容并不要求整个范围同时存在于内存中:

maxNumber

答案 1 :(得分:1)

试试这个!

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

答案 2 :(得分:0)

我花了很多时间才能得到这个,但我相信我能做到,现在已经完成了,通过更容易的方式,这个杀死每一个想法随机不重复,非常简单的代码没有任何哲学或困难的开发人员制作...(欢迎来到我的作品)(BEST OF THE BEST):

(1-10)之间没有任何重复的数字,1-我在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>在VB中有些不同我也有它:
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

答案 3 :(得分:0)

我是这样做的,如果你想要你可以像方法一样交换。

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] + " ");
    }
}

}