English 中文(简体)
VB.NET - 从随机生成器中删除一些
原标题:VB.NET - Removing a number from a random number generator

我正试图制造一个非常简单的模拟器。 彩票有6个,产生的数量必须在1 - 49之间,不能在下个数字中。 我已尝试利用其他资源的功能,但我并不完全相信,如果我适当利用这一职能。 任何帮助都是巨大的。 感谢。

Public Class Form1

Private Sub cmdRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRun.Click
      #Creates a new Random class in VB.NET
    Dim RandomClass As New Random()



      ####################################
    Dim RandomNumber1 As Integer
    RandomNumber1 = RandomClass.Next(1, 49)
      #Displays first number generated
    txtFirst.Text = (RandomNumber1)



      ####################################
    Dim RandomNumber2 As Integer
    RandomNumber2 = RandomClass.Next(1, 49)
    If RandomNumber2 = RandomNumber1 Then
        RandomNumber2 = RandomClass.Next(1, 49)
    End If

      #Displays second number generated
    txtSecond.Text = (RandomNumber2)



      ####################################
    Dim RandomNumber3 As Integer
    RandomNumber3 = RandomClass.Next(1, 49)
    If RandomNumber3 = RandomNumber2 Or RandomNumber2 Then
        RandomNumber3 = RandomClass.Next(1, 49)
    End If

      #Displays third number generated
    txtThird.Text = (RandomNumber3)



      ####################################
    Dim RandomNumber4 As Integer
    RandomNumber4 = RandomClass.Next(1, 49)
    If RandomNumber4 = RandomNumber3 Or RandomNumber2 Or RandomNumber1 Then
        RandomNumber4 = RandomClass.Next(1, 49)
    End If

      #Displays fourth number generated
    txtFourth.Text = (RandomNumber4)



      ####################################
    Dim RandomNumber5 As Integer
    RandomNumber5 = RandomClass.Next(1, 49)
    If RandomNumber5 = RandomNumber4 Or RandomNumber3 Or RandomNumber2 Or RandomNumber1 Then
        RandomNumber5 = RandomClass.Next(1, 49)
    End If

      #Displays fifth number generated
    txtFifth.Text = (RandomNumber5)



      ####################################
    Dim RandomNumber6 As Integer
    RandomNumber6 = RandomClass.Next(1, 49)
    If RandomNumber6 = RandomNumber5, RandomNumber4, RandomNumber3, RandomNumber2, RandomNumber1 Then
        RandomNumber6 = RandomClass.Next(1, 49)
    End If

      #Displays sixth number generated
    txtSixth.Text = (RandomNumber6)


End Sub
最佳回答

换言之,不使用“if”一词,而是在发现新数字之前保持随机生成。 目前,如果你重复,然后又重复第二次尝试,你就会继续工作。

此外,虽然我没有VB专家,但我认为你需要全面具体说明每一项比较,而不是:

If RandomNumber3 = RandomNumber2 Or RandomNumber2 Then
    RandomNumber3 = RandomClass.Next(1, 49)
End If

你们需要:

While RandomNumber3 = RandomNumber1 Or RandomNumber3 = RandomNumber2 Then
    RandomNumber3 = RandomClass.Next(1, 49)
End While

这里有其他选择,例如生成1-49号数字,填满这些编号,然后排出头6个结果......或保留“直到出现新病例”但将结果保留在一组。 您可以避免太多的法典重复。

问题回答

你在这里不需要随机的编号生成器,你需要一个与细化算法相结合的生成器。

建立一系列<代码>N项目(例如,我们使用7个),每个项目都含有与其立场有关的分类:

+---+---+---+---+---+---+---+
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
+---+---+---+---+---+---+---+
                            <pool(7)

将集合体的大小定为7个。

然后,根据集合规模(即从1个到7个不等)生成随机数字。 让我说一下你的创造者回归3。

减去第3职位的价值,然后以最高价值取代,从而减少集合面积:

+---+---+---+---+---+---+---+
| 1 | 2 | 7 | 4 | 5 | 6 | 7 | -> 3
+---+---+---+---+---+---+---+
                        <pool(6)

然后,在你放弃所需价值之前,你才这样做。 如果我们的命运从7个增加到5个:

+---+---+---+---+---+---+---+
| 1 | 2 | 7 | 4 | 5 | 6 | 7 |
+---+---+---+---+---+---+---+
                            <pool(7)
rnd(7) returns 3
+---+---+---+---+---+---+---+
| 1 | 2 | 7 | 4 | 5 | 6 | 7 | -> 3
+---+---+---+---+---+---+---+
                        <pool(6)
rnd(6) returns 1
+---+---+---+---+---+---+---+
| 6 | 2 | 7 | 4 | 5 | 6 | 7 | -> 1
+---+---+---+---+---+---+---+
                    <pool(5)
rnd(5) returns 5
+---+---+---+---+---+---+---+
| 6 | 2 | 7 | 4 | 5 | 6 | 7 | -> 5
+---+---+---+---+---+---+---+
                <pool(4)
rnd(4) returns 2
+---+---+---+---+---+---+---+
| 6 | 4 | 7 | 4 | 5 | 6 | 7 | -> 2
+---+---+---+---+---+---+---+
            <pool(3)
rnd(3) returns 1
+---+---+---+---+---+---+---+
| 7 | 4 | 7 | 4 | 5 | 6 | 7 | -> 6
+---+---+---+---+---+---+---+
        <pool(2)

在那里,有5-7人(3,1,5,2,6人)没有复制的可能性,也没有高效的O(n)方法获得。 任何解决办法,如果已经使用,仅仅依靠随机抽取和核对,则效率就会降低。

如果是VB2008,则采用LINQ另一种选择:

Dim rnd As New Random()

Dim randomNumbers = From n in Enumerable.Range(1, 49) _
                    Order By rnd.Next() _
                    Select n _
                    Take 6

 Do something with the numbers here

这是这样做的简单方式。 如果使用Random语种不够随机,那么你可能不得不选择另一种方法。

你们必须把文本箱Im的名称改为你重新使用的。

    Dim rand As New Random
    Dim winnum As New List(Of Integer)
    Dim num, counter As Integer
    Dim result As String = ""

    Do
        num = rand.Next(1, 49)
        If winnum.Contains(num) Then
            Do
                num = rand.Next(1, 49)
            Loop Until winnum.Contains(num) = False
        End If
        winnum.Add(num)
        counter += 1
    Loop Until counter = 6

     Extracting and displaying the numbers from the array

    For n As Integer = 0 To 5
        result = winnum(n) & " " & result
    Next

     The textbox I m using to display the result is result.text

    result.Text = result

你们也可以使用守则,作为上文建议的另一名研究员。 在以下法典中,数字是随机生成的,从一组数字中删除,直到达到所需数量。 然后,所剩数字就显示。 然而,这并不是为抽签创造数字的好办法,因为数字序列有些可以预测,但它们是独特的。 该守则是:

    Dim rand As New Random, winnum As New List(Of Integer)
    Dim num As Integer, result As String = ""

    For n As Integer = 1 To 49
        winnum.Add(n)
    Next

    Do
        num = rand.Next(1, 49)

        If winnum.Contains(num) Then
            winnum.Remove(num)
        End If

    Loop Until winnum.Count = 7

    For n As Integer = 0 To 5
        result = winnum(n) & " " & result
    Next

    a.Text = result

我只想像(C#)那样做。

public static IEnumerable<int> Lotto(int max)
    {
        var random = new Random((int)DateTime.Now.Ticks);
        var numbers = new List<int>(Enumerable.Range(1, max));
        while(numbers.Count > 0)
        {
            int index = random.Next(1, numbers.Count) - 1;
            yield return numbers[index];
            numbers.RemoveAt(index);
        }
    }

    static void Main(string[] args)
    {


        var lotto = Lotto(49).GetEnumerator();
        lotto.MoveNext();
        int r1 = lotto.Current;
        lotto.MoveNext();
        int r2 = lotto.Current;
        lotto.MoveNext();
        int r3 = lotto.Current; 

        Console.WriteLine("{0} {1} {2}", r1, r2, r3 );

    }

不要抽取随机数字和重复检查,你只能穿透数字,检查每一次随机抽取的奇迹:

Dim count As Integer = 6   How many numbers to pick
Dim pos As Integer = 1   Lowest value to pick from
Dim items As Integer = 49   Number of items in the range

Dim rnd As New Random()
Dim result As New List(Of Integer)()

While count > 0
  If rnd.Next(items) < count Then
    result.Add(pos)
    count -= 1
  End If
  pos += 1
  items -= 1
End While

清单<代码>result目前载有6个数字,没有重复,随机摘自1-49。 作为额外奖金,名单上的数字已经分类。

我认为,窒息也是最快的替代办法。 但更容易阅读的是,你的方法加上收集功能:

Dim numbers As New List(Of Int32)
For i As Int32 = 1 To 6
    Dim containsNextNumber As Boolean = False
    While Not containsNextNumber
        Dim rnd As New Random(Date.Now.Millisecond)
        Dim nextNumber As Int32 = rnd.Next(1, 50)
        If Not numbers.Contains(nextNumber) Then
            numbers.Add(nextNumber)
            containsNextNumber = True
        End If
    End While
Next
numbers.Sort()  sort the numbers from low to high




相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?

热门标签