473,320 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Random Numbers Not Being Random

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

Sep 21 '06 #1
5 1277
....
Dim storage As Integer
Randomize ' Initialize random-number generator
Do While count < 5
....

- Timo

"Anthony" <aM********@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.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

Sep 21 '06 #2

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.

--
Tom Shelton

Sep 21 '06 #3
Anthony

Go to this link, "Truly Random Numbers"

http://www.vb-tips.com/dbPages.aspx?...0-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

"Anthony" <aM********@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.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

Sep 21 '06 #4
"Tom Shelton" <to*@mtogden.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.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.
Sep 21 '06 #5

Greg wrote:
"Tom Shelton" <to*@mtogden.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.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

Sep 21 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Nicholas Geraldi | last post by:
Im looking for a decent random number generator. Im looking to make a large number of random numbers (100 or so, if not more) in a short period of time (as fast as possible). the function i was...
3
by: Joe | last post by:
Hi, I have been working on some code that requires a high use of random numbers within. Mostly I either have to either: 1) flip a coin i.e. 0 or 1, or 2) generate a double between 0 and 1. I...
21
by: Marc Dansereau | last post by:
Hi all I am new to this forum and to the c programming language. If I understand, the random() function in C return numbers that follow a uniform distribution U(0,1). Can somebody know how to...
5
by: cvnweb | last post by:
I am trying to generate 2 random numbers that are diffrent, in order to add them to existing numbers to generate numbers that start out the same, but are randomly added and subtracted so that they...
104
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a...
12
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
21
by: chico_yallin | last post by:
I just wana make a random id number based on4 digits-for examples?? Thanks in Advance Ch.Yallin
13
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000,...
6
by: badcrusher10 | last post by:
Hello. I'm having trouble figuring out what to do and how to do.. could someone explain to me what I need to do in order to work? THIS IS WHAT I NEED TO DO: Professor Snoop wants a program...
24
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.