Forum Discussion
Generate Random number with conditions
Nicoletta991 wrote: ``generate 400 random numbers [....] with mean 48 when Time is below 100. Mean 42 when time is between 1000 and 1700. Mean [sic; incomplete]``
I interpret that to mean: for all the random numbers associated with Time between 1000 and 1700, for example, their mean should be 42.
(Note that the column labeled Time appears to contain random numbers.)
It is impossible to offer a turnkey solution because of all the details that you omit. For example:
1. What version(s) of Excel do you use? I am sure a better solution is possible with Office 365 Excel, especially a "beta" version. I do not have that.
2. What should the mean of the numbers be when Time is 100 or more and less than 1000? Or is "100" a typo?
3. What should the mean of the numbers be when Time is more than 1700?
4. Do you require an exact mean of 42 when Time is between 1000 and 1700, for example? Or an approximate mean of 42? If the latter, with what degree of confidence (e.g. 95%)?
5. What is the acceptable range of random numbers in each category? For example, they must be non-negative (i.e. zero or more)? Or they must be positive (excludes zero)? They must be integers? If they can be non-integers, with what precision? Etc, etc, etc.
6. Do you require an Excel-only solution? Or would a VBA solution be acceptable?
7. Etc, etc, etc. (I am probably forgetting some details.)
For starters, if I want to generate 20 random non-negative integers (i.e. zero or more) with a mean of exactly 48, that is the same as saying that I want the 20 integers to sum to 960 (20*48).
I would accomplish that with the following paradigm.
Formulas:
B3: =B2*B1
A6: =ROUND($B$3*SUM($B$6:B6)/$C$6 - SUM($A$5:A5), 0)
B6: =RAND()
C6: =SUM(B6:B25)
C7: =SUM(A6:A25)
C8: =AVERAGE(A6:A25)
Copy A6:B6 down the columns
That is only one part of the solution, at best. But I hope it helps.