|
-
May 15th, 2003, 07:15 AM
#1
Thread Starter
New Member
I need help!
How do i make numbers randomize when i want them to be between 1 and 10 only?
Please..i need help
-
May 15th, 2003, 11:13 AM
#2
Addicted Member
hey mate,
the Rnd() function returns pseudo random numbers between 0 and 1 inclusive. to extend this range to between 1 and 10 just do this:
VB Code:
Dim RetVal As Single
RetVal = Rnd * 10 ' this will produce numbers between 0 and 10
all you have to do now is to test whether the produced number is less than 1 and redo the line if it is.
One thing that sustains me through life is the conciousness of the immense inferiority of everyone else
--Oscar Wilde
-
May 15th, 2003, 01:11 PM
#3
Fanatic Member
Hmm, I always thought that the Rnd function was 0 inclusive, but 1 exclusive.
Furthermore there's no need to delete numbers less than 0, with the following calculation:
The difference between 10 and 1 is 9. The minimum number is 1.
This function will return floating point values.
If you want to have a integer value between 1 and 10 (both inclusive) then you can use this calculation:
VB Code:
RetVal = Int(Rnd * 10) + 1
-
May 16th, 2003, 04:36 AM
#4
Fanatic Member
One more thing
If you want 'truly' random numbers (at least, closer to truly random) then chuck this before your call:
VB Code:
Randomize Timer
'Picking the random number 1 to n:
rand_int= Int(Rnd*n)+1
sql_lall 
-
May 16th, 2003, 10:33 PM
#5
Thread Starter
New Member
ok
but i want it the random number to be displayed on labels called
lblnumber1 'and
lblnumber2
how do i make it display the random numbers there when the form loads?
-
May 17th, 2003, 02:51 AM
#6
Addicted Member
hey guys,
Riis you're right. My mistake. The rnd function produces the following set : 0 =< Rnd < 1
it is not inclusive of 1.
According to the Visual Basic Documentation, to produce a pseudo random number between an upper bound and lower bound, inclusive, use the following formula:
VB Code:
Int((UpperBound - LowerBound +1 ) * Rnd) +1
One thing that sustains me through life is the conciousness of the immense inferiority of everyone else
--Oscar Wilde
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|