+ Reply to Thread
Results 1 to 5 of 5

Random number generator not always generating random numbers

  1. #1
    Registered User
    Join Date
    01-22-2016
    Location
    Canada
    MS-Off Ver
    365
    Posts
    41

    Random number generator not always generating random numbers

    Hi,

    I have five different work units each with different regulations and different number of regulations. Periodically, we do random inspections to endure the regulations are being followed. I created a macro to randomly generate three non-repeating numbers which correspond to the list of regulations. Each work unit has its own sheet and each sheet has its own random number generator macro. The only difference between each macro is the upperbound because the upperbound corresponds to the number of regulations in each work unit: Sheet 1 has 105 regulations, Sheet 2 has 109, Sheet 3 has 77, Sheet 4 has 71, and Sheet 5 has 74.

    The macro works as desired except for when Sheet 3 (named GFCCH) is the first sheet opened and the macro for random numbers relating to this sheet is run before any macro on any other sheet. Instead of random numbers, the set of three numbers generated are predictable such that if I run the macro four times, I’ll always get: 55/42/45; 23/24/60; 2/59/63; and 55/4/32. However, if I open Sheets 1,2,4, or 5 first and run the random number generating macro on that sheet, then go back to Sheet 3 to run the random number generating macro on Sheet 3, the three numbers generated on Sheet 3 will no longer be predictable.

    I have attached the workbook. Does anyone know why the results on Sheet 3 are not random ONLY if the random number generating macro on Sheet 3 is run first? I have this problem in both Excel 2016 and Excel 365 and with 32 or 64-bit installs.

    I thank you in advance for any help or advice you provide.
    Attached Files Attached Files

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,630

    Re: Random number generator not always generating random numbers

    Hi there,

    I know that you've asked for an explanation rather than a workaround, but this workaround will simplify and reduce the code you need to meet your requirements. The code I've used is as follows:

    Please Login or Register  to view this content.
    One routine for the entire workbook rather than one routine per worksheet.

    Maybe sometime I'll try to figure out why your code works the way it does, but in the meantime, happy random numbering!

    Hope this helps.

    Regards,

    Greg M
    Attached Files Attached Files

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: Random number generator not always generating random numbers

    The issue is you have not seeded the randomizer.

    All your sheets start with the same so-called random numbers when you first start a session (open the workbook). As a test, open the workbook and click only on Centres button and note the sets of numbers. Close and reopen the workbook and do the same. The sets of numbers are identical.

    The trick is to seed the Randomizer once when you first run the macro.
    Put this at the start of your macros. That should fix it.
    Please Login or Register  to view this content.

    On another note: you could have just one generator macro for all the sheets and have each button pass upper and lower linits to it.

    Please Login or Register  to view this content.
    Put this in a Standard code module e.g. Module1
    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  4. #4
    Registered User
    Join Date
    01-22-2016
    Location
    Canada
    MS-Off Ver
    365
    Posts
    41

    Re: Random number generator not always generating random numbers

    Thank you Greg M and AlphaFrog for the quick responses!

    This was a piece of code that I wrote for the five separate work units before they were merged together into one work unit. As a result, it was simpler for me to keep them as individual macros instead of figuring out how to rewrite the code to be more tight and efficient (I'm pretty much a novice to VBA programming). That is my excuse and I'm sticking to it!

    As for having to seed the randomizer...would never have thought of that, AlphaFrog. This is great info to have going forward.

    The knowledge of the experts on this forum continues to amaze me!

  5. #5
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,630

    Re: Random number generator not always generating random numbers

    Hi again,

    Many thanks for your feedback and also for the Reputation increase - much appreciated!

    You're welcome - glad I was able to help.

    Best regards,

    Greg M

+ 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. Generating a list of Random numbers of random length...
    By Growl in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-19-2013, 12:10 PM
  2. Replies: 5
    Last Post: 06-21-2013, 10:40 AM
  3. Random number generator, discluding specific numbers
    By JVesuvius in forum Excel General
    Replies: 2
    Last Post: 01-02-2013, 12:43 PM
  4. Need Random Number Generator but with Weighted Probability for Certain Numbers...
    By michaeljoeyeager in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 07-13-2012, 03:52 PM
  5. Need Help with a Random Number Generator with Weighted Probability for Certain Numbers
    By michaeljoeyeager in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-13-2012, 02:48 PM
  6. Generating a Random Number of Random Numbers
    By Garrus in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-10-2010, 09:39 AM
  7. Random number generator and matching all numbers
    By Zyphon in forum Excel General
    Replies: 2
    Last Post: 03-14-2008, 04:40 AM
  8. [SOLVED] Generating Random Number from a set of numbers
    By CalsLib in forum Excel General
    Replies: 3
    Last Post: 03-17-2006, 12:27 PM

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