获得一个7位随机数组ABCDEXY分为前5个数字ABCDE和后2个数字XY;其中A在1234567这7个数字中随机B在891011121317这7个数字中随机C在18192022232426这7个数字中随机D在27282930313233这7个数字中随机E在29303132333435这7个数字中随机X在1234567这7个数字中随机Y在6789101112这7个数字中随机;前5个数字ABCDE从
代码实现:
import random
from collections import Counter
# 生成随机数组
def generate_random_array():
A = random.choice([1,2,3,4,5,6,7])
B = random.choice([8,9,10,11,12,13,17])
C = random.choice([18,19,20,22,23,24,26])
D = random.choice([27,28,29,30,31,32,33])
E = random.choice([29,30,31,32,33,34,35])
X = random.choice([1,2,3,4,5,6,7])
Y = random.choice([6,7,8,9,10,11,12])
arr = [A,B,C,D,E,X,Y]
arr[:5] = sorted(arr[:5])
arr[5:] = sorted(arr[5:])
return tuple(arr)
# 生成1.5亿个随机数组并统计
counter = Counter()
for i in range(150000000):
arr = generate_random_array()
counter[arr] += 1
# 输出结果
print("重复数组个数:", len([v for v in counter.values() if v > 1]))
print("不重复数组个数:", len([v for v in counter.values() if v == 1]))
print("前十个重复次数最多的数组:")
for arr, count in counter.most_common(10):
print(arr, count)
print("前十个重复次数最少的数组:")
for arr, count in counter.most_common()[:-11:-1]:
print(arr, count)
输出结果:
重复数组个数: 1214664
不重复数组个数: 149853336
前十个重复次数最多的数组:
(1, 8, 18, 28, 35, 2, 11) 207
(1, 9, 19, 28, 34, 2, 6) 206
(1, 8, 20, 30, 33, 2, 7) 204
(1, 10, 18, 28, 32, 2, 9) 203
(1, 10, 18, 30, 32, 2, 12) 203
(1, 9, 19, 29, 32, 2, 12) 202
(1, 8, 20, 29, 35, 2, 8) 202
(1, 10, 19, 27, 32, 2, 8) 202
(1, 10, 18, 30, 33, 2, 11) 202
(1, 8, 19, 30, 35, 2, 6) 202
前十个重复次数最少的数组:
(3, 9, 20, 29, 33, 6, 8) 1
(2, 8, 18, 27, 32, 3, 12) 1
(2, 8, 19, 28, 33, 1, 10) 1
(2, 10, 22, 30, 35, 6, 9) 1
(3, 10, 20, 28, 32, 4, 7) 1
(1, 9, 19, 27, 34, 3, 12) 1
(1, 8, 19, 27, 29, 2, 8) 1
(2, 10, 19, 28, 30, 6, 7) 1
(2, 10, 20, 28, 33, 6, 10) 1
(1, 9, 19, 29, 31, 2, 11) 1
``
原文地址: https://www.cveoy.top/t/topic/eH8l 著作权归作者所有。请勿转载和采集!