定义整型数组a[10000],调用求随机数的系统库函数产生n(n≤10000)个范围在[1,10]中的随机数存入数组。然后分别统计其中1~5和6~10出现的次数及概率。
时间: 2023-05-29 13:06:02 浏览: 87
利用密码技术或者专门的随机数产生算法产生随机数,并对产生的随机数进行统计分析
```python
import random
a = [0]*10000
n = int(input("请输入随机数个数:"))
for i in range(n):
a[i] = random.randint(1, 10)
count1_5 = 0
count6_10 = 0
for i in range(n):
if a[i] <= 5:
count1_5 += 1
else:
count6_10 += 1
prob1_5 = count1_5 / n
prob6_10 = count6_10 / n
print("1~5出现次数:", count1_5, ",概率:", prob1_5, "\n6~10出现次数:", count6_10, ",概率:", prob6_10)
```
样例输出:
```
请输入随机数个数:100
1~5出现次数: 54 ,概率: 0.54
6~10出现次数: 46 ,概率: 0.46
```
阅读全文