Locked lesson.
About this lesson
In this lesson, we'll show how to generate random numbers.
Exercise files
Download this lesson’s related exercise files.
11 - Random Numbers.docx57.1 KB 11 - Random Numbers SOLUTION.docx
55.6 KB
Quick reference
Random Numbers
Random Numbers are easy to generate using Python.
When to use
Do this whenever you need to generate dummy data.
Instructions
First, we need to import the random library into our program:
from random import random
from random import randint
To create a random number (decimal):
random()
To Create a random integer between 0 an 10:
randint(0,10)
Hints & tips
- from random import random
- random()
- from random import randint
- randint(0,10)
- 00:04 Okay, in this video I want to show you how to work with random numbers with Python.
- 00:07 And we're going to use this later on to generate dummy data,
- 00:11 fake numbers to populate our Excel spreadsheets.
- 00:15 when working on some projects.
- 00:16 So random numbers with Python are actually very, very easy.
- 00:20 We just have to import the random library from Python.
- 00:24 So we can do that up here, we can go from random, import random.
- 00:30 Now this will import the random library, which will generate random numbers, but
- 00:34 they're not really the kind of random numbers we want.
- 00:36 And you'll see what I mean right now.
- 00:38 So let's just create one and we do that by just calling the random function.
- 00:41 Right. So, if we wanted to print this
- 00:43 to the screen, we could wrap this whole thing in our print function like we've
- 00:46 done before, and I've saved this file as rand.pi.
- 00:49 So, go ahead and save this, head over to our terminal and let's run this file.
- 00:54 And when we do you see we get this random number.
- 00:56 Now this is a decimal number.
- 00:58 What we call a float 0.67, whatever.
- 01:02 If we we run this again, we get another random number.
- 01:05 But again, this is not a particularly useful number usually.
- 01:08 We usually want integers, whole numbers, right?
- 01:11 And, in order to use whole numbers, we need something else.
- 01:14 So we can go from random import randint.
- 01:20 And this will allow us to create random integers.
- 01:23 And like I said integers are just whole numbers like four, 27, 18, right?
- 01:29 So, to use this It's a little bit different.
- 01:32 Let's create a variable for this.
- 01:33 Let's call this my_rand.
- 01:36 And we can call randint.
- 01:38 And this is a function and now we need to pass in a range.
- 01:42 So, we need to tell Python where we want to grab our number from.
- 01:46 So, let's say from 0 to 100 or even just from 0 to 10.
- 01:51 So now, we can print out my_rand and
- 01:55 this will print out a number between 0 and 10.
- 01:59 It can be 0, it can be 10, it can be any number in between.
- 02:03 So, it might be 0, and might be 4, it might be 8, and it might be 10.
- 02:08 Right? So, okay, let's go ahead and
- 02:09 save this and run it.
- 02:11 And see what we got, and we get 5.
- 02:14 We run this again.
- 02:16 We get 4, we run this again.
- 02:17 We get 10.
- 02:18 So, it's really just that easy to generate random numbers with Python.
- 02:23 And this is a really useful sort of thing to know, not just for this Excel course,
- 02:27 but for any sort of Python programming.
- 02:29 So, now let's put this together with our for
- 02:31 loop that we learned in the last video.
- 02:33 And let's put a little spin on it, so let's create a for loop.
- 02:36 And I'm going to call for number in.
- 02:40 Now instead of looping through a list or
- 02:42 something we can look we can loop through just a number of times, a range of times,
- 02:46 and we can do that by calling the range function.
- 02:49 And then we can say, how many times do we want to loop through this, so
- 02:52 let's loop through this 10 times.
- 02:55 What do we want to do each time?
- 02:56 Well, let's just create another random number and let's comment this one out.
- 03:00 And then let's just print out that.
- 03:06 That random number.
- 03:07 So, every time we loop through here, and we're going to loop through here 10 times,
- 03:10 we're going to generate a random integer and then just print it out.
- 03:13 Let's go ahead and save this and run it.
- 03:15 And see what we got.
- 03:17 And we see 1, 6, 10, 8, 1, 1,
- 03:20 1 since it's random it could be a bunch of ones in a row, you never know.
- 03:26 So, let's run this again and we get a whole other list of random numbers.
- 03:31 Clear the screen and run it again.
- 03:34 Again, here's 0 because it can be 0, it can be 10.
- 03:36 It could be any number in between.
- 03:37 And it's very cool.
- 03:38 So, quick and easy way to generate as many random numbers as you want,
- 03:42 we could generate 1,000 of them, right?
- 03:45 So we could save this.
- 03:47 And if we run this, if we scroll up here we'll know,
- 03:51 we'll see, we've got so many random numbers.
- 03:56 And if you counted them all there would be 1,000.
- 03:58 We could change this to 10.
- 04:00 And we could say from 0 to 1,000 but our range of random numbers.
- 04:08 If we save this and run it.
- 04:13 We see we get bigger numbers.
- 04:15 So very, very cool.
- 04:16 We don't have to start at zero.
- 04:18 We could start at between100 and say 150.
- 04:25 We save this and run it.
- 04:28 You'll notice that all these numbers are between 100 and 150.
- 04:31 So, any kind of random numbers you want to generate in any sort of range,
- 04:34 you have complete control over this.
- 04:36 If you want something besides just integers and you just want decimals,
- 04:41 you can use the random that we used at the beginning of this video.
- 04:46 And, very cool.
- 04:47 So, that's how to create random numbers.
- 04:48 That's how to create range in our for loop.
- 04:51 That's going to be useful to us later on.
- 04:53 And that's all there is to it.
- 04:54 So I think that's all for the Python programming of this course.
- 04:57 In the next video, we're going to jump right into open PI Excel.
Lesson notes are only available for subscribers.