+ Reply to Thread
Results 1 to 11 of 11

Random number generation with constraints

Hybrid View

  1. #1
    Registered User
    Join Date
    11-24-2011
    Location
    malaysia
    MS-Off Ver
    Excel 2007
    Posts
    19

    Random number generation with constraints

    Hi,

    I would like to generate random number (integer) for job sizes for various product where the total job size number is pre-determined. Besides this, the job size for each product should be within 1 to the max lot number. The example is show as in attached file.

    If you know how this could be done, please help. Thank you.
    Attached Files Attached Files
    Last edited by sc11; 01-14-2012 at 02:03 AM.

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,231

    Re: Random number generation with constraints

    Hi sc11 and welcome to the forum,

    Try in E5 the formula
    =RANDBETWEEN(1,G5)
    and pull it down to the other yellow cells.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Registered User
    Join Date
    11-24-2011
    Location
    malaysia
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Random number generation with constraints

    But, one more constraint is that total number of job size need to fix to 20 (as an example). Any idea ?

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,231

    Re: Random number generation with constraints

    So you want to pick 5 random numbers that have a maximum of the number in col G AND the sum of these random numbers needs to add up to 20?

    If the above is correct, then the numbers in column E aren't really random.

    See if the attached does it for you.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    11-24-2011
    Location
    malaysia
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Random number generation with constraints

    Yes. 5 random numbers have a maximum of the number in col G AND the sum of these random numbers needs to add up to 20. I want to find a way to automate this task.
    Instead of pressing F9, is it possible to write a code or macro so that it will achieve the desired sum number everytime it run?

  6. #6
    Valued Forum Contributor
    Join Date
    12-05-2011
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010 & 2013
    Posts
    308

    Re: Random number generation with constraints

    Hi

    The attached file uses macros to automate the whole process. Another bonus - the numbers don't change every time the sheet is recalculated - they only change when your run the macro.

    Enter the target in cell E10. Enter the maxima for each job.

    Press ALT-F8 and run "FindResult"

    The macro relies on there being two columns between the Max and Job columns. Also, I have added two named ranges.

    Cheers, Rob.
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    11-24-2011
    Location
    malaysia
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Random number generation with constraints

    Hi Rob,

    I not able to open the excel file:

    Error message:"Excel found unreadable content in "job size randomness.xlsm" Do you want to recover the contents of this workbook? If you trust the source of this workbook, click yes."

    When i clicked yes, the repaired file is blank.

    The repaired record:
    Excel completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.
    Removed Records: Worksheet properties from /xl/workbook.xml part (Workbook)

    How to fix this error? I really hope to try your solution.

    Regards,
    SC

  8. #8
    Valued Forum Contributor
    Join Date
    12-05-2011
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010 & 2013
    Posts
    308

    Re: Random number generation with constraints

    Hi

    Here is the retry of the upload. File size looks correct this time.

    Cheers, Rob.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    11-24-2011
    Location
    malaysia
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Random number generation with constraints

    Hi Rob,
    It is working this time, thank you. But the problem is everytime i re-run it, it gives me the same solution.
    I would like to have different combination of job sizes that could meet the target cell (E10). Is it possible to do so?

    Regards,
    SC

  10. #10
    Valued Forum Contributor
    Join Date
    12-05-2011
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010 & 2013
    Posts
    308

    Red face Re: Random number generation with constraints

    Apologies, SC, I forgot to include the Randomize command at the top of the macro.

    Attached file corrects this oversight.

    Cheers, Rob.
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    11-24-2011
    Location
    malaysia
    MS-Off Ver
    Excel 2007
    Posts
    19

    Re: Random number generation with constraints

    Rob, Thank you very much. It works exactly what i want.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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