+ Reply to Thread
Results 1 to 9 of 9

how to generate random numbers in range 1:20 with unique results

  1. #1
    Valued Forum Contributor
    Join Date
    01-19-2010
    Location
    Melbourne Australia
    MS-Off Ver
    latest is Excel 2016. have older versions
    Posts
    624

    how to generate random numbers in range 1:20 with unique results

    Hi Team,

    In an earlier life I was tasked with finding a "random" method of selecting two numbers from a "1 to 20" range so that the generated numbers can be applied to an set of people who will be partnered in a golf game draw.

    It is only one draw per year so I don't care if the players have previously played together in past years.

    I am hoping that the possible solution can easily be modified by a "passable knowledge level" person to be able to select a mystery "9" out of 18 holes that count for scores that particular round.

    I am sure golfers in the forum will understand my questions, happy to explain further to anybody who isn't following my request

    (btw: this is an issue only for the 20 guys who go away once a year to play golf, the world will not collapse if I have to draw numbers out of a hat, just looking for a slightly more elegant solution and I already have a few scoring macros so my first guess (but not only possibility) is VBA)

    Jmac

  2. #2
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: how to generate random numbers in range 1:20 with unique results

    Hi,

    Sorry - so to clarify you simply want a list of random numbers from 1 to 20 with no duplication?

    (And you're right - the golf stuff went straight over my head! )

    Regards
    Click * below if this answer helped

    Advanced Excel Techniques: http://excelxor.com/

  3. #3
    Valued Forum Contributor
    Join Date
    01-19-2010
    Location
    Melbourne Australia
    MS-Off Ver
    latest is Excel 2016. have older versions
    Posts
    624

    Re: how to generate random numbers in range 1:20 with unique results

    Hi XOR LX,

    There must be good golf courses in Yorkshire surely????

    The golf bit was to make sure people didn't think I was trying to win the pools or a lottery (been reading too many threads in the water cooler perhaps )

    Yes, I have 20 players that I am trying to form into teams so I need random numbers from 1 to 20 with no duplications as you correctly stated.

    I would also hope that a little tweaking to that "code" would allow me to generate random numbers from 1 to 18 with no duplicates for another purpose (mystery 9) although I will only ever need the first 9 numbers generated.

    As I said in OP, the old fashioned way would be to stick marbles / balls / cards into a hat and draw them out, I am trying to "blind the natives with black magic" so as to avoid any suggestions that the draw may be rigged in anyway..... can you believe some people would think that on a sporting trip

    Jmac

  4. #4
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: how to generate random numbers in range 1:20 with unique results

    There being good golf courses in Yorkshire and me understanding golf is not like a simple IF(TRUE formula!!

    One way is to put, in A1:

    =RANDBETWEEN(1,20)

    Then in A2 and copy down to A20:

    =INDEX(SMALL(IF(1-ISNUMBER(MATCH(ROW($1:$20),A$1:A1,0)),ROW($1:$20)),ROW(INDIRECT("1:"&20-COUNT(A$1:A1)))),RANDBETWEEN(1,20-COUNT(A$1:A1)))

    though obviously VBA would give you a much simpler set-up.

    Excel as "black magic", eh? Scary thought!

    Regards

  5. #5
    Valued Forum Contributor
    Join Date
    01-19-2010
    Location
    Melbourne Australia
    MS-Off Ver
    latest is Excel 2016. have older versions
    Posts
    624

    Re: how to generate random numbers in range 1:20 with unique results

    Hi XOR LX,

    Thanks again for your reply.

    I tried the suggestions but kept getting an error from the second formula (and it is too complicated for my poor old brain on a Sunday) so I took the "=RANDBETWEEN(1,20)" suggestion and started to play in VBA.

    I managed to get a result using the following code block
    Please Login or Register  to view this content.
    I am happy to admit there may be 100's of better ways to skin this particular cat but I have what I need so I am happy (The key output for me in the sequence the numbers are drawn in)

    A simple change to the range in the generate statement and my other issue is also resolved.

    I am a bit pleased with myself for getting this far but I acknowledge the start your response gave me, thanks heaps

    Jmac

  6. #6
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: how to generate random numbers in range 1:20 with unique results

    Sorry. I should've of course pointed out that the second formula is an array formula**.

    **Array formulas are not entered in the same way as 'standard' formulas. Instead of pressing just ENTER, you first hold down CTRL and SHIFT, and only then press ENTER. If you've done it correctly, you'll notice Excel puts curly brackets {} around the formula (though do not attempt to manually insert these yourself).

  7. #7
    Valued Forum Contributor
    Join Date
    01-19-2010
    Location
    Melbourne Australia
    MS-Off Ver
    latest is Excel 2016. have older versions
    Posts
    624

    Re: how to generate random numbers in range 1:20 with unique results

    Hmmmmm

    another lesson in how much I do not know about excel

    @ XOR LX, I will play with that new knowledge

    This forum give you so many new ideas, it is so helpful

    thanks for your help

    Jmac

  8. #8
    Forum Expert XOR LX's Avatar
    Join Date
    04-18-2013
    Location
    Turin, Italy
    MS-Off Ver
    Office 365
    Posts
    7,742

    Re: how to generate random numbers in range 1:20 with unique results

    Sure. And you're welcome!

  9. #9
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: how to generate random numbers in range 1:20 with unique results

    Keep it simple.
    See the attachment. The resulting unique random numbers in column B.
    Refresh using F9


    If you prefer a 100% VBA approach:

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by snb; 03-23-2014 at 11:06 AM.



+ 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: 22
    Last Post: 06-27-2024, 10:35 AM
  2. Formula to generate unique random numbers?!?
    By aims in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 04-07-2013, 03:14 PM
  3. Generate random non-repeated range of numbers?
    By satoshi in forum Excel General
    Replies: 2
    Last Post: 06-17-2009, 05:32 AM
  4. [SOLVED] generate unique random numbers
    By Stephen Larivee in forum Excel - New Users/Basics
    Replies: 7
    Last Post: 03-28-2006, 08:10 PM
  5. [SOLVED] Unique RANDOM NUMBERS within specified range
    By Matt D Francis in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 02-09-2006, 09:40 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