Locked lesson.
About this lesson
How to generate random numbers with Ruby.
Exercise files
Download this lesson’s related exercise files.
Random Numbers.docx59 KB Random Numbers - Solution.docx
59.1 KB
Quick reference
Random Numbers
Let's generate some random numbers!
When to use
Use this anytime you want to generate random numbers.
Instructions
To generate a random number in Ruby:
puts Random.rand(0..100)
Hints & tips
- puts Random.rand(0..100)
- 00:04 Okay, in this video I want to talk about random numbers.
- 00:06 And there's a lot of times when you're going to need to generate a random number
- 00:09 for a lot of different reasons.
- 00:11 And Ruby makes it really easy.
- 00:12 In the last couple of videos, we talked about methods and
- 00:15 we built our own methods.
- 00:16 Ruby has a lot of built in methods that you can use, and we've actually used some
- 00:20 of them throughout this course already, didn't even realize it.
- 00:23 But one of the nice methods to use is the random number method.
- 00:27 And it's just random.rand and then that.
- 00:31 Remember when we called our method in the last video we put these little parenthesis
- 00:36 in order to pass arguments.
- 00:37 Same thing, we're passing an argument into the random.rand method.
- 00:42 So if we puts this, save it and come back here and hit Reload,
- 00:47 we get this 0.64 blah, blah, blah.
- 00:50 If we run it again, we get another random number and
- 00:53 just keeps generating these crazy random numbers.
- 00:55 Well, this is obviously a float, it's obviously decimal.
- 00:59 I don't really need that kind of a random number I need like 4,
- 01:02 92, 6, I need an integer.
- 01:04 So how do we do that?
- 01:06 Well, we do that just by passing our parameters into these little parentheses
- 01:10 up here.
- 01:11 And remember when we're looking at I think, it was at four in the each loop.
- 01:14 We did this range of 0.., I think we went from zero to five.
- 01:18 Let's go zero to ten.
- 01:20 But we save this, now if we hit reload, we get 2, 0, 6, 4.
- 01:27 And we can pick any sort of range we want from 0 to 100.
- 01:31 We save this and our program 58, 55.
- 01:36 We don't have to start at 0 we can go from 100 to 399.
- 01:42 Doesn't matter.
- 01:43 105, 200, 303, 345.
- 01:48 And what it's doing is it's calling the random method.
- 01:51 It's just generating a random number between this range and
- 01:54 it's puts-ing it out onto the screen.
- 01:56 So we can do it just like that, we can puts our
- 01:59 random number out to the screen, which is useful.
- 02:01 We can also stuff this into a variable.
- 02:04 Let's call it my_random_number equals that so if we save this and run
- 02:09 it now nothing's going to happen because we've just we put it into our variable.
- 02:14 We haven't done anything with it.
- 02:16 So now we can go puts my_random_number.
- 02:20 If we save that and run it.
- 02:22 We get whatever our number is.
- 02:24 So that's cool we can add to it later on once it's in a variable.
- 02:30 That's the good thing about putting it in a variable,
- 02:32 we can do stuff to it later on.
- 02:34 Now we get 1386, 1361, 1390, 1239.
- 02:39 So, all kinds of goofy reasons you might want to play with random numbers.
- 02:43 Let's go names equals John,
- 02:50 Tim, and Mary.
- 02:54 So let's go put names, and
- 02:57 let's say we want a random.rand between 0 and
- 03:03 2, because our range is 0, 1, and 2.
- 03:08 Now, if we save this guy and hit Reload, we get Tim.
- 03:11 Actually, let's get rid of this.
- 03:13 Comment you out, John, Mary.
- 03:17 Every time I reload this, I get a new random number.
- 03:20 And I can use that as the index number for my array.
- 03:23 Just a goofy example, all kinds of ways you can use random numbers.
- 03:26 And in fact, going forward in the course, we're going to use random numbers for
- 03:28 some of the stuff that we're going to be doing.
- 03:30 So I thought it'd be a good idea to teach you how to use them now.
- 03:33 So that's all for random numbers.
- 03:35 In fact, that's all for the intermediate Ruby section.
- 03:37 In the next section, we're going to jump into advanced Ruby topics and
- 03:41 we're going to start learning about classes.
Lesson notes are only available for subscribers.