Ruby - 范围内的随机数,但有例外

Chr*_*ian 2 ruby random exception

我有一系列抽奖的随机数字.

我如何选择第二名的随机数等等,而没有再次拉出第一名的风险?

$first = rand(0..99999)
$second = rand(0..99999)
$third = rand(0..99999)
Run Code Online (Sandbox Code Playgroud)

我需要在以下图纸中得到某种例外.

Gar*_*eth 10

shuffle将置换整个阵列,这对于大型阵列来说可能很慢.sample是一个快得多的操作

(1..99999).to_a.sample(3)
Run Code Online (Sandbox Code Playgroud)

出于基准目的:

> require 'benchmark'
> arr = (0..99999).to_a; 0
> Benchmark.realtime { 10_000.times { arr.sample(3) } }
=> 0.002874
> Benchmark.realtime { 10_000.times { arr.shuffle[0,3] } }
=> 18.107669
Run Code Online (Sandbox Code Playgroud)