Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Use <a href="http://www.ruby-doc.org/core-2.1.3/Kernel.html#method-i-rand" rel="noreferrer"><code>rand(range)</code></a></p> <p>From <a href="http://www.codeodor.com/index.cfm/2007/3/25/Ruby-random-numbers/1042" rel="noreferrer">Ruby Random Numbers</a>:</p> <blockquote> <p>If you needed a random integer to simulate a roll of a six-sided die, you'd use: <code>1 + rand(6)</code>. A roll in craps could be simulated with <code>2 + rand(6) + rand(6)</code>.</p> <p>Finally, if you just need a random float, just call <code>rand</code> with no arguments.</p> </blockquote> <hr> <p>As <a href="https://stackoverflow.com/users/8279/marc-andre-lafortune">Marc-André Lafortune</a> mentions in <a href="https://stackoverflow.com/questions/198460/how-to-get-a-random-number-in-ruby/2773866#2773866">his answer below (go upvote it)</a>, <a href="http://www.ruby-lang.org/en/news/2009/07/20/ruby-1-9-2-preview-1-released/" rel="noreferrer">Ruby 1.9.2 has its own <code>Random</code> class</a> (that Marc-André himself <a href="http://redmine.ruby-lang.org/issues/show/3104" rel="noreferrer">helped to debug</a>, hence the <a href="http://redmine.ruby-lang.org/versions/show/11" rel="noreferrer">1.9.2 target</a> for that feature).</p> <p>For instance, in this <a href="http://www.eggheadcafe.com/software/aspnet/35817496/random-integer-within-a-r.aspx" rel="noreferrer">game where you need to guess 10 numbers</a>, you can initialize them with:</p> <pre><code>10.times.map{ 20 + Random.rand(11) } #=&gt; [26, 26, 22, 20, 30, 26, 23, 23, 25, 22] </code></pre> <p>Note: </p> <ul> <li><p>Using <code>Random.new.rand(20..30)</code> (using <code>Random.new</code>) generally would not be a good idea, as explained in detail (again) by <a href="https://stackoverflow.com/users/8279/marc-andre-lafortune">Marc-André Lafortune</a>, in <a href="https://stackoverflow.com/questions/198460/how-to-get-a-random-number-in-ruby/2773866#2773866">his answer</a> (again).</p></li> <li><p>But if you don't use <code>Random.new</code>, then the <a href="http://www.ruby-doc.org/core/classes/Random.src/M000693.html" rel="noreferrer">class method <code>rand</code></a> only takes a <code>max</code> value, not a <code>Range</code>, as <a href="https://stackoverflow.com/users/66725/banister">banister</a> (energetically) points out in the comment (and as documented in the <a href="http://www.ruby-doc.org/core/classes/Random.html" rel="noreferrer">docs for <code>Random</code></a>). Only the <a href="http://www.ruby-doc.org/core/classes/Random.src/M000688.html" rel="noreferrer">instance method</a> can take a <code>Range</code>, as illustrated by <a href="https://stackoverflow.com/questions/5722881/generate-a-random-number-with-7-digits/5723105#5723105">generate a random number with 7 digits</a>.</p></li> </ul> <p>This is why the equivalent of <code>Random.new.rand(20..30)</code> would be <code>20 + Random.rand(11)</code>, since <code>Random.rand(int)</code> returns “a random integer greater than or equal to zero and <em>less than the argument</em>.” <code>20..30</code> includes 30, I need to come up with a random number between 0 and 11, excluding 11.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload