Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code for creating randon numbers

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I have got to create a calculator for some course work.

I need to write some code so that two random numbers are displayed into two labels on the form.

These random numbers vary depending upon what difficulty level the user has chosen e.g. Level 1 the numbers range from 1 to 10; Level 2 the numbers range from 5 to 20; and level 3 where the numbers range from 21 to 99.


Could someone please help me ASAP

Thanks
 
To get you started make a form with 2 labels and 4 command buttons. The labels will be label1 and label2. The command buttons will be command1,2,3,and 4.

Set the caption for the first three commands to Level 1, Level 2, and Level 3. Set the caption for command4 to Quit.

Make the command1 button procedure as follows.

Private Sub Command1_Click()
Dim labela As Variant
Dim labelb As Variant
labela = Int(Rnd * 10)
labelb = Int(Rnd * 10)
If labela = 0 Then labela = 10
If labelb = 0 Then labelb = 10
Label1.Caption = labela
Label2.Caption = labelb
End Sub

Make the command4 procedure as follows:

Private Sub Command4_Click()
End
End Sub


This will set the label captions to the random numbers.

You will have to use some conditional expressions for the Level 2 and Level 3 ranges.
 
Hi Laura,

For 1 to 10
intVar = int(Rnd * 10) + 1

For 5 to 20
intVar = int(Rnd * 16) + 5

For 21 to 99
intVar = int(Rnd * 79) + 21

Jon
 
don't forget, in order for the numbers to truly be random you must use the randomize statement

sub command1_click()
Dim labela As Variant
Dim labelb As Variant

randomize

labela = Int(Rnd * 10)
labelb = Int(Rnd * 10)
If labela = 0 Then labela = 10
If labelb = 0 Then labelb = 10
Label1.Caption = labela
Label2.Caption = labelb
End Sub

tsmith
 
" ... don't forget, in order for the numbers to truly be random you must use the randomize statement ... "

Actually, you should review the documentation on this point.


MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
michealred,

Please, explain. Does the randomize [seed] statement not insure a new set of random numbers each time it is called? I have used

sub command1_click()

num=int(rnd*10)
label1.caption=str(num)
end sub

without the randomize statement I get a pattern of numbers, that repeat each time I run the program. When I place the randomize statement in the sub it appeared to fix the problem. Is this not correct?

tsmith
 
Hi TSmmith,

Yes you do need to seed the randomization but you only need to do it once. Normally you would stick in Form_Load, you have it in a Command Button so you'll be re-seeding each time the Sub is called.

Jon
 
MichaelRed's point, albeit not explicit in his message, is that the random number generator in VB is not actually "truly...[]...random", rather than criticising the use of randomize to start off a new sequence of (pseudo-) random numbers.

We had a fairly long thread about this in this forum a couple of months back.
 
... but the 'conclusions' reached in the thread are in the documentation ... As shown below.

Randomize uses number to initialize the Rnd function's random-number generator, giving it a new seed value. If you omit number, the value returned by the system timer is used as the new seed value.

If Randomize is not used, the Rnd function (with no arguments) uses the same number as a seed the first time it is called, and thereafter uses the last generated number as a seed value.

Note To repeat sequences of random numbers, call Rnd with a negative argument immediately before using Randomize with a numeric argument. Using Randomize with the same value for number does not repeat the previous sequence.
MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top