+ Reply to Thread
Results 1 to 8 of 8

Random unique numbers

  1. #1
    Registered User
    Join Date
    11-23-2014
    Location
    Estonia
    MS-Off Ver
    2013
    Posts
    3

    Random unique numbers

    I hope this is the correct place to post this but I really do need help.

    I need to write a programm for excel vba and since we just now started to learn how to write code in vba im having issues. This is how it has to work:
    I click on a button which generates 7 unique numbers(5 in a range of 1-50 and 2 in a range of 1-8) and puts them in textbox.

    I've tried searching for a solution since this has been asked a thousand times I assume but the ones i've found seems really difficult so I havn't been able to replicate any of them. Any help would be highly appreciated.

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Random unique numbers

    Most of us here try not to give the exact answers for what would appear to be homework problems. You can derive the answer on your own at Chip Pearsons site: http://www.cpearson.com/excel/randomnumbers.aspx

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Random unique numbers

    The logic that I like for non-duplicating random selection is:

    To choose 2 random numbers between 1 and 8, without duplication:

    1) Create an array of the numbers 1 - 8 such that myArray(n) = n
    2) randomly re order that array

    for n from 1 to 8
    swap myArray(n) with myArray(random)
    next n

    3) choose the first two numbers in the reordered array.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  4. #4
    Registered User
    Join Date
    11-23-2014
    Location
    Estonia
    MS-Off Ver
    2013
    Posts
    3

    Re: Random unique numbers

    So far I know how to generate the random number but how to check if it's unique is beyond me. Also should I put the random number code into a loop that goes 5 times?

  5. #5
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Random unique numbers

    try this code it may help you
    the code generates unique numbers between 1 and 30
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    11-23-2014
    Location
    Estonia
    MS-Off Ver
    2013
    Posts
    3

    Re: Random unique numbers

    Could you explain it abit more please, I am really new at this. I don't understand how your if loop works.

    You do the randbetween function and you give the value that you get from that to variable c. Variable r is just used for moving so the numbers don't overlap but how does if not b(c) check for uniques?
    Last edited by Peidus; 11-23-2014 at 12:26 PM.

  7. #7
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Random unique numbers

    Look I have this code because I need that issue before . I'm not the author of the code
    Does it help you? If so you will find some dude to help how it works

  8. #8
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Random unique numbers

    the way that the Rand code in post #5 works is that when a number (c) is chosen the value of the array b(c) is set to True.
    Before putting a new C into the "chosen pile" it checks to b(C) to determine if it has already been chosen.

    The flaw in that is that when you get to the last number, it loops and loops and loops and .... until finally RandBetween() returns the last number.
    Thats a lot of useless looping.

    That code puts 1-30 in B1:B30 in random order.
    using the swap approach, this code will loop 60 times (30 times to make the array 1-30, 30 time to re-order it)
    Please Login or Register  to view this content.
    The loop count of the checking approach in Rand varies but testing shows it averages over 100 loops per run.

+ 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] Unique random numbers
    By ducecoop in forum Excel General
    Replies: 3
    Last Post: 05-09-2012, 04:41 PM
  2. Unique random numbers
    By Bartlett in forum Excel General
    Replies: 2
    Last Post: 01-28-2012, 03:42 PM
  3. random unique numbers
    By Juli in forum Excel General
    Replies: 2
    Last Post: 05-05-2011, 05:48 PM
  4. Excel 2007 : unique random numbers
    By mani01 in forum Excel General
    Replies: 6
    Last Post: 03-24-2010, 05:50 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