A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Excel » General Discussion
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

VBA "Rnd" Function: Truly Random?



 
 
Thread Tools Display Modes
  #1  
Old September 23rd, 2005, 06:19 PM
TheRobsterUK
external usenet poster
 
Posts: n/a
Default VBA "Rnd" Function: Truly Random?


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


--
TheRobsterUK


------------------------------------------------------------------------
TheRobsterUK's Profile: http://www.excelforum.com/member.php...fo&userid=9924
View this thread: http://www.excelforum.com/showthread...hreadid=470300

  #2  
Old September 23rd, 2005, 08:19 PM
David Billigmeier
external usenet poster
 
Posts: n/a
Default

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" wrote:


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


--
TheRobsterUK


------------------------------------------------------------------------
TheRobsterUK's Profile: http://www.excelforum.com/member.php...fo&userid=9924
View this thread: http://www.excelforum.com/showthread...hreadid=470300


  #3  
Old September 27th, 2005, 04:50 AM
Jerry W. Lewis
external usenet poster
 
Posts: n/a
Default

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...9.36e9afd8%40p...

implements the 2003 algorithm in VBA.

An even better algorithm is the Mersenne Twister

http://www.math.sci.hiroshima-u.ac.j...at/MT/emt.html
http://www.math.sci.hiroshima-u.ac.j...S/FORTRAN/fort...
http://www-personal.engin.umich.edu/...neTwister.html

which is implemented in the freeware NtRand

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

Jerry

TheRobsterUK wrote:

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


 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Automatically up date time in a cell Mark General Discussion 5 May 12th, 2005 12:26 AM
clock Wildman Worksheet Functions 2 April 26th, 2005 10:31 AM
What if the back-end "moves"? John S. Ford, MD General Discussion 13 November 15th, 2004 09:33 PM
Function not evaluated Srinivas Chundi Worksheet Functions 1 January 31st, 2004 07:37 PM
Random function Chris G. Worksheet Functions 2 November 6th, 2003 01:09 AM


All times are GMT +1. The time now is 09:39 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.