+ Reply to Thread
Results 1 to 9 of 9

VBA RND and Randomize Repetition / Period Problem

  1. #1
    Registered User
    Join Date
    01-31-2014
    Location
    London
    MS-Off Ver
    Excel 2003, 2010
    Posts
    12

    VBA RND and Randomize Repetition / Period Problem

    I have come across a rather odd issue with excel / VBA and the RND function. The issue is that repeatedly reinitializing the RND function with Randomize results in a lot of repeated "random" values; what looks like a random number generator with a very short period.

    Now my understanding from these two MS links;

    http://msdn.microsoft.com/en-us/libr...=vs.90%29.aspx
    http://msdn.microsoft.com/en-us/libr...=vs.90%29.aspx

    Is that the randomize statement reinitialize the RND function using a seed value from the system clock. Since random numbers generated after repeated randomize statements are not the same, it would seem that the seed values generated by the system timer are in some way conspiring to produce a short period of random numbers.

    And surprise surprise there seems to be an issue with it;
    http://support2.microsoft.com/defaul...b;en-us;120587
    but I am not sure why or how this produces the results I am seeing.

    Can anyone shed some light on whats going on?

    See code below. If I enable the randomize statement in rand_wrapper() function (highlighted with lots of #########s) I get a lot of repeated values for the random number, if not I dont.

    Please Login or Register  to view this content.

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: VBA RND and Randomize Repetition / Period Problem

    Hi nb-,

    The following code describes my understanding of how random number generator seeding works. I prefer to use my own seed number, rather than rely on the Excel timer as you do.

    Please Login or Register  to view this content.
    Lewis

  3. #3
    Registered User
    Join Date
    01-31-2014
    Location
    London
    MS-Off Ver
    Excel 2003, 2010
    Posts
    12

    Re: VBA RND and Randomize Repetition / Period Problem

    Lewis, thanks for the info. I think I have some dubious typing in my code. I also note you are properly reinitializing RND before setting the seed.

    I am however still at a loss as to what the cause of the repetition I am getting is? E.g. in my example above for 10,000 numbers I essentially get 2,000 'random' numbers each repeated 5 times.

  4. #4
    Registered User
    Join Date
    01-31-2014
    Location
    London
    MS-Off Ver
    Excel 2003, 2010
    Posts
    12

    Re: VBA RND and Randomize Repetition / Period Problem

    As an aside (confusing the issue slightly between the worksheet function RAND() and the VBA function RND) I found this link quite interesting;

    http://www.vbforums.com/showthread.p...28%29-function

    It seems the native VB RND function only has a period of 16,777,216 which is ridiculous.
    Last edited by nb-; 10-29-2014 at 06:43 AM.

  5. #5
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: VBA RND and Randomize Repetition / Period Problem

    Hi nb-,

    Thanks for the reply.

    It seems the native VB RND function only has a period of 16,777,216 which is ridiculous.
    That is a given, since the Excel random number generator uses Single Precision (32 bit) Math. Back in the day, I rarely used the random number generators supplied by the compilers I used, but used a custom algorithm (long since lost by me), which had been known to give excellent pseudo-random results.

    I am however still at a loss as to what the cause of the repetition I am getting is? E.g. in my example above for 10,000 numbers I essentially get 2,000 'random' numbers each repeated 5 times.
    I ran your code exactly as written, with the addition of a comparison loop after the 'Quick Sort'. Each time I ran the code, I got 10,000 unique numbers. I also 'eyeball tested' goodness of fit by counting the numbers in certain ranges
    Please Login or Register  to view this content.
    and putting the results in rows 1 to 100. Without any statistical tests, the numbers seemed evenly distributed as I would expect.

    ----------------

    Please Login or Register  to view this content.
    In the above code, I think the Randomize statement is unnecessary, as you are reseeding the values during each random number access.

    Lewis

  6. #6
    Registered User
    Join Date
    01-31-2014
    Location
    London
    MS-Off Ver
    Excel 2003, 2010
    Posts
    12

    Re: VBA RND and Randomize Repetition / Period Problem

    The issue I get is actually much worse here than 5x repetition its more like 50x repetition in 10,000.

    Indeed the randomize statement in the rand_wrapper() function is unnecessary. Without the randomize statement I get perfectly sensible looking numbers but with it I get a great deal of repetition (excel 2010 / win 8.1) I am surprised you aren't able to replicate this problem I get it every time I run the code.
    E.g (lowest 199 values sorted from 10,000)

    With Randomize ;

    Please Login or Register  to view this content.

    Without Randomize;

    Please Login or Register  to view this content.

  7. #7
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: VBA RND and Randomize Repetition / Period Problem

    All my numbers are different with and without randomize. However I am running Excel 2003 on a Vista 32 bit system (circa 2008). Who says older isn't better.

    Lewis

  8. #8
    Registered User
    Join Date
    01-31-2014
    Location
    London
    MS-Off Ver
    Excel 2003, 2010
    Posts
    12

    Re: VBA RND and Randomize Repetition / Period Problem

    Hum... I wonder what it is, i get the same issues in excel 2003. I wonder what can be causing it... have to try it on a different machine.

  9. #9
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: VBA RND and Randomize Repetition / Period Problem

    Could be the system clock generating the same seed each time with Randomize. Try using your own seed number. My post #2 in this thread has an example.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 04-24-2013, 11:38 PM
  2. [SOLVED] Complicated:Calculate time period based on no. of units sold + fluctuations within period
    By omaha.crab in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-06-2012, 03:04 AM
  3. If Cell Ends with a Period, Remove Period
    By Ocean Zhang in forum Excel General
    Replies: 2
    Last Post: 08-07-2011, 04:18 PM
  4. Replies: 2
    Last Post: 08-06-2011, 11:15 AM
  5. Repetition
    By EAMOG in forum Excel General
    Replies: 5
    Last Post: 12-15-2008, 06:37 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1