0

我是 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

4 回答 4

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

哪里numberToPick5ifotos6if hatos,并且maxNumber取决于tiz, harminc, kilencven, negyvenot,egyeniegyeniertek

如果您maxNumber的数字很大并且您只想选择几个数字,则以下内容不需要整个范围立即在内存中:

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);
}
于 2012-10-11T09:29:52.733 回答
1

试试这个!

if (i!=y && Array.Equals(lottoszamok[i], lottoszamok[y]))
于 2012-10-11T09:29:17.887 回答
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
于 2013-01-26T22:26:11.983 回答
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] + " ");
    }
}

}

于 2014-07-30T11:35:23.113 回答