从数组中选择唯一的随机数 [英] Choose unique random number from an array

查看:38
本文介绍了从数组中选择唯一的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

b 是我想要的最大赢家.

b is the maximum winner that I want.

b.times do
  winner = participant[rand(participant.count)]
end

我每次都需要产生一个独特的赢家.如何在不对该代码进行太多更改的情况下实现这一目标?

I need to generate a unique winner every time. How can I achieve this without making too many changes to this code?

推荐答案

已经有一个方法.只需使用 Array#sample:

There is already a method for that. Just use Array#sample:

winners = participants.sample(b)

这篇关于从数组中选择唯一的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆