Python Random Number Generator: the Random Module  ||  Python Tutorial  ||  Learn Python Programming

Python Random Number Generator: the Random Module || Python Tutorial || Learn Python Programming



Views:131483|Rating:4.94|View Time:6:51Minutes|Likes:4524|Dislikes:53
To generate random numbers in Python, you use the Random Module. This contains functions for generating random numbers from both continuous and discrete distributions. In this video, we will cover the key random number generators.

➢➢➢➢➢➢➢➢➢➢
To learn Python, you can watch our playlist from the beginning:

➢➢➢➢➢➢➢➢➢➢
We recommend:
Python Cookbook, Third edition from O’Reilly

The Mythical Man Month – Essays on Software Engineering & Project Management

Shop Amazon Used Textbooks – Save up to 90%

➢➢➢➢➢➢➢➢➢➢
Subscribe to Socratica:

To support more videos from Socratica, visit
Socratica Patreon

Socratica Paypal

We also accept Bitcoin! 🙂
Our address is: 1EttYyGwJmpy9bLY2UcmEqMJuBfaZ1HdG9

➢➢➢➢➢➢➢➢➢➢
Python instructor: Ulka Simone Mohanty
Written & Produced by Michael Harrison
FX by Andriy Kostyuk

the world is a chaotic place from Heisenberg's uncertainty principle to the butterfly effect our lives are fraught with randomness Python understands this and comes equipped with a random module which gives you a wide variety of functions for generating random numbers this is useful if you want to add unpredictability to your games or if you want to run Monte Carlo simulations but the games begin we begin with a serious warning random numbers are used in cryptography but the functions in the random module were not built for such purposes in fact I thought explicitly warns you against doing so but for most other applications the random module is a fantastic tool to use the random module you must first import it to see the methods available print the directory for this module today we will explore many of these functions and give a wide variety of examples we will begin with a random function this function returns a random number in the interval 0 to 1 the square bracket means it can return 0 while the open parenthesis means it will never return 1 let's see some examples let's display ten random numbers from the interval 0 to 1 begin by importing the random module next use the range function to perform a loop with 10 iterations in each loop print a random number run those numbers do indeed look random what if we run this again 10 very different numbers when you run this you should get 10 different numbers than the ones displayed here that's exactly what you want from the random function another feature of the random function is that it represents the uniform distribution without getting too technical this just means the probabilities of numbers being chosen are equally spread out over the interval what if you want to generate a random number from a different interval for example how would you generate random numbers from the interval 3 to 7 there are several ways for starters you could write your own function to do so start by picking a random number in the interval 0 to 1 by using the random function next multiply by 4 so the random number is in the interval 0 to 4 we multiplied by 4 because that's the width of the interval 3 to 7 finally add 3 to shift the number to the target interval 3 to 7 let's write the function to do this import a random right the function by picking a random number then scaling and shifting let's print 10 random numbers using this function as you can see they all lie in the interval 3 to 7 but there's an easier way to generate random numbers from any interval use the uniform function this function is included in the random module so why did I bother writing a function that Python already gives you the reason is to highlight an important fact the random function can be used to build customized random number generators in fact it's the key ingredient for many of the functions in the random module let's test the uniform function like before we will pick 10 random numbers uniformly chosen between 3 & 7 run excellent the random and uniform functions are both uniform distributions but there are other distributions where some groups of numbers are more likely to be chosen than others the most widely encountered is the normal distribution also known as the bell curve a normal distribution is completely described by just two numbers the mean and the standard deviation the mean is the average it's where the bell curve Peaks the standard deviation describes how wide or narrow the curve is you use the Greek letter nu for the mean and the lowercase Sigma for the standard deviation to generate random numbers from a normal distribution you use the normal variate function when you call this function you must pass in both the mean and the standard deviation for example to generate 20 numbers from a bell curve with mean 0 and standard deviation 1 you call the normal variate function and pass in the two parameters notice how the numbers are bunched around the mean zero here are 20 numbers from a normal distribution with mean 0 and standard deviation 9 notice that these numbers are more widely spread out if you make the standard deviation small the random numbers will be tightly grouped if you change the mean to 5 you'll see that the random numbers are now bunched around 5 sometimes you don't want a random number chosen from an infinite number of possibilities for example what if you want to simulate the role of a six-sided die for this you use the R and int function this is an abbreviation for random integer if you want a random whole number between 1 and 6 call r + int with the smallest possible integer and then the largest possible integer now we're rolling there are even times when you want a random selection from a list of values which are not numbers for example suppose you are writing a program to play rock paper and scissors in this game there are three possible outcomes rock paper and scissors to pick a random value from this list use the choice function when you call the choice function pass in a list of values you want Python to choose from I still think that rock should be paper in the random module there is also a function called Socratic ax this function gives the probability someone will like our video or subscribe to our channel with your help we can improve these odds for your convenience YouTube provides a subscribe button somewhere around here and if we'd like to help accelerate our production of videos you can support us on patreon or contribute using Bitcoin thank you

43 Replies to “Python Random Number Generator: the Random Module || Python Tutorial || Learn Python Programming”

  1. Toni Kaiser

    Sehr hilfreiches Video vielen Dank dafür aber was sollte ich machen wenn ich einen zufälligen Wert aus einem Dictionary benutzen möchte?

  2. nnaji chidiebere

    This is the best tutorial i have ever seen. i really dont like commenting in comment sections but i thing i have to tell you that you are doing a very good job.THANK YOU, i really appreciate.

  3. Jerry Gundecker

    It's worth watching these until the end, if for no other reason but the punch line at the end.
    She delivers the lesson like a machine and then cuts you off at the ankles with a line like, "a fuzzy-wuzzy mess".

  4. Arvinda Kashan

    Hello there, I wish that something like a group of six numbers each could input only select range of number. There shouldn't any repeated numbers in any of six number and could generated possible combination. That could list out on work sheet with a number finder as well. They should print out on the lottery bet slip from any printer. I hope that you could help me. Thank you.

  5. Socratica

    It's official! The Socratica Python Kickstarter was a success! Thank you to all of our supporters. Because of you, many more Python videos coming soon!! 💜🦉

  6. Caoimhin Tew

    At this time, 46 hardcore C++ coders didn't like this video. Sucks to be them! Keep up the awesome work, it's helped me learn how to code better in a few months than I have in all time (QBASIC with line numbers, green screen, C128, 5.25" floppy disk)!

  7. No Name

    If i were to generate a random number without importing random module, how will I do it?
    Also how can I see the lines of code inside a module?

  8. CopperyMarrow15

    you liar…

    Traceback (most recent call last):

    File "<pyshell#22>", line 1, in <module>

    random.Socratica()

    AttributeError: 'module' object has no attribute 'Socratica'

  9. Leon Müller

    No shit i just wanted to know how to instantiate a randomstate in a Perceptron Forward LEarning Process not fucking watch a james bod movie xD. This is why people think that all programers are hackers

  10. gaurav raichandani

    I never really comment on videos but I really appreciate the way you are teaching Python and I just want to add that I honestly laughed a lot at "Now we're rolling…"

  11. Tigertyp

    I want my discord bot to send Z random numbers between X and Y but with a for loop he prints Z messages with only one number per messagee. Can i do something that he prints the Z amount of numbers in one message?

Leave a Reply

Your email address will not be published. Required fields are marked *