-10

I want to roll a dice from 1 to 1000. However, over many trials, I want the average to be 100.

How?

int weighted_rand(int min, int max, int avg);

...

weighted_rand(1, 1000, 100);

This isn't homework btw.

Uniform distributions uniform_int_distribution (C++11) uniform_real_distribution (C++11) generate_canonical (C++11) Bernoulli distributions bernoulli_distribution (C++11) binomial_distribution (C++11) negative_binomial_distribution (C++11) geometric_distribution (C++11) Poisson distributions poisson_distribution (C++11) exponential_distribution (C++11) gamma_distribution (C++11) weibull_distribution (C++11) extreme_value_distribution (C++11) Normal distributions normal_distribution (C++11) lognormal_distribution (C++11) chi_squared_distribution (C++11) cauchy_distribution (C++11) fisher_f_distribution (C++11) student_t_distribution (C++11) Sampling distributions discrete_distribution (C++11) piecewise_constant_distribution (C++11) piecewise_linear_distribution (C++11)

y2k
  • 65,388
  • 27
  • 61
  • 86
  • Choose appropriate distribution. – Jarod42 Dec 30 '17 at 22:58
  • 2
    What did you try? – Jongware Dec 30 '17 at 23:00
  • I know about std::uniform_int_distribution, but there are so many other distributions and I don't come from a math background so I do not know which one to use for this purpose. – y2k Dec 30 '17 at 23:06
  • " I want the average to be 100." - why? –  Dec 30 '17 at 23:06
  • Um, because that's what I need? It's irrelevant and implementation specific. – y2k Dec 30 '17 at 23:07
  • It is not clear what you are trying to carry out. The average is computed as the sum of elements divided by their number. This is a random experiment with a common assumption that its distribution is uniform which has a formula for its expected value. – CroCo Dec 30 '17 at 23:11
  • You can have uniform distribution from 2-1000 and adjust the probability with case 1. Is it acceptable ? – Jarod42 Dec 30 '17 at 23:16
  • @Jarod42, a die has solely six outcomes (i.e. 1,2,3,4,5, and 6). – CroCo Dec 30 '17 at 23:20
  • 1
    "Um, because that's what I need?" - point is, there may be a better solution to your problem than the one you propose - XY problem anyone? –  Dec 30 '17 at 23:20
  • @CroCo: As I understand, OP want to roll a unfair D1000 instead of a regular D6. (which indeed is not a common dice). – Jarod42 Dec 30 '17 at 23:23
  • 2
    Give 100 weight 1 and all the rest weight 0. – juanchopanza Dec 30 '17 at 23:26
  • A “random” function which always returns 100 would satisfy the constraints for range and average. As would a function which returns 1 in 80% of the cases, 370 in 16% of the cases and 1000 in 4% of the cases. So I *assume* you have some additional expectations for your distribution, but you need to make them explicit to get a suitable answer. – MvG Dec 30 '17 at 23:26

1 Answers1

1

You might be able to use a binomial distribution if its properties satisfy your needs. It gives you control over the maximum value and the mean. You can even choose a non-integer mean if you want to. You cannot choose the minimum, as that is always zero, so you may have to offset the result:

int weighted_rand(int min, int max, double avg) {
  std::binomial_distribution distribution{
    max - min,                // number of trials
    (avg - min) / (max - min) // success probability of each trial
  };
  return distribution(prng) + min;
}

Since a normal distribution is often used for continuous variables in the absence of more detailed information, using the binomial distribution as its discrete counterpart might be be a good solution.

But this is far from the only one, and depending on your use case, it might be terrible. You need to provide more details. As I wrote in a comment, a “random” function which always returns 100 would satisfy the constraints for range and average. As would a function which returns 1 in 80% of the cases, 370 in 16% of the cases and 1000 in 4% of the cases. So I assume you have some additional expectations for your distribution, but you need to make them explicit to get a suitable answer. The above assumes a kind of bell-shaped distribution.

MvG
  • 57,380
  • 22
  • 148
  • 276