+ Reply to Thread
Results 1 to 9 of 9

Generating random numbers to obtain a fixed sum in column

  1. #1
    Registered User
    Join Date
    09-20-2019
    Location
    lebanon
    MS-Off Ver
    2016
    Posts
    36

    Generating random numbers to obtain a fixed sum in column

    Hi I'm trying to generate random number in column B for each userID that total sum for each userID is in column E,
    and if there is an order canceled in column D it will still blank ,
    but I didn't know how to do it, my code is here:

    Please Login or Register  to view this content.
    my attachement the first sheet of my code, and the second sheet what I really except
    Attached Files Attached Files
    Last edited by fatoom91; 01-17-2020 at 02:03 PM.

  2. #2
    Registered User
    Join Date
    09-20-2019
    Location
    lebanon
    MS-Off Ver
    2016
    Posts
    36

    Re: Generating random numbers to obtain a fixed sum in column

    can any one help me for this issue

  3. #3
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Generating random numbers to obtain a fixed sum in column

    Are you required to use a macro for this? Here's how I did it using spreadsheet functions:

    1) In G2, generate a random number or 0 if canceled =IF(D2="order canceled",0,RAND()) and copy down.
    2) In H2, sum up the random numbers for each userid =SUMIFS($G$2:$G$28,$A$2:$A$28,A2) and copy down.
    3) In I2, compute the random numbers =G2/H2*VLOOKUP(A2,$A$2:$E$28,5,FALSE).

    That will generate random reals that add up to your values. If you need random integers, then you can round the results to the nearest integer, adding or subtracting from one or more of the random numbers to make sure they all add up to the chosen value.

    Will that work for you?
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  4. #4
    Registered User
    Join Date
    09-20-2019
    Location
    lebanon
    MS-Off Ver
    2016
    Posts
    36

    Re: Generating random numbers to obtain a fixed sum in column

    Quote Originally Posted by MrShorty View Post
    Are you required to use a macro for this? Here's how I did it using spreadsheet functions:

    1) In G2, generate a random number or 0 if canceled =IF(D2="order canceled",0,RAND()) and copy down.
    2) In H2, sum up the random numbers for each userid =SUMIFS($G$2:$G$28,$A$2:$A$28,A2) and copy down.
    3) In I2, compute the random numbers =G2/H2*VLOOKUP(A2,$A$2:$E$28,5,FALSE).

    That will generate random reals that add up to your values. If you need random integers, then you can round the results to the nearest integer, adding or subtracting from one or more of the random numbers to make sure they all add up to the chosen value.

    Will that work for you?
    thanks for your helping but how I do know the random generating for step 3?

  5. #5
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Generating random numbers to obtain a fixed sum in column

    I'm not sure I understand the latest question. The random numbers used in step 3 were generated in step 1. Step 2 takes the random numbers generated in step 1 and sums them up. In step 3, we then divide each random number by the sum of all of the random numbers (for that id) to get a normalized random number where the sum of those normalized random numbers will add up to 1. Then, by multiplying by the desired target sum, we will get a collection of random numbers that add up to the desired sum.

  6. #6
    Registered User
    Join Date
    09-20-2019
    Location
    lebanon
    MS-Off Ver
    2016
    Posts
    36

    Re: Generating random numbers to obtain a fixed sum in column

    Quote Originally Posted by MrShorty View Post
    I'm not sure I understand the latest question. The random numbers used in step 3 were generated in step 1. Step 2 takes the random numbers generated in step 1 and sums them up. In step 3, we then divide each random number by the sum of all of the random numbers (for that id) to get a normalized random number where the sum of those normalized random numbers will add up to 1. Then, by multiplying by the desired target sum, we will get a collection of random numbers that add up to the desired sum.
    can I do this steps without normalized ? because when I changed the orders from decimal to integer the sum of them will change too!
    I mean I want to divide the sum for each User in the same ID in integer not in decimial bcz if I round them or change them to integer the sum of them will change too !
    Last edited by fatoom91; 01-22-2020 at 03:45 AM.

  7. #7
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,004

    Re: Generating random numbers to obtain a fixed sum in column

    Hi, fatoom91
    Try this:
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    09-20-2019
    Location
    lebanon
    MS-Off Ver
    2016
    Posts
    36

    Re: Generating random numbers to obtain a fixed sum in column

    Quote Originally Posted by Akuini View Post
    Hi, fatoom91
    Try this:
    Please Login or Register  to view this content.
    thanks so much !!

  9. #9
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,004

    Re: Generating random numbers to obtain a fixed sum in column

    You're welcome, glad to help, & thanks for the feedback.

+ 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. Generate random Number
    By ukyank in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-20-2014, 11:26 AM
  2. How can i generate a random number between 0.8 and 1.2
    By hawkinsr86 in forum Excel General
    Replies: 4
    Last Post: 01-03-2014, 10:03 AM
  3. Replies: 8
    Last Post: 11-18-2013, 04:24 PM
  4. Replies: 5
    Last Post: 06-21-2013, 10:40 AM
  5. [SOLVED] How to generate a random number?
    By frankk119 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-22-2012, 09:19 AM
  6. [SOLVED] Generate random 6-16-byte random hexadecimal number
    By jsamuelshn in forum Excel General
    Replies: 6
    Last Post: 07-20-2012, 10:38 AM
  7. Generate a random number
    By psatala in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-18-2011, 02:51 PM
  8. generate a random number and use if function to generate new data
    By Dogdoc1142 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 04-25-2006, 10:50 PM

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