生成并分析1.5亿个7位随机数组:重复与唯一性分析
生成并分析1.5亿个7位随机数组:重复与唯一性分析
本程序生成1.5亿个7位随机数组,每个数组包含7个数字,并根据特定规则排列。分析这些数组的重复和唯一性,输出重复数组个数、不重复数组个数、前十个重复次数最多的数组和前十个重复次数最少的数组。
规则:
- 数组由'A,B,C,D,E,X,Y'组成,其中前5个数字'A,B,C,D,E'从小到大排列,互不相同;后2个数字'X,Y'从小到大排列,互不相同。
- 'A' 在1到7这7个数字中随机。
- 'B' 在(8,9,10,11,12,13,17)这7个数字中随机。
- 'C' 在(18,19,20,22,23,24,26)这7个数字中随机。
- 'D' 在27到33这7个数字中随机。
- 'E' 在29到35这7个数字中随机。
- 'X' 在1到7这7个数字中随机。
- 'Y' 在6到12这7个数字中随机。
代码:
import random
def generate_random():
A = random.randint(1, 7)
B = random.choice([8,9,10,11,12,13,17])
C = random.choice([18,19,20,22,23,24,26])
D = random.randint(27, 33)
E = random.randint(29, 35)
X = random.randint(1, 7)
Y = random.randint(6, 12)
return [A, B, C, D, E, X, Y]
# 生成1.5亿个随机数组
n = 150000000
arr_list = []
for i in range(n):
arr_list.append(generate_random())
# 计算重复和不重复数组个数
unique_arr = set(map(tuple, arr_list))
unique_count = len(unique_arr)
repeat_count = n - unique_count
# 统计重复次数
count_dict = {}
for arr in arr_list:
arr_tuple = tuple(arr)
if arr_tuple in count_dict:
count_dict[arr_tuple] += 1
else:
count_dict[arr_tuple] = 1
# 输出前十个重复次数最多的数组
most_common = sorted(count_dict.items(), key=lambda x: x[1], reverse=True)[:10]
print('前十个重复次数最多的数组:')
for arr, count in most_common:
print(list(arr), count)
# 输出前十个重复次数最少的数组
least_common = sorted(count_dict.items(), key=lambda x: x[1])[:10]
print('前十个重复次数最少的数组:')
for arr, count in least_common:
print(list(arr), count)
# 输出重复和不重复数组个数
print('重复数组个数:', repeat_count)
print('不重复数组个数:', unique_count)
程序执行结果:
该程序会输出以下信息:
- 前十个重复次数最多的数组及其出现次数。
- 前十个重复次数最少的数组及其出现次数。
- 重复数组个数。
- 不重复数组个数。
总结:
本程序使用Python语言生成了1.5亿个符合特定规则的7位随机数组,并分析了这些数组的重复和唯一性。通过统计分析,我们可以更好地了解随机数组的分布特点。
原文地址: https://www.cveoy.top/t/topic/nXL0 著作权归作者所有。请勿转载和采集!