Python Tutorial: Generate Random Numbers and Data Using the random Module

///Python Tutorial: Generate Random Numbers and Data Using the random Module

Python Tutorial: Generate Random Numbers and Data Using the random Module

FavoriteLoadingAdd to favorites

In this Python Programming Tutorial, we will be learning how to generate random numbers and choose random data from lists using the random module. I personally use the random module pretty often in my tutorials to generate random data. This can also be used be games, simulations, and plenty of other useful tasks. Let’s get started.

The code from this video can be found at:

✅ Support My Channel Through Patreon:

✅ Become a Channel Member:

✅ One-Time Contribution Through PayPal:

✅ Cryptocurrency Donations:
Bitcoin Wallet – 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet – 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet – MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot

✅ Corey’s Public Amazon Wishlist

✅ Equipment I Use and Books I Recommend:

▶️ You Can Find Me On:
My Website –
My Second Channel –
Facebook –
Twitter –
Instagram –

#Python

source

By |2019-09-11T22:46:13+00:00September 11th, 2019|Python Video Tutorials|48 Comments

48 Comments

  1. Aditya Lokwani September 11, 2019 at 10:46 pm - Reply

    any idea on how to stop the numbers from repeating

  2. Zakir Hossain September 11, 2019 at 10:46 pm - Reply

    This is pretty cool! I was just searching for how to use random in Python. And probably this is THE BEST tutorial that exactly expressed what I want. Thanks a lot, Corey for this wonderful video!

  3. awarefn September 11, 2019 at 10:46 pm - Reply

    wow that's cool

  4. devilhunter1555 September 11, 2019 at 10:46 pm - Reply

    thank you for all the new concepts on random math functions, didn't know there was this much support for mathematical concepts

  5. Fireskull557 September 11, 2019 at 10:46 pm - Reply

    I’m making a blackjack game using this thank you for helping

  6. s baskaravishnu September 11, 2019 at 10:46 pm - Reply

    your videos are excellent. Your videos are very much useful to me, Many thanks for that. My warm regards to you.

  7. Endless Void Studios September 11, 2019 at 10:46 pm - Reply

    another awesome vid you should write a book how to program the
    Corey Schafer way best seller in the tech industry fo sure

  8. Kanwar Programmer September 11, 2019 at 10:46 pm - Reply

    import random

    value = random.randint(1, 6)

    print(value)

    its not working

  9. XxalienxX YT September 11, 2019 at 10:46 pm - Reply

    I've read about random functions in Python all the time but from your video the commands got burned in my brain. I learned ay more from this.

  10. justArandomname September 11, 2019 at 10:46 pm - Reply

    Corey how do i dm you i have a lot of questions and i need help

  11. Ran Zivkovic September 11, 2019 at 10:46 pm - Reply

    man, I am a starter programmer, of course, python, you teach and explain so well!!! my teacher is really bad and you help a lot !!!my school doesn't do a very good job of teaching programming so u are awesome

  12. Wave Fx Motion Design September 11, 2019 at 10:46 pm - Reply

    What idid s the difference between random.sample and random.choices ?

  13. kuls43 September 11, 2019 at 10:46 pm - Reply

    Thanks Cory. One question, won't it contain duplicate rows? Though the probability is very low but it does have any kind of check.

  14. Lucio Libanori September 11, 2019 at 10:46 pm - Reply

    you type fast lol

  15. Avian Gamer September 11, 2019 at 10:46 pm - Reply

    It seems like "import random" isn't working for me, here is a screen shot if someone could help me thanks. http://prntscr.com/ni02kh

  16. Flutshy September 11, 2019 at 10:46 pm - Reply

    Could you help me with this, I've created a random dice game with the player getting 5 random dice values. How would i get the amount of times i rolled a 3 or 5 or 4 to display to the screen.

  17. Nikki sage September 11, 2019 at 10:46 pm - Reply

    how to generate infinite random training dataset?
    pls can you help?

  18. An Nghe September 11, 2019 at 10:46 pm - Reply

    I have the following Python script to write 256 bytes of random data from a list(sector) into a file name 1.bin

    I expect to write the hex values from the list(sector) into the file 1.bin

    However when I dump the file 1.bin (hex dump) I see the numbers in the file are all difference from expected values of the list(sector).

    I am appreciated if anyone can show me how to fix the error.

    import random

    import pickle

    sector = list(range(0, 256))

    random.shuffle(sector) #sector has 256 bytes of random data

    f = open('1.bin', 'wb') #open file 1.bin to write

    pickle.dump(sector, f) #write sector to file

    f.close()

    Notice: I could use the struct library (as shown below) but the execution is very slow

    import struct

    from random import getrandbits

    f = open(r'e:/1.bin', 'wb')

    file_size = 1048576000 # 1Gbyte data file

    i = 0

    while (i<file_size):

    random_num = getrandbits(8) # 1 byte (8 bits) random integer data (from 0 to 255)

    entry = struct.pack('<B', random_num) # convert 1 byte integer to hex

    f.write(entry) # write hex to file

    i += 1

    f.close()

  19. Daniel Azulay _ September 11, 2019 at 10:46 pm - Reply

    You have the best tutorials ever, I mean you are making everything much easier and explaining concepts deeply and correctly, most of the tutorials out there are not doing that, thanks!!

  20. Sahil King September 11, 2019 at 10:46 pm - Reply

    This is absolutely awesome! 10 days of work summarised here in less tham 15 mins! Thanks man!
    I have a ques though!
    A function “generateKey()” that returns a randomly generated string of 26 characters containing all the letters of the alphabet once and only once in python

    Same concept as above for the string?

  21. Germán Chrystan September 11, 2019 at 10:46 pm - Reply

    What if you want to get all possible outcomes from the last operation? How would you Code It?

  22. amirreza mohammadi September 11, 2019 at 10:46 pm - Reply

    Thank you.

  23. Said Zbita September 11, 2019 at 10:46 pm - Reply

    If I need some help in my assignment or just to any question, should I just post it as comment or is it OK to send text message? Thank you

  24. Said Zbita September 11, 2019 at 10:46 pm - Reply

    Thank you so much for this awesome video, it gives me more idea about the random module. thank you again.

  25. John Doe September 11, 2019 at 10:46 pm - Reply

    How did you get f strings to work in Sublime? I could not and asked about it on the forum. Thanks.

  26. Jeffaldoo September 11, 2019 at 10:46 pm - Reply

    A lot of this is outdated.

  27. Diego Castañeda September 11, 2019 at 10:46 pm - Reply

    Awesome tutorial! a quick question,when i do : print(random.choice(try_again)),sometimes i get the same try-again error message,i have 5 different messages but sometimes it will print the same message twice,how can i avoid printing the same message twice continuosly?

  28. brendley ohmua September 11, 2019 at 10:46 pm - Reply

    truly amazing, appreciate it very much.

  29. Chrish-Chan September 11, 2019 at 10:46 pm - Reply

    so boring. Do something interesting

  30. Dhirendra Kumar September 11, 2019 at 10:46 pm - Reply

    thank u sir ,for such a amazing video

  31. Cameron C September 11, 2019 at 10:46 pm - Reply

    Are any of the numbers in the uniform method innclusive/non inclusive as well like the random.random method does?

  32. Samuel K September 11, 2019 at 10:46 pm - Reply

    I used this to make rock, paper, scissors. lol

  33. TheTripleDeuce September 11, 2019 at 10:46 pm - Reply

    so heres a question how could i choose a random list between 3 predefined lists, like i have a list of songs, a list of movies and a list of tv shows, i want it to choose one list and then output a title from the winning list

    p.s: very informative BTW saved me so much of a headache working with random already lol

  34. krish jain September 11, 2019 at 10:46 pm - Reply

    Thank you so much. I needed this for a geussing game i am working on.

  35. Ali khan September 11, 2019 at 10:46 pm - Reply

    nice and amazing video how can we generate public key master secret key and private key by using random number generator?? i really appreciated you to do a separate video for this special topic thanks you….

  36. Yash Solanki September 11, 2019 at 10:46 pm - Reply

    random.choices is not working , i'm using python 2 version. how i can run this ?

  37. Dev Parkk September 11, 2019 at 10:46 pm - Reply

    Thanks a lot…this video was really helpful…

  38. kamal kumar September 11, 2019 at 10:46 pm - Reply

    subscribed after seeing this video. U are simply " The Best"

  39. Moaz El-sawaf September 11, 2019 at 10:46 pm - Reply

    Thanks a lot sir ❤

  40. LEARN! SHARE! and GROW! September 11, 2019 at 10:46 pm - Reply

    Thank you!. Good explanation.

  41. Anthony September 11, 2019 at 10:46 pm - Reply

    Thank you!! I'm teaching myself to code and I've been searching everywhere for the 'random.choice' function!! 😂 I can now receive a value from a list at random!

  42. Dylan Maulucci September 11, 2019 at 10:46 pm - Reply

    Great tutorial. However, can I generate a negative integer? for example: x = random.randint(0,-10) When I try I get a ValueError

  43. Mengzhen Chen September 11, 2019 at 10:46 pm - Reply

    Thank you!

  44. Tor Ivan Boine September 11, 2019 at 10:46 pm - Reply

    awesome. that "for num in range …" section helped me with the problem I had.

  45. serwa September 11, 2019 at 10:46 pm - Reply

    nice video, which text editor or IDE do you use?

  46. Shruti Shah September 11, 2019 at 10:46 pm - Reply

    also tell about how random.seed works

  47. Shruti Shah September 11, 2019 at 10:46 pm - Reply

    hey how do you run program in sublimetext ? whats that console beneath your code how to do this in sublime text?

  48. Mariam Ibrahim September 11, 2019 at 10:46 pm - Reply

    Thanks a lot, this tutorial is so clear and helpful

Leave A Comment

*