Locked lesson.
About this lesson
How to create and use random numbers.
Exercise files
Download this lesson’s related exercise files.
Random Numbers.docx59.1 KB Random Numbers - Solution.docx
59.1 KB
Quick reference
Random Numbers
How to generate random numbers in Python.
When to use
Use this whenever you need to generate random numbers.
Instructions
To create a random number, import the random module:
import random
To create a random floating point number between 0 an 1:
print(random.random())
To create a floating point random number in a range from 1 to 100:
print(random.uniform(1,100))
To create a random integer between 1 and 100:
print(random.randint(1,100))
Hints & tips
- import random
- print(random.random())
- print(random.uniform(1,100))
- print(random.randint(1,100))
- 00:04 In this video I want to look at random numbers.
- 00:06 We've been doing some pretty complicated stuff, functions, and loops, and
- 00:10 things like that.
- 00:10 Now we're going to take a step back and do something a little bit easier and
- 00:13 just generate random numbers.
- 00:15 Now, there's lots of times when you're going to need to generate a random number,
- 00:18 very common sort of thing.
- 00:20 And so we're going to learn how to do that in this video.
- 00:22 Now, some programming languages make getting a random number easier than
- 00:26 others.
- 00:27 Ruby, for instance, is very simple to get a random number.
- 00:30 JavaScript a little bit harder.
- 00:31 Python falls into the a little bit harder camp.
- 00:34 It's kind of a little bit trickier to get a random number, but it's not that bad,
- 00:38 and we're going to see how to do it right now.
- 00:40 So, first off, we need to import a module in order to deal with the random numbers.
- 00:44 And you see up the top of our program, we've already imported this OS module.
- 00:48 And actually in the next video, we're going to talk about importing modules and
- 00:51 learn more about them.
- 00:53 But for right now, you can just know that to import a module you just type import,
- 00:57 and the module name.
- 00:58 In this case, we're going to import random.
- 01:00 It's the random module.
- 01:01 So now this will allow us to do different things with random numbers,
- 01:05 to generate them, to manipulate them, to do all kinds of cool stuff.
- 01:08 So first things first, how do we get a random number?
- 01:11 Well, there's a couple of different ways, depending on what you want.
- 01:14 And we'll start out with this sort of non-common thing.
- 01:18 You're probably not going to want this one all that often but we'll knock it out
- 01:22 first and then go to the more realistic forms of random numbers.
- 01:25 First off, let's just print out, and we're going to call the random class,
- 01:30 we're going to call the random function in the random class, and
- 01:34 we're going to pass it no parameters.
- 01:37 And let's just save this and see what happens here.
- 01:40 We get this big, huge floating point number, and
- 01:42 you'll notice if we run this program again, we get a completely different one.
- 01:47 So every time we run this, the random generator generates another random number.
- 01:51 And what this is, it's between zero and one, and it always will be.
- 01:55 So, that's definitely a random number, right.
- 01:58 But in the real world you're usually going to want an integer,
- 02:01 something more useable, right?
- 02:03 So let's take a look at this, and let's expand on this a little bit more.
- 02:06 Instead of just calling random and getting a floating point between zero and
- 02:11 one, let's call random.uniform.
- 02:14 And if we save this and run it, we get an error.
- 02:16 We have to pass it a parameter.
- 02:17 So we have to say what range we want our random number to be in.
- 02:21 So we can go between 1 and 100, let's say.
- 02:24 So now if we save this and run it, we get 31.0102, whatever 8 point whatever, we're
- 02:29 still getting a floating point number, which may or may not be what you want.
- 02:34 Now, there's two different ways to deal with this.
- 02:36 We learned earlier that we can change a floating point into a integer by
- 02:41 calling the int method.
- 02:43 So we could just wrap this whole thing in an int.
- 02:46 And if we run this we get 7, 9, 94, 87, and you see it's between 1 and 100.
- 02:52 So that's one way to do it.
- 02:54 A little bit sloppy, a little bit inelegant.
- 02:56 So instead of doing that, we could just get rid of this uniform and
- 02:59 instead call the randint function.
- 03:01 And again, we're going to pass in the parameters between 1 and 100.
- 03:05 Save this and run it, now we get a returned integer.
- 03:10 3, 4, 45, every time we run the program we get a different one obviously.
- 03:15 Now I'm not going to go in the math of how these random numbers are generated.
- 03:18 You can look that up if you're all that interested.
- 03:20 It's a whole seeding thing and big algorithm.
- 03:23 We don't need to go into that, but this is pretty much the easiest way to generate
- 03:27 just a regular random number, an integer between 1 and 100.
- 03:31 Or we can make it 90 and 100.
- 03:33 It doesn't matter what range you want, that'll work.
- 03:37 I mean, you could do something like this.
- 03:41 Get all kinds of weird numbers.
- 03:42 So those are random numbers, pretty simple, just remember we need
- 03:46 to import the random module, and then it's just a matter of printing this out.
- 03:50 Here we've printed this.
- 03:52 We could easily call my_random, and just slap this into a variable.
- 03:57 And then later on if we wanted to print(my_random),
- 04:01 we could do like that, as we've done other things in the past.
- 04:05 And most of the time honestly, you're going to do that,
- 04:07 you're going to slap this random number into a variable and
- 04:10 then you're going to do something with that variable later on.
- 04:12 So, anyway, those are random numbers.
- 04:14 In the next video we'll learn all about modules.
Lesson notes are only available for subscribers.