如何在Ruby中生成a和b之间的随机数?

发布于 2024-10-06 22:30:36 字数 123 浏览 0 评论 0原文

例如,要生成 3 到 10 之间的随机数,我使用: rand(8) + 3

有没有更好的方法来执行此操作(例如 rand(3, 10)< /代码>) ?

To generate a random number between 3 and 10, for example, I use: rand(8) + 3

Is there a nicer way to do this (something like rand(3, 10)) ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

独夜无伴 2024-10-13 22:30:36

更新:Ruby 1.9.3 Kernel#rand 也接受范围

rand(a..b)

http://www.rubyinside.com/ruby-1-9-3-introduction-and-changes-5428.html

转换为数组可能成本太高,而且没有必要。


(a..b).to_a.sample

或者

[*a..b].sample

Array#sample

Ruby 1.8 中的标准.7+。
注意:在1.8.7中被命名为#choice,在以后的版本中被重命名。

但是无论如何,生成数组需要资源,你已经写的解决方案是最好的,你可以这样做。

UPDATE: Ruby 1.9.3 Kernel#rand also accepts ranges

rand(a..b)

http://www.rubyinside.com/ruby-1-9-3-introduction-and-changes-5428.html

Converting to array may be too expensive, and it's unnecessary.


(a..b).to_a.sample

Or

[*a..b].sample

Array#sample

Standard in Ruby 1.8.7+.
Note: was named #choice in 1.8.7 and renamed in later versions.

But anyway, generating array need resources, and solution you already wrote is the best, you can do.

一场信仰旅途 2024-10-13 22:30:36
Random.new.rand(a..b) 

其中 a 是最低值,b 是最高值。

Random.new.rand(a..b) 

Where a is your lowest value and b is your highest value.

离鸿 2024-10-13 22:30:36
rand(3..10)

内核#rand

当 max 为 Range 时,rand 返回一个随机数,其中 range.member?(number) == true。

rand(3..10)

Kernel#rand

When max is a Range, rand returns a random number where range.member?(number) == true.

挽清梦 2024-10-13 22:30:36

您可以使用范围运算符。请注意两种形式之间的区别:

3..10  # includes 10
3...10 # doesn't include 10

You can use range operators. Do note the difference between the two forms:

3..10  # includes 10
3...10 # doesn't include 10
你的笑 2024-10-13 22:30:36
def random_int(min, max)
    rand(max - min) + min
end
def random_int(min, max)
    rand(max - min) + min
end
卸妝后依然美 2024-10-13 22:30:36

请参阅这个答案:Ruby 1.9.2中有,但在早期版本中则不然。我个人认为 rand(8) + 3 很好,但如果您有兴趣,请查看链接中描述的 Random 类。

See this answer: there is in Ruby 1.9.2, but not in earlier versions. Personally I think rand(8) + 3 is fine, but if you're interested check out the Random class described in the link.

姜生凉生 2024-10-13 22:30:36

10 和 10**24

rand(10**24-10)+10

For 10 and 10**24

rand(10**24-10)+10
想挽留 2024-10-13 22:30:36

这是 #sample#rand 的快速基准测试:

irb(main):014:0* Benchmark.bm do |x|
irb(main):015:1*   x.report('sample') { 1_000_000.times { (1..100).to_a.sample } }
irb(main):016:1>   x.report('rand') { 1_000_000.times { rand(1..100) } }
irb(main):017:1> end
       user     system      total        real
sample  3.870000   0.020000   3.890000 (  3.888147)
rand  0.150000   0.000000   0.150000 (  0.153557)

因此,执行 rand(a..b) 是正确的做法

And here is a quick benchmark for both #sample and #rand:

irb(main):014:0* Benchmark.bm do |x|
irb(main):015:1*   x.report('sample') { 1_000_000.times { (1..100).to_a.sample } }
irb(main):016:1>   x.report('rand') { 1_000_000.times { rand(1..100) } }
irb(main):017:1> end
       user     system      total        real
sample  3.870000   0.020000   3.890000 (  3.888147)
rand  0.150000   0.000000   0.150000 (  0.153557)

So, doing rand(a..b) is the right thing

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文