Home All Groups Group Topic Archive Search About

Random Numbers Not Being Random

Author
21 Sep 2006 4:58 AM
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

Author
21 Sep 2006 5:23 AM
Timo
....
Dim storage As Integer
Randomize   ' Initialize random-number generator
Do While count < 5
....

- Timo

Show quoteHide quote
"Anthony" <aMakiel***@gmail.com> wrote in message
news:1158814738.047233.129410@e3g2000cwe.googlegroups.com...
> 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
>
Author
21 Sep 2006 5:26 AM
Tom Shelton
Anthony wrote:
Show quoteHide quote
> 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.

--
Tom Shelton
Author
21 Sep 2006 5:56 AM
Greg
Show quote Hide quote
"Tom Shelton" <t**@mtogden.com> wrote in message
news:1158816404.102600.257040@i3g2000cwc.googlegroups.com...
>
> Anthony wrote:
>> 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.

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.
Author
21 Sep 2006 11:31 AM
Tom Shelton
Greg wrote:
Show quoteHide quote
> "Tom Shelton" <t**@mtogden.com> wrote in message
> news:1158816404.102600.257040@i3g2000cwc.googlegroups.com...
> >
> > Anthony wrote:
> >> 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.
>
> 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.

--
Tom Shelton
Author
21 Sep 2006 5:28 AM
Miro
Anthony

Go to this link, "Truly Random Numbers"

http://www.vb-tips.com/dbPages.aspx?ID=6242f843-83f1-449c-9c40-14135729046b

It talks about truly random numbers.
Cor makes quite a bit of posts on this newsgroup and this is part of his
website.

Miro

Show quoteHide quote
"Anthony" <aMakiel***@gmail.com> wrote in message
news:1158814738.047233.129410@e3g2000cwe.googlegroups.com...
> 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
>