My Secret Life as a Spaghetti Coder
home | about | contact | privacy statement
Update: Ruby 1.9.3 adds the ability to use ranges as arguments to rand, which produces more obvious code. So if you're using it, instead of using "magic offsets" like I did in the original post (as Joni Orponen mentions in the comments below), it would be better to use rand(1..6) to simulate a die roll.

So to summarize: if you need a percentage between 0 and 1, just call rand. If you need an integer between 0 and x (not including x), you can still call rand(x). Finally, if you need a number in a specific range, just call rand(x..y) where x is the lower bound of the range, and y is the higher end.

(And recall that if you want a non-inclusive range, you can use 3 periods, like rand(1...100) to get numbers from 1 to 99. Although if you're typing the number out, it's certainly better to use 1..99, if you had used a variable in the higher part of the range, the 3rd period is preferable to 1..(x-1), in my opinion.)

The original post follows:

Another quick note today... I surprisingly have yet to need a random number in Ruby up to this point (or forgot if I did), so I went through a little hassle trying to find out how. Turns out, you can simply use rand(int). So, if you needed a random integer to simulate a roll of a six-sided die, you'd use: 1 + rand(6). A roll in craps could be simulated with 2 + rand(6) + rand(6).

Finally, if you just need a random float, just call rand with no arguments. After that, you can modify the range as you normally would in anything else (i.e., using addition and multiplication).

I guess I used some crappy search terms, because I couldn't find this via google, and I didn't see it skimming the usual suspects in the Ruby docs.

Hey! Why don't you make your life easier and subscribe to the full post or short blurb RSS feed? I'm so confident you'll love my smelly pasta plate wisdom that I'm offering a no-strings-attached, lifetime money back guarantee!

Leave a comment

Things are always too easy in Ruby. :)

Posted by Brian Jones on Jul 03, 2007 at 09:40 PM UTC - 6 hrs

See also

Posted by Sy Ali on Aug 18, 2007 at 11:28 PM UTC - 6 hrs

Thanks Sy. That link looks cool, and goes into a lot more depth than here.

Posted by Sammy Larbi on Aug 19, 2007 at 10:47 AM UTC - 6 hrs

If +number+ is greater than the size of the array, the method will simply return the array itself sorted randomly

sort_by{ rand }.slice(0...number)

Posted by ukrainian ruby club on Oct 04, 2007 at 02:05 PM UTC - 6 hrs

Wouldn't 1 + rand(6) give an integer in {1,2,3,4,5,6,7}, since rand(6) could potentially be 6 and 1+6=7

Posted by Lynn Taylor on Nov 06, 2009 at 06:58 PM UTC - 6 hrs

Good question Lynn. According to the documentation:

rand "Converts max to an integer using max1 = max.to_i.abs. If the result is zero, returns a pseudorandom floating point number greater than or equal to 0.0 and less than 1.0. Otherwise, returns a pseudorandom integer greater than or equal to zero and less than max1"

So basically, it will find an integer in the range [0,6) - it includes 0 but will be strictly less than 6.

Posted by Sammy Larbi on Nov 07, 2009 at 10:14 AM UTC - 6 hrs

"I guess I used some crappy search terms"

me too, searching "ruby dice" leads to nirvana :)

Posted by skob on Jan 25, 2010 at 07:57 AM UTC - 6 hrs

Thanks, dude.
I used this on a RGSS for a RPG Maker game.

Posted by Shin on Feb 19, 2010 at 10:12 PM UTC - 6 hrs

Guys ! ..... I did it from irb ...

irb(main):001:0> rand(6) + 1 .....

Succinct, Sweet and Sexy ! .....

Posted by Arkapravo on Feb 22, 2010 at 04:40 AM UTC - 6 hrs

BASIC had this from the 60s. What's so sweet and sexy about this? This should be standard fare on ALL programming languages, and if they don't, then they suck and people should be vociferous about it.

Programming is about solving problems on time and in budget -- I don't have time to keep searching tomes upon tomes upon tomes of (e-)books for solutions to otherwise easy problems.

Posted by munch on Jan 19, 2011 at 04:19 PM UTC - 6 hrs

Dice cannot roll zero. Real world analogies are nice and statistics distributions are hard to grasp. Go with safe practices instead of magic offsets.

rand(1..6) + rand(1..6)

Posted by Joni Orponen on Feb 14, 2012 at 10:22 AM UTC - 6 hrs

Good point Joni. However, back when this post was written, rand did not support ranges.

I do much prefer the range option though, because it makes it clear what is going on without having to think much about it.

Posted by Sammy Larbi on Feb 14, 2012 at 10:44 AM UTC - 6 hrs

That is most correct: ranges as arguments for rand() were only introduced in Ruby 1.9.3 [1].

The reason why I posted is the high search engine ranking [2] of this post - in fact you are the first hit at the time of posting this. Adding the information here is great community service, IMO.

On that note, since you do seem to be editing your posts over time, you could just edit the main post body to hint this new oppoturnity out to people.


Posted by Joni Orponen on Feb 15, 2012 at 03:02 AM UTC - 6 hrs

Good point, suggestion taken and the post has been updated.

Posted by Sammy Larbi on Feb 15, 2012 at 05:59 AM UTC - 6 hrs

Thanks, this post helped me to understand rand in Ruby! It still works on Ruby 2.2.2!

Posted by Marcos on Jul 12, 2015 at 06:15 AM UTC - 6 hrs

Thanks,forthis post /Please explain this
def random_vector(minmax)
return do |i|
rand_in_bounds(minmax[i][0], minmax[i][1])

Posted by yaser on Oct 28, 2015 at 07:03 AM UTC - 6 hrs

Check out for what happens when you use

Posted by Sammy Larbi on Oct 28, 2015 at 07:25 AM UTC - 6 hrs

Leave a comment

Leave this field empty
Your Name
Email (not displayed, more info?)


Subcribe to this comment thread
Remember my details

Picture of me

.NET (19)
AI/Machine Learning (14)
Answers To 100 Interview Questions (10)
Bioinformatics (2)
Business (1)
C and C++ (6)
cfrails (22)
ColdFusion (78)
Customer Relations (15)
Databases (3)
DRY (18)
DSLs (11)
Future Tech (5)
Games (5)
Groovy/Grails (8)
Hardware (1)
IDEs (9)
Java (38)
JavaScript (4)
Linux (2)
Lisp (1)
Mac OS (4)
Management (15)
MediaServerX (1)
Miscellany (76)
OOAD (37)
Productivity (11)
Programming (168)
Programming Quotables (9)
Rails (31)
Ruby (67)
Save Your Job (58)
scriptaGulous (4)
Software Development Process (23)
TDD (41)
TDDing xorblog (6)
Tools (5)
Web Development (8)
Windows (1)
With (1)
YAGNI (10)

Agile Manifesto & Principles
Principles Of OOD
Ruby on Rails

RSS 2.0: Full Post | Short Blurb
Subscribe by email:

Delivered by FeedBurner