VBA "Rnd" Function: Truly Random?

T

TheRobsterUK

Hi,

I am having some doubts over the VBA "Rnd" function about if it really
generates a random number (between 0 and 1). In the Help file, it
states that:
Returns a Single containing a random number.

Syntax

Rnd[(number)]

The optional number argument is a Single or any valid numeric
expression.

Return Values

If number is Rnd generates
Less than zero The same number every time, using number as the seed.
Greater than zero The next random number in the sequence.
Equal to zero The most recently generated number.
Not supplied The next random number in the sequence.

Remarks

The Rnd function returns a value less than 1 but greater than or equal
to zero.

The value of number determines how Rnd generates a random number:

For any given initial seed, the same number sequence is generated
because each successive call to the Rnd function uses the previous
number as a seed for the next number in the sequence.

Before calling Rnd, use the Randomize statement without an argument to
initialize the random-number generator with a seed based on the system
timer.

Does this mean that after the first random number is generated, this is
used as the seed for the next one and that the number sequence from then
on is pre-determined?

What I actually want is the VBA equivalent of the worksheet RAND()
function, but I need truly random numbers, not ones that are
pre-determined by what came before!

So if I have the following code:


Code:
--------------------
Let i = 1 to 1000
Range("A1") = Rnd
next i
--------------------


Does that generate 1,000 truly random numbers or will they be in a
pre-determined sequence?!!?

Thanks
-Rob
 
G

Guest

Computers cannot give a "Truly Random" random number, just can't be done,
impossible. The nice thing about the VBA Rnd function, though, is you can
specify the seed by using "Randomize" like so:

function rand2()
Randomize 12345
rand2 = rnd
end function

Which generates random numbers based on a seed value of 12345

--
Regards,
Dave


TheRobsterUK said:
Hi,

I am having some doubts over the VBA "Rnd" function about if it really
generates a random number (between 0 and 1). In the Help file, it
states that:
Returns a Single containing a random number.

Syntax

Rnd[(number)]

The optional number argument is a Single or any valid numeric
expression.

Return Values

If number is Rnd generates
Less than zero The same number every time, using number as the seed.
Greater than zero The next random number in the sequence.
Equal to zero The most recently generated number.
Not supplied The next random number in the sequence.

Remarks

The Rnd function returns a value less than 1 but greater than or equal
to zero.

The value of number determines how Rnd generates a random number:

For any given initial seed, the same number sequence is generated
because each successive call to the Rnd function uses the previous
number as a seed for the next number in the sequence.

Before calling Rnd, use the Randomize statement without an argument to
initialize the random-number generator with a seed based on the system
timer.

Does this mean that after the first random number is generated, this is
used as the seed for the next one and that the number sequence from then
on is pre-determined?

What I actually want is the VBA equivalent of the worksheet RAND()
function, but I need truly random numbers, not ones that are
pre-determined by what came before!

So if I have the following code:


Code:
--------------------
Let i = 1 to 1000
Range("A1") = Rnd
next i
--------------------


Does that generate 1,000 truly random numbers or will they be in a
pre-determined sequence?!!?

Thanks
-Rob
 
J

Jerry W. Lewis

As David Brillinger has indicated, "random" numbers from the worksheet
RAND() function are also predetermined by what came before; but the
exact algorithm depends on your Excel version

http://support.microsoft.com/kb/q86523/
http://support.microsoft.com/kb/q828795/

It also appears that RNGs in the worksheet, Analysis ToolPak, and VBA
each uses a different algorithm. ATP, VBA and pre 2003 worksheet
algorithms are not suitable for serious simmulation work.

http://groups.google.com/groups?selm=2dc49b63.0402050649.36e9afd8@p...

implements the 2003 algorithm in VBA.

An even better algorithm is the Mersenne Twister

http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/VERSIONS/FORTRAN/fort...
http://www-personal.engin.umich.edu/~wagnerr/MersenneTwister.html

which is implemented in the freeware NtRand

http://www.numtech.com/NtRand/

Jerry
Hi,

I am having some doubts over the VBA "Rnd" function about if it really
generates a random number (between 0 and 1). In the Help file, it
states that:

Returns a Single containing a random number.

Syntax

Rnd[(number)]

The optional number argument is a Single or any valid numeric
expression.

Return Values

If number is Rnd generates
Less than zero The same number every time, using number as the seed.
Greater than zero The next random number in the sequence.
Equal to zero The most recently generated number.
Not supplied The next random number in the sequence.

Remarks

The Rnd function returns a value less than 1 but greater than or equal
to zero.

The value of number determines how Rnd generates a random number:

For any given initial seed, the same number sequence is generated
because each successive call to the Rnd function uses the previous
number as a seed for the next number in the sequence.

Before calling Rnd, use the Randomize statement without an argument to
initialize the random-number generator with a seed based on the system
timer.

Does this mean that after the first random number is generated, this is
used as the seed for the next one and that the number sequence from then
on is pre-determined?

What I actually want is the VBA equivalent of the worksheet RAND()
function, but I need truly random numbers, not ones that are
pre-determined by what came before!

So if I have the following code:


Code:
--------------------
Let i = 1 to 1000
Range("A1") = Rnd
next i
--------------------


Does that generate 1,000 truly random numbers or will they be in a
pre-determined sequence?!!?

Thanks
-Rob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Questions about Random Numbers in VB.NET 2
VB.NET Random / C++ rnd 12
is RND really random? 3
Generate random numbers 3
How random is Rnd 11
Randomiser 3
generate a sequence of random numbers 1
Key Generation 1

Top