+ Reply to Thread
Results 1 to 8 of 8

Random number generator with x number of duplicates

  1. #1
    Registered User
    Join Date
    10-12-2014
    Location
    Australia
    MS-Off Ver
    2019
    Posts
    54

    Random number generator with x number of duplicates

    Hi all,

    I was wondering how to generate random numbers with limited duplicates.

    I have a code to select these specific numbers only in VBA,

    Please Login or Register  to view this content.
    The thing is I want to limit my duplication as I want to run this on multiple rows.

    An example of this scenario would be:

    I have 100 rows, within the hundred rows there will be only 1 two, 3 fours, 4 fives, ..... , 99 one hundreds.

    Obviously there wouldn't be 191 numbers within 100 rows, just saying if a 4 is in the row then the program will now have two 4s left to insert or whether it will be used or not based on random choice.

    Hope this makes sense as I have tried searching but I have no luck in finding a solution.

    Please let me know how I can go about it.

    Cheers.
    Last edited by atomichybrid; 03-19-2016 at 06:40 AM.

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Random number generator with x number of duplicates

    Wow this is interesting.

    I am sure that I can do this.

    But I would need to use a user defined function.

    You haven't said if you wanted you output as a column, a row or a cell

    I will look at this and get back to you.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Registered User
    Join Date
    10-12-2014
    Location
    Australia
    MS-Off Ver
    2019
    Posts
    54

    Re: Random number generator with x number of duplicates

    Hi mehmetcik,

    Thanks for your response I do want this to output 1 number in each row in one column and I can offset it in anyway I like later on, I'm just having trouble with the logic of building it.

    Cheers

  4. #4
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Random number generator with x number of duplicates

    I think I have it.

    I have built in two lots of error handling for you, to use while we test it.

    The first one: calculates your RunSum from you Mn and Mx number, it then checks to make sure that you have enough available rows, RunReq.

    The second will break your loop at 2000 loops. you can increase this as you get to trust the macro.

    The routine works by filling an array with random numbers between your specified minimum and maximum numbers.

    It then counts between your min and max numbers and counts how many times that number exists in the array.
    If it matches or exceeds your limit then the number is replaced by another valid random number, obviously this could bust the limit of that number,
    so we raise a flag and loop back after our loop is complete. And we do it all over again.

    The Closer your RunSum is to your RunReq then the more loops you will need.

    I set the limit of loop backs to 2000, but this could be infinite if my first check proves to be effective.
    Even with 2000 loops my macro sometimes breaks out of the loop before finding a valid solution.

    But hey you wanted random numbers right?

    Lets see how you get on.


    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by mehmetcik; 03-19-2016 at 11:52 AM.

  5. #5
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Random number generator with x number of duplicates

    Thanks for posing such an interesting problem.

    I spent some time testing this. It is a bit confusing but I have made some progress.

    The latest version allows you to either enter a range, or a start address and the numbers of rows to insert

    I have created a sheet specific macro that looks for you entering a FactRand formula and then copies the formula down to the required number of rows.

    Th sheet specific macro copies and pastes values over the formulas that it adds to save some confusion.

    Please Login or Register  to view this content.
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: Random number generator with x number of duplicates

    Hi atomichybrid (and fellow Aussie ),

    Here's my attempt:

    Please Login or Register  to view this content.
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  7. #7
    Registered User
    Join Date
    10-12-2014
    Location
    Australia
    MS-Off Ver
    2019
    Posts
    54

    Re: Random number generator with x number of duplicates

    Thanks guys for your input and I have given this a run, sorry for the late response I have been busy in the past few days. I really appreciate the work and effort you guys have placed in making the code work nicely and effectively. I apologise if I have made some confusion along the lines of '1 two, 3 fours, 4 fives, ..... , 99 one hundreds'.

    My intention was that I only have the choice of numbers 2, 4, 5, 8, 10, 20, 50, 100. Meaning I can only have one 2, three 4s, four 5s, seven 8s, nineteen 20s, 49 fifties and ninety nine 100s, other numbers cannot appear as the choice selection is only in the list. I believe my heading of the thread was the confusing factor, it should be something like 'known number selection with x number of duplicates' or something along those lines.

    Great effort guys this has been progressing well, I could use these later on when generating random numbers with limited restriction.

    Cheers.

  8. #8
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,525

    Re: Random number generator with x number of duplicates

    Hi atomichybrid,

    Let us know how this goes:

    Please Login or Register  to view this content.
    Regards,

    Robert

+ 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] How do you prevent duplicates when using random number generator?
    By Aurbo in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-07-2015, 03:48 PM
  2. Replies: 5
    Last Post: 06-21-2013, 10:40 AM
  3. [SOLVED] Better Random Number Generator
    By WannaBeExceller in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-16-2013, 05:47 PM
  4. Random Number Generator
    By phattony72 in forum Excel General
    Replies: 2
    Last Post: 02-19-2008, 12:56 PM
  5. Random number generator
    By uncee in forum Excel General
    Replies: 6
    Last Post: 08-14-2007, 03:05 PM
  6. same number appears in a random number generator
    By Carmel in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 05-27-2006, 07:25 PM
  7. Try Random Number Generator
    By dok112 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-18-2005, 02:10 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