Results 1 to 6 of 6

Thread: VBA "Rnd" Function: Truly Random?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Location
    Right here
    Posts
    275

    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

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Location
    Right here
    Posts
    275

    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

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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:
    1. Randomize
    2.  
    3. Let i = 1 to 1000 '<-- Shouldnt it be For and not Let?
    4.     Range("A1") = Rnd
    5. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: VBA "Rnd" Function: Truly Random?

    Quote 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.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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
  •  



Click Here to Expand Forum to Full Width