Random Numbers Not Being Random

A

Anthony

For Some reason, i get the same value everytime...
Ive tested the RND Function with msgboxes and labels, they all have no
influence on the RND Function...
It still doesnt work...

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim count As Integer
Dim rndNumber(5) As Integer
Dim userNumbers(5) As Integer
Dim storage As Integer
Do While count < 5
storage = Val(InputBox("Enter a lottery number:",
"LOTTERY!", ""))
userNumbers(count) = storage
rndNumber(count) = Rnd()
count += 1
Select Case count
Case 1
lbl1.Text = storage
Case 2
lbl2.Text = storage
Case 3
lbl3.Text = storage
Case 4
lbl4.Text = storage
Case 5
lbl5.Text = storage
End Select
Loop
Dim lowernumber As Single = 100000
Dim uppernumber As Single = 1000000
Dim uppernumber2 As Single = 9
Dim lowernumber2 As Single = 1

lblRndLoot.Text = "$" & Int(lowernumber + rndNumber(5) *
(uppernumber - lowernumber)) & " Dollars!"

lblFirstValue.Text = 1 + Int(rndNumber(1) * 10)
lblSecondValue.Text = 1 + Int(rndNumber(2) * 10)
lblThirdValue.Text = 1 + Int(rndNumber(3) * 10)
lblFourthValue.Text = 1 + Int(rndNumber(4) * 10)
lblFifthValue.Text = 1 + Int(rndNumber(5) * 10)



End Sub
 
T

Timo

....
Dim storage As Integer
Randomize ' Initialize random-number generator
Do While count < 5
....

- Timo
 
T

Tom Shelton

Anthony said:
For Some reason, i get the same value everytime...
Ive tested the RND Function with msgboxes and labels, they all have no
influence on the RND Function...
It still doesnt work...

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim count As Integer
Dim rndNumber(5) As Integer
Dim userNumbers(5) As Integer
Dim storage As Integer
Do While count < 5
storage = Val(InputBox("Enter a lottery number:",
"LOTTERY!", ""))
userNumbers(count) = storage
rndNumber(count) = Rnd()
count += 1
Select Case count
Case 1
lbl1.Text = storage
Case 2
lbl2.Text = storage
Case 3
lbl3.Text = storage
Case 4
lbl4.Text = storage
Case 5
lbl5.Text = storage
End Select
Loop
Dim lowernumber As Single = 100000
Dim uppernumber As Single = 1000000
Dim uppernumber2 As Single = 9
Dim lowernumber2 As Single = 1

lblRndLoot.Text = "$" & Int(lowernumber + rndNumber(5) *
(uppernumber - lowernumber)) & " Dollars!"

lblFirstValue.Text = 1 + Int(rndNumber(1) * 10)
lblSecondValue.Text = 1 + Int(rndNumber(2) * 10)
lblThirdValue.Text = 1 + Int(rndNumber(3) * 10)
lblFourthValue.Text = 1 + Int(rndNumber(4) * 10)
lblFifthValue.Text = 1 + Int(rndNumber(5) * 10)



End Sub

Anthony,

Try inserting a call to randomize, just before your loop. DON'T DO IT
IN THE LOOP, just once at the begining of the method. See what
happens.
 
G

Greg

Tom Shelton said:
Anthony,

Try inserting a call to randomize, just before your loop. DON'T DO IT
IN THE LOOP, just once at the begining of the method. See what
happens.

Here's an example of how I generate random numbers.

'Display 100 random numbers between 1 and 6
Dim iCounter
Dim sAns As String = ""
Dim generator As New Random
For iCounter = 1 To 100
sAns = sAns & " " & Str((generator.Next(1, 6 + 1)))
Next
MsgBox(sAns)

Cheers.
 
T

Tom Shelton

Greg said:
Here's an example of how I generate random numbers.

'Display 100 random numbers between 1 and 6
Dim iCounter
Dim sAns As String = ""
Dim generator As New Random
For iCounter = 1 To 100
sAns = sAns & " " & Str((generator.Next(1, 6 + 1)))
Next
MsgBox(sAns)

Cheers.

I almost always use the Random class as well. But, the OP was using
RND - so, he needs to make a call to Randomize to initialize the
generator properly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top