+ Reply to Thread
Results 1 to 8 of 8

excel RandBetweenInt always give same result

  1. #1
    Forum Contributor
    Join Date
    12-25-2011
    Location
    Lahore, Punjab, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    287

    excel RandBetweenInt always give same result

    I have to generate a random number using formula RandBetweenInt. Problem is that I get same sequence of random numbers generated.

    Please see attached file > Sheet1 > Cell B2

    Generating random numbers always create a list 108, 117, 59, 61, 3

    Even if I save file and close and re-open it, it will generate random number same as before 108, 117, 59, 61, 3 and so on.

    Why excel doesn't create random numbers that are not preset?

    Regards
    Attached Files Attached Files
    Last edited by caabdul; 04-20-2018 at 02:08 AM.

  2. #2
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,819

    Re: excel RandBetweenInt always give same result

    I don't know how well this behavior is documented, but it has long been a property of VBA's random number generator. The random number generator will generate the same "random" sequence, unless you use the Randomize statement to reseed the random number generator. https://msdn.microsoft.com/en-us/vba...mize-statement

    The solution to your query should be as simple as adding a randomize statement to the UDF, or have an open event procedure the executes a Randomize statement to properly initialize VBA's random number generator.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

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

    Re: excel RandBetweenInt always give same result

    I think the behavior is very well documented. From the Randomize help page:

    Randomize uses number to initialize the Rnd function's random-number generator, giving it a new seed value. If you omit number, the value returned by the system timer is used as the new seed value.

    If Randomize is not used, the Rnd function (with no arguments) uses the same number as a seed the first time it is called, and thereafter uses the last generated number as a seed value.
    From the Rnd help page:

    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.
    [....]
    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.
    Last edited by joeu2004; 04-19-2018 at 11:15 PM. Reason: minor

  4. #4
    Forum Contributor
    Join Date
    12-25-2011
    Location
    Lahore, Punjab, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    287

    Re: excel RandBetweenInt always give same result

    All I need is a formula that generate different sequence of random numbers from 1 to 201 every time it is run, excluding the numbers given in a range.

    May be NOW() can influence it some way.

  5. #5
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,610

    Re: excel RandBetweenInt always give same result

    Per #2
    Please Login or Register  to view this content.
    Ben Van Johnson

  6. #6
    Forum Contributor
    Join Date
    12-25-2011
    Location
    Lahore, Punjab, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    287

    Re: excel RandBetweenInt always give same result

    I have worked out a formula, I don't want to use a macro.

    This formula seems working
    =ROUND(((upperlimit-lowerlimit)+1)*MOD(NOW(),0.0001)*10000,0)
    Where upperlimit is 201 and lowerlimit is 1 in my case. It seems working perfectly (however not able to exclude numbers without a macro) to generate a random number with varying sequence every time.
    Last edited by caabdul; 04-20-2018 at 01:03 AM.

  7. #7
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,610

    Re: excel RandBetweenInt always give same result

    Why is that formula preferred to RANDBETWEEN(lowerlimit,upperlimit)?

  8. #8
    Forum Contributor
    Join Date
    12-25-2011
    Location
    Lahore, Punjab, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    287

    Re: excel RandBetweenInt always give same result

    Thanks MrShorty, joeu2004, protonLeah

    Sorry I didn't know RandBetweenInt was a set function in my file. I am using this file a long time and I thought RandBetweenInt was a standard excel formula, lols.

    Anyways, Randomize works for me in macro.

+ 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. [SOLVED] Ignore blank cell to give give non zero result
    By rehan0129 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 01-24-2018, 12:04 PM
  2. Replies: 2
    Last Post: 10-05-2017, 10:45 AM
  3. [SOLVED] Excel appears to give incorrect result?
    By ancient333 in forum Excel General
    Replies: 2
    Last Post: 03-29-2017, 02:00 PM
  4. please help with compare two columns and give me result?
    By tomislav91 in forum Excel General
    Replies: 3
    Last Post: 01-24-2015, 02:48 PM
  5. Vlookup give same result on all rows
    By iebwen in forum Excel General
    Replies: 8
    Last Post: 07-17-2014, 03:16 AM
  6. [SOLVED] i have multiple lookup in my excel, its give result "0" i want blank outpu.
    By vengatvj in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 09-25-2013, 02:05 PM
  7. Read time from Excel Sheet give strange result
    By abhay in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-31-2007, 05:06 AM

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