Random number (1 Viewer)

kirkm

Registered User.
Local time
Tomorrow, 00:29
Joined
Oct 30, 2008
Messages
1,257
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
Local time
Today, 06:29
Joined
May 11, 2011
Messages
11,611
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.
Local time
Tomorrow, 00:29
Joined
Oct 30, 2008
Messages
1,257
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
Local time
Today, 04:29
Joined
Oct 29, 2018
Messages
21,357
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.
Local time
Today, 07:29
Joined
May 21, 2018
Messages
8,463
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
Local time
Today, 06:29
Joined
Feb 28, 2001
Messages
26,996
@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.
Local time
Today, 22:29
Joined
Jul 4, 2013
Messages
2,770
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
Local time
Today, 06:29
Joined
Feb 28, 2001
Messages
26,996
Rnd can return zero under some circumstances, but I thought it "broke" if it ever reached zero.
 

Users who are viewing this thread

Top Bottom