首页 > 解决方案 > 经验洗牌检查python

问题描述

经验洗牌检查。运行计算实验以检查我们的改组代码是否像宣传的那样工作。编写一个程序,该程序采用整数命令行参数 m 和 n,对大小为 m 的数组进行 n 次洗牌,在每次洗牌之前用 a[i] = i 初始化,并编写一个 m×m 表,使得第 i 行给出所有 j 的 i 在位置 j 结束的次数. 数组中的所有条目都应接近 n/m。

我正在尝试在 python 中洗牌一个数组。我的代码是这样的:

import random
import sys

m = int(sys.argv[1])
n = int(sys.argv[2])
a = []      # a[i] = i
res =[]     # for resit the array as a

for i in range(m):
    a.append(i) # a[i] = i
res = a.copy()

for i in range(n):
    random.shuffle(a)
    print(a)
    # i can't understand how to print m-m table that i gives number of times of i wound up
    # in position j for all j.
    a = res.copy()

print(n/m)

标签: python

解决方案


推荐阅读