+ Reply to Thread
Results 1 to 7 of 7

random number issue

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-13-2019
    Location
    Halifax, Canada
    MS-Off Ver
    2016
    Posts
    176

    random number issue

    Hi Gurus,

    I am using Rnd() function to generate random number in a loop; however, every time I run macro, the same random numbers are repeated. For clarity, I would like to post the following first 3 random numbers which repeat in the same positions.

    1. 0.705547511577606
    2. 0.533424019813538
    3. 0.579518616199493

    and so on.

    My loop looks like this:
    Sub Random()
    
    Dim R as Double, i as Integer
    
    For i = 1 to 1000
         R = Rnd() '(1st = 0.705547511577606, 2nd = 0.533424019813538, 3rd = 0.579518616199493)
         other codes ...............
    Next i
    
    End Sub
    My result should be different due to the use of random number but are in fact same. Is it a default pattern to generate random number? I was anticipating random number in every macro run with no repetitions. I appreciate your guidance.

    Thank you
    Roshan
    Last edited by Roshan.Shakya; 01-23-2021 at 08:50 PM.

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: random number issue

    
    Sub Random()
    
    Dim R as Double, i as Integer
    
    For i = 1 to 1000
    
         Randomize 10
    
         R = Rnd() '(1st = 0.705547511577606, 2nd = 0.533424019813538, 3rd = 0.579518616199493)
         other codes ...............
    Next i
    
    End Sub
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Forum Contributor
    Join Date
    05-13-2019
    Location
    Halifax, Canada
    MS-Off Ver
    2016
    Posts
    176

    Re: random number issue

    Thank you mehmetcik,

    Randomizing also gives the same pattern. The first 3 following random numbers (for simplicity) are same in every macro run.

    1. 0.574993312358856
    2. 0.492133736610413
    3. 0.271870911121368

    and so on ...

    I require different random numbers in every macro run. Is there a way to do it?

    Thank you
    Roshan

  4. #4
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: random number issue

    Either use Randomize without an argument, or use Randomize Application.WorksheetFunction.Randbetwwen(1,999999).

    That said, worksheet RAND() and RANDBETWEEN() use much better pseudorandom number generators than VBA's Rnd. If I recall correctly, VBA's Rnd uses an 1970s era linear congruential generator. MSFT updated Excel's worksheet RAND() to reflect a few extra decades of numerical programming research in Excel 2003 and more in Excel 2007.

    If you need to do serious Monte Carlo simulations, you should be using a real stats package rather than ANY spreadsheet, but if you have to use Excel, you'd actually be much better off using Application.WorksheetFunction.Rand() and consigning VBA's Rnd to the oblivion it deserves.

  5. #5
    Forum Contributor
    Join Date
    05-13-2019
    Location
    Halifax, Canada
    MS-Off Ver
    2016
    Posts
    176

    Re: random number issue

    Thank you hrlngrv,

    I found worksheet RAND() better alternative for my Monte Carlo Simulation. Thank you for sharing this valuable knowledge.

    Roshan

  6. #6
    Valued Forum Contributor
    Join Date
    12-14-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2007
    Posts
    439

    Re: random number issue

    Try this
    Sub Random()
    
    Dim R As Double, i As Integer
    Randomize
    For i = 1 To 1000
         R = Rnd() 
         'other codes ...............
    Next i
    
    End Sub
    ❖ Please mark your thread is SOLVED if there has been offered a solution that works fine for you.

    ❖ If you like solutions provided by anyone, feel free to add reputation by clicking on ✶ Add Reputation bottom left of their posts.

  7. #7
    Forum Contributor
    Join Date
    05-13-2019
    Location
    Halifax, Canada
    MS-Off Ver
    2016
    Posts
    176

    Re: random number issue

    Thank you all for sharing your valuable time and knowledge. I really appreciate your advices.

    Roshan

+ 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. Random issue when running macro
    By Karenba in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-24-2018, 12:22 AM
  2. Random Number - Random Selection
    By stribor40 in forum Excel General
    Replies: 5
    Last Post: 05-24-2017, 02:52 PM
  3. [SOLVED] Random numbers but each number not eqully random.
    By richhhh in forum Excel General
    Replies: 11
    Last Post: 09-06-2016, 01:16 PM
  4. Replies: 5
    Last Post: 06-21-2013, 10:40 AM
  5. [SOLVED] Generate random 6-16-byte random hexadecimal number
    By jsamuelshn in forum Excel General
    Replies: 6
    Last Post: 07-20-2012, 10:38 AM
  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. [SOLVED] How do I find random number in list of random alpha? (Position is.
    By jlahealth-partners in forum Excel General
    Replies: 0
    Last Post: 02-08-2005, 02:06 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