How in Python to determine which random number will most often appear from the list of values starting from (00, 0,1,2 ... 36?

I recently started learning the random module. You need to do it 1000 times and identify the most frequent number, if possible in % relation, or at least how many times.

If you can still output not 1, but 5 of the most frequent numbers.

The problem with 00, I do not understand how to write it as a number.

Author: Kromster, 2020-03-12

2 answers

import random
from collections import Counter

MAX_NUM = 36        # 0, 1, ..., 36        (00 то же самое как 0)
RAND_NUMS = 1000    # сколько случайных
OUTPUT_NUMS = 3     # сколько нужно вывести

random_list = random.choices(range(MAX_NUM+1), k=RAND_NUMS)

print(Counter(random_list).most_common(OUTPUT_NUMS))

(00 same as 0.)


Test for MAX_NUM = 4, RAND_NUMS = 10 and OUTPUT_NUMS = 3:

In[217]: random_list = random.choices(range(MAX_NUM+1), k=RAND_NUMS)
In[218]: random_list
[3, 4, 2, 3, 1, 2, 2, 1, 3, 3]
In[219]: Counter(random_list).most_common(OUTPUT_NUMS)
[(3, 4), (2, 3), (1, 2)]

B %:

most_common = Counter(random_list).most_common(OUTPUT_NUMS)
most_common_perc = [(i, 100 * j / RAND_NUMS) for i, j in most_common]
print(most_common_perc)
[(3, 40.0), (2, 30.0), (1, 20.0)]
 1
Author: MarianD, 2020-03-13 04:13:29

If I understood correctly, then so somehow:

from random import choice
from collections import Counter

if __name__ == '__main__':
    rnd = [
        '00',
        '0',
        '1',
        '2',
# ...
        '31',
        '32',
        '33',
        '34',
        '35',
        '36'
    ]

    rnd_list = [choice(rnd) for _ in range(1000)]

    d = Counter(rnd_list)
    print(d.most_common(5))
    print({a[0]: f'{a[1] / 1000 * 100:.1f}%' for a in d.most_common(5)})
 1
Author: Ole Lukøje, 2020-03-13 14:30:32