|
-
Sep 23rd, 2005, 01:05 PM
#1
Thread Starter
Hyperactive Member
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
http://www.sudsolutions.com
-
Sep 23rd, 2005, 01:46 PM
#2
Re: VBA "Rnd" Function: Truly Random?
That's about as random as you can get with vba. If you don't use the RANDOMIZE command before calling your sequence, you will get exactly the same numbers if you run it again. If you do call it, the sequence will be different each time.
Do you want to generate a number between 0 and 1? That is what you will get.
You can also say Int(Rnd*???)+1 where ???= the upper bound you want to pick from (minus 1). If you use 6, than the sequence of numbers will be between 1 and 6
-
Sep 23rd, 2005, 02:16 PM
#3
Thread Starter
Hyperactive Member
Re: VBA "Rnd" Function: Truly Random?
Hi,
Yes I just want to generate random numbers between 0 and 1, but I want them to be different each time the code is run. I don't want to use seeded values.
How would I use the Randomise function? What is the code?
Thanks
-Rob
http://www.sudsolutions.com
-
Sep 23rd, 2005, 03:12 PM
#4
Re: VBA "Rnd" Function: Truly Random?
Just place the call "Randomize" before your rnd code. It will help to make sure your numbers will not be repeated during each run. Also, check out the help file docs on Randomize for a more indepth explaination.
VB Code:
Randomize
Let i = 1 to 1000 '<-- Shouldnt it be For and not Let?
Range("A1") = Rnd
next i
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Sep 23rd, 2005, 03:16 PM
#5
Re: VBA "Rnd" Function: Truly Random?
 Originally Posted by TheRobster
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!
Use RANDOMIZE once.
Next call to RND() has been seeded by RANDOMIZE to be a random number.
Then each RND() will return a random number based on a seed of the prior random number - which is pretty random wouldn't you agree??
That is as random as it gets.
-
Sep 23rd, 2005, 03:23 PM
#6
Re: VBA "Rnd" Function: Truly Random?
Unless you use .NET which has a more random randomizer.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|