How to generate a random number between a and b in

2019-01-30 04:32发布

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)) ?

8条回答
We Are One
2楼-- · 2019-01-30 05:10

For 10 and 10**24

rand(10**24-10)+10
查看更多
混吃等死
3楼-- · 2019-01-30 05:12

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.

查看更多
登录 后发表回答