+ Reply to Thread
Results 1 to 4 of 4

Random whole number between 100000 - 999999

  1. #1
    Forum Contributor
    Join Date
    04-12-2013
    Location
    Usually at work, in the UK
    MS-Off Ver
    Excel 2010
    Posts
    639

    Random whole number between 100000 - 999999

    Can someone explain how to get this to work please, I have been reading for an hour and there are many different syntaxs and the one most commonly used doesn't work when I try to run a macro?

    Please Login or Register  to view this content.

  2. #2
    Registered User
    Join Date
    03-12-2014
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    41

    Re: Random whole number between 100000 - 999999

    Your function seems fine. an Integer only holds 32 bits hence it's unable to a hold a value that high.

    try this

    Please Login or Register  to view this content.

  3. #3
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: Random whole number between 100000 - 999999

    There's an instrinsic function for whole numbers.

    Please Login or Register  to view this content.
    Make Mom proud: Add to my reputation if I helped out!

    Make the Moderators happy: Mark the Thread as Solved if your question was answered!

  4. #4
    Forum Expert
    Join Date
    05-01-2014
    Location
    California, US
    MS-Off Ver
    Excel 2010
    Posts
    1,795

    Re: Random whole number between 100000 - 999999

    Quote Originally Posted by Sc0tt1e View Post
    Can someone explain how to get this to work please,
    [....]
    Please Login or Register  to view this content.
    Several mistakes:

    1. Use type Long instead of type Integer. Type Integer cannot handle values larger than 32767.

    2. Change the random expression to Int((999999 - 100000) * Rnd) + 100000. Your expression returns random values between 1 and 900001, not between 100000 and 999999 as intended.

    3. Add the following code in the beginning if you do not have similar code somewhere else.
    Please Login or Register  to view this content.
    That ensures that Rnd returns a difference sequence of random values each time.

    Generally, I agree with the suggestion to use WorksheetFunction.RandBetween(100000, 999999) instead. It is simpler.

    But WorksheetFunction.RandBetween is slower. On my computer, it is 20 times slower. YMMV. On the other hand, we are still talking about microseconds. Nothing to worry about unless your procedure is called many thousands of times.

+ 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: 5
    Last Post: 06-21-2013, 10:40 AM
  2. [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
  3. 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
  4. 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
  5. [SOLVED] How do I convert 999999 to 999-999 in an excel document?
    By Chris in forum Excel General
    Replies: 2
    Last Post: 01-06-2005, 04:06 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