0
$\begingroup$

I'm creating a game in which someone opens a chest and the chest will give them a random prize. The maximum I can give out is 85,000,000 in 10,000 chests which is 8,500 average however I want some to make it so some chests will be below this value and above and to be able to set a min lose of 2,500 and max win 250,000 but still get the total value of 85,000,000.

I'm really struggling to come up with an algorithm for this using my C# knowledge.

  • 1
    The minimum is no problem, just subtract $2500\times 10000$ from $85$ million...so now you are distributing $60$ million but the minimum is $0$. The max is more of a hassle. [here](http://www.mathworks.com/matlabcentral/fileexchange/9700-random-vectors-with-fixed-sum?requestedDomain=www.mathworks.com) is code that claims to do it (I have not tested it personally).2017-01-06
  • 1
    Absent the max, I would reset the minimum to $0$ (as in my prior comment) and then choose $9999$ points uniformly on $[0,1]$...sort them and take the distances. That gives a random partition of $1$ and you can multiply by $60$ million to get what you want (without the max. condition unfortunately).2017-01-06
  • 0
    Another possible approach: Start with all chests containing 8,500. Transfer some random amount of prize between the first and second chest such that none of the bounds are violated. Repeat for the second and third, then third and fourth, and so on. In the end, shuffle all the chests to eliminate ordering bias. This probably does not give a uniform sampling on the space of allowed prize distributions, but is quite simple and fast and likely good enough for a game.2017-01-06

1 Answers 1

0

You want to partition a number (85,000,000) in 10.000 integer pieces with minimum value of 2,500

That's the same as partitioning the number 34,000 in 10,000 pieces at minimum of 1.

Just imagine that you are extracting 1 coin worth \$2500 from a bag of 34,000 coins. Each chest has at least one coin, so you need to reserve 10,000 coins, and you can only randomly distribute the remaining 24,000 coins.

Keep an accounting of how many coins remain each time a chest is looted.

You want also choice a probabilistic distribution with a minimum of 1 coin (\$2,500), average 3.4 (\$8,500), and maximum 100 coins (\$250,000))

A Poisson or Gamma distribution is maybe what you want.

Once you had chosen a probabilistic distribution, all you need is a random generator with that distribution. If you choose a Poisson, then just search "Algorithm for generating Poisson random values", and there are already lots of solutions.

You will not run short of coins, because you reserved one coin per chest. Near the end, you may have remaining coins non distributed, but because the number of chests is large (10,000), the expected value of the remaining coins will be close to 0, and much lower than 100, so you need to check that you have less than 100 coins for each remaining chest. On the rare case that the remaining coins are larger than the maximum prize multiplied by the remaining chests, just assign the maximum prize. Those cases would be very improbable, so it will not change your probabilistic distribution.