python随机整数数组_从数组中选择不相等的随机整数(python)

您需要的是Fisher-Yates shuffle上的变体。我不“做”python(我是一个Java用户),但是,这很简单。。。。在

以有序的方式创建源“set”的数组(从1到101的数组)。在

然后将变量last设置为array.size - 1,并执行以下操作:int[] ret = new int[5] // return array of 5 members.

for (int i = 0; i < 5; i++) { // 5 is the number of values you want.

int rand = random(last + 1) // get a random integer from 0 to last (includes last)

# put the value at array[rand] in your return array var

ret[i] = array[rand]

# move the value at the end to the value we just copied out:

array[rand] = array[last]

# decrease the size of the values we can select from:

last ;

}

这样你就可以从集合中选择5个随机值。没有两面派,都有相同的可能性。在

一个完整的费舍尔·耶茨洗牌可以在适当的地方完成整个阵型。我只是用你需要的算法的一部分。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值