lundi 16 janvier 2017

Random number distribution generator with a guaranteed sum over a given number of iterations

I'm trying to write an algorithm that will generate a distribution of numbers that will sum to a defined value and will do it all in a given number of iterations.

This is what I've come up with in Python:

import random

count = 100
hours = 24
total_runs = 0
total_time = 0

while count > 0 and hours > 0:
    round = -(-count // hours) # Ceiling division

    while round > random.randint(round, count):
        round = random.randint(round, count)

    count -= round
    hours -= 1
    total_runs += round
    total_time += 1

    print('%d | %d' % (count, round))

print()
print('%d total runs' % total_runs)
print('%d total time' % total_time)

This algorithm works but doesn't produce a "random" distribution. The distributions it creates look like this:

95 | 5
90 | 5
85 | 5
80 | 5
76 | 4
72 | 4
68 | 4
64 | 4
60 | 4
56 | 4
52 | 4
48 | 4
44 | 4
40 | 4
36 | 4
32 | 4
28 | 4
24 | 4
20 | 4
16 | 4
12 | 4
8 | 4
4 | 4
0 | 4

100 total runs
24 total time

What algorithm would generate a distribution like so?

100 | 4
96 | 9
87 | 5
82 | 10
72 | 1
71 | 7
64 | 2
62 | 8
54 | 6
48 | 8
40 | 7
33 | 3
30 | 1
29 | 1
28 | 4
24 | 6
18 | 3
15 | 2
13 | 1
12 | 4
8 | 1
7 | 3
4 | 2
2 | 2

100 total runs
24 total time




Aucun commentaire:

Enregistrer un commentaire