1
random = Random.new.rand(1..50)
puts random

此代码每毫秒不断生成一个随机数。我想每 5 秒生成一个随机数。像这样的东西,

random = (Random.new.rand(1..50), 5000)

其中 5000 是指时间(每 5 秒生成 1 到 50 之间的随机数)

4

1 回答 1

1

您可以使用sleep暂停当前线程:

while true
  puts rand(50) + 1
  sleep 5
end
于 2014-09-30T07:46:58.867 回答