VB.NET - Removing a number from a random number generator
Posted
by Alex
on Stack Overflow
See other posts from Stack Overflow
or by Alex
Published on 2010-05-13T01:23:43Z
Indexed on
2010/05/13
1:34 UTC
Read the original article
Hit count: 422
I am trying to create a lottery simulator. The lottery has 6 numbers, the number generated must be between 1 - 49 and cannot be in the next number generated. I have tried using the OR function but I'm not entirely sure if I am using it properly. Any help would be great. Thanks.
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
© Stack Overflow or respective owner