# Random number (1 Viewer)

#### kirkm

##### Registered User.
Code:
``````Public Function randomValue(top As Integer, low As Integer)
Randomize    ' Initialize random-number generator.
randomValue = Int((top - low + 1) * Rnd) + low ' Generate random value between low and top.
randomValue = Int(top * Rnd) + low
End Function``````

Which calc is correct? They can't both be. can they ?
If a number wanted between 10 and 22 then it'd be int(13*rnd)+ 10. That's different to the 2nd calc.

#### plog

##### Banishment Pending
Here's the documentation:

Says this is the formula for a number from lowerbound thru upperbound (which means includes both upperbound and lowerbound):

Int((upperbound - lowerbound + 1) * Rnd + lowerbound)

#### kirkm

##### Registered User.
That's a third variety of the same (??) thing. But if it's right I'll use it. It's harder than you'd think to eliminate wrong ones.

#### theDBguy

##### I’m here to help
Staff member
That's a third variety of the same (??) thing. But if it's right I'll use it. It's harder than you'd think to eliminate wrong ones.
Is it a third method, or is it the same as the first one in your original post?

#### MajP

##### You've got your good things, and you've got mine.
Reason that Plog is correct is that rnd gives a number between 0 and 1, but I do not think it includes 0 and 1. So it is some number greate than 0 and less than 1.
if that is the case lets assume the top is 10 and bottom is 0
the 1st formula would give you
Int(10-0 + 1) * Rnd) + 0
or
int(11) * rnd
so this gives you 0 - 10 since int truncates

The second formula is wrong
Int(top * Rnd) + low
int(10 * rnd) + 0

this will give you a value between 0 and 9. Assume the rnd gave you .99999
You multiply by ten and you get 9.999
int gives you
9
you would never get 10.

And agree that is the same formula.

Last edited:

#### The_Doc_Man

##### Immoderate Moderator
Staff member
@MajP - you are correct in that RND excludes certain numbers in its range. The way it is computed, the number can never be 0 and can never be exactly 1.00000...000 either. The number starts (via Randomize) as a bit pattern plucked out of the middle bytes of the time-of-day clock multiplied by the seed number and then truncated. Thereafter, they extract and integer-multiply the middle bits of that number by the seed (again) and drop the results into a floating-point number, bytes 2-4, as the mantissa. Because of "hidden-bit" normalization, if you ever had 0 or 1.0000...000 your next number and all numbers thereafter would 0.

#### Cronk

##### Registered User.
I'd always thought Rnd could return 0. Just checked on the MS documentation
The Rnd function returns a value less than 1 but greater than or equal to zero.

@kirkm
Code:
`` randomValue = Int(top * Rnd) + low``
can return values higher than top

#### The_Doc_Man

##### Immoderate Moderator
Staff member
Rnd can return zero under some circumstances, but I thought it "broke" if it ever reached zero.

Replies
13
Views
188
Replies
6
Views
293
Replies
6
Views
141
Solved Union Query
Replies
5
Views
335
Replies
7
Views
326