VB.NET - Removing a Number from a Random Number Generator

I am trying to create a lottery simulator. The lottery has 6 numbers, the number of generated should be from 1 to 49 and can not be generated in the next quantity. I tried using the OR function, but I'm not quite sure if I use it correctly. Any help would be great. Thank.

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
+3
source share
8 answers

Use "While" instead of "If" - in other words, continue to generate random numbers until you find a new one. Currently, if you get a duplicate, and then get a duplicate on your second try, you will continue.

, 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 ... " ", . .

+5

, .

N ( ), , :

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

7.

(.. 1 7). , 3.

3, , :

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

, . 5 7:

+---+---+---+---+---+---+---+
| 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) . , , , .

+3

LINQ, VB2008:

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 , .

+1

, , , .

    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
+1

, . , . . , - , . :

    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
+1

- ( #)

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

    }
0

Instead of picking random numbers and checking for duplicates, you can simply scroll through the numbers and check the odds for each number to be chosen against the random number:

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

The list resultnow contains six numbers without duplicates, randomly selected from the range 1-49. As an added bonus, the numbers on the list are already sorted.

0
source

I think shuffling is also the fastest alternative. But it’s easier to read - your approach in combination with collections contains a function:

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
0
source

Source: https://habr.com/ru/post/1745205/


All Articles