+ Reply to Thread
Results 1 to 8 of 8

Create random number between 1 and 5 without getting the same number twice in a row

  1. #1
    Registered User
    Join Date
    11-20-2007
    Location
    Wales
    MS-Off Ver
    2013
    Posts
    31

    Create random number between 1 and 5 without getting the same number twice in a row

    Hi,

    I am trying to create a worksheet that has five different pictures appearing numerous times but I don't want the same picture to appear twice in a row. So far I have linked a picture to a number and have been using randbetween(1,5). I am presuming an if statement is needed but I have been unsuccessful in searching for a solution. When I say twice in a row I don't mean as in rows and columns I mean as in one after another. Does that make sense?

    Many thanks

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,384

    Re: Create random number between 1 and 5 without getting the same number twice in a row

    If it's random, it's random. Therefore, especially in a small sample, you may get the same number more than once. You need to store the number and compare the next number to the stored number. If it's the same, try again. When it's different, accept it and replace the stored number.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Create random number between 1 and 5 without getting the same number twice in a row

    ...deleted, hang on...
    Last edited by shg; 10-09-2016 at 06:28 PM.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Create random number between 1 and 5 without getting the same number twice in a row

    Here we go:

    A
    B
    1
    2
    4
    A2: =MOD(SUM(A1, RANDBETWEEN(0, 3)), 5) + 1
    3
    5
    4
    2
    5
    3
    6
    4
    7
    5
    8
    2
    9
    1
    10
    4
    11
    3
    12
    1
    13
    2
    14
    4
    15
    1
    16
    3
    17
    4
    18
    1
    19
    3
    20
    5
    21
    4

  5. #5
    Registered User
    Join Date
    11-20-2007
    Location
    Wales
    MS-Off Ver
    2013
    Posts
    31

    Re: Create random number between 1 and 5 without getting the same number twice in a row

    Thank you shg! That works perfectly and such a 'seemingly' simple formula. I would love to understand it fully.

    So MOD works out the remainder of two numbers, which is (SUM(A1, RANDBETWEEN(0, 3)) and 5.
    (SUM(A1, RANDBETWEEN(0, 3)) this adds the previous random number to another random number between 0 and 3. Why 0?
    And then you add a 1 at the end. Is this something to do with not ending up with a 0?

    If you have time to explain I'd really like to further my knowledge.

    Many thanks anyway

  6. #6
    Registered User
    Join Date
    11-20-2007
    Location
    Wales
    MS-Off Ver
    2013
    Posts
    31

    Re: Create random number between 1 and 5 without getting the same number twice in a row

    ...deleted, managed to duplicate post?

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Create random number between 1 and 5 without getting the same number twice in a row

    You're welcome.

    Consider the numbers as a ring: 1,2,3,4,5,1,2,3,4,5,... For any given number, the formula randomly chooses one of the four that follow.

    It would be simpler if the numbers cycled 0 to 4:

    A
    B
    1
    2
    1
    A2: =MOD(SUM(A1, RANDBETWEEN(1, 4)), 5)
    3
    0
    4
    2
    5
    0
    6
    1
    7
    4
    8
    1
    9
    0
    10
    1
    11
    0
    12
    2
    13
    3


    The difference between that and the other formula just caters to the sequence being 1-based instead of 0-based.

  8. #8
    Registered User
    Join Date
    11-20-2007
    Location
    Wales
    MS-Off Ver
    2013
    Posts
    31

    Re: Create random number between 1 and 5 without getting the same number twice in a row

    So it would be better if the RANDBETWEEN in A1 was RANDBETWEEN(0,4) rather than RANDBETWEEN(1,5) ?

    Currently trying to create a similar formula for 12 pictures this seems to be working

    =MOD(SUM(A1, RANDBETWEEN(0, 10)), 12) + 1

    Is this correct or just an accident? All I have done is make the 2nd number of the RANDBETWEEN two less than the total number of pictures but I don't know why.

    I have just tried setting A1 to RANDBETWEEN(0,11)
    and A2 to =MOD(SUM(A2, RANDBETWEEN(0, 10)), 11) but I get duplicates with this.

    Sorry I may be having a maths problem rather than an excel problem here

+ 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. Create random number when cell changes
    By Matt W in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-12-2011, 06:00 PM
  3. Can I create a random number without Analysis ToolPak?
    By rkjudy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-10-2011, 11:26 AM
  4. Create random number from 3-10
    By maacmaac in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 05-05-2009, 05:32 PM
  5. [SOLVED] How do I create a random number generator in excel?
    By Katie in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 07-19-2006, 08:55 PM
  6. [SOLVED] Create a unique random number
    By Jack in forum Excel General
    Replies: 2
    Last Post: 04-19-2006, 04:20 PM
  7. Create a New Random Number Every 2 Seconds
    By gatesheadthunde in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-06-2005, 05:15 PM
  8. Create Random Number Generator
    By Pascale in forum Excel Formulas & Functions
    Replies: 14
    Last Post: 09-06-2005, 12:05 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