PDA

Click to See Complete Forum and Search --> : I need help!


qwertyvb
May 15th, 2003, 07:15 AM
How do i make numbers randomize when i want them to be between 1 and 10 only?



Please..i need help

TheAlchemist
May 15th, 2003, 11:13 AM
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:


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.

riis
May 15th, 2003, 01:11 PM
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:

RetVal = Rnd * 9 + 1


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:

RetVal = Int(Rnd * 10) + 1

sql_lall
May 16th, 2003, 04:36 AM
If you want 'truly' random numbers (at least, closer to truly random) then chuck this before your call:

Randomize Timer
'Picking the random number 1 to n:
rand_int= Int(Rnd*n)+1

qwertyvb
May 16th, 2003, 10:33 PM
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?

TheAlchemist
May 17th, 2003, 02:51 AM
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:



Int((UpperBound - LowerBound +1 ) * Rnd) +1