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

Поділитися
Вставка
  • Опубліковано 7 лют 2025
  • 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:
    github.com/Cor...
    ✅ Support My Channel Through Patreon:
    / coreyms
    ✅ Become a Channel Member:
    / @coreyms
    ✅ One-Time Contribution Through PayPal:
    goo.gl/649HFY
    ✅ Cryptocurrency Donations:
    Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
    Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
    Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot
    ✅ Corey's Public Amazon Wishlist
    a.co/inIyro1
    ✅ Equipment I Use and Books I Recommend:
    www.amazon.com...
    ▶️ You Can Find Me On:
    My Website - coreyms.com/
    My Second Channel - / coreymschafer
    Facebook - / coreymschafer
    Twitter - / coreymschafer
    Instagram - / coreymschafer
    #Python

КОМЕНТАРІ • 426

  • @LindsaySummers124
    @LindsaySummers124 6 років тому +367

    I just learned more in 10 minutes than I have in 10 days trying to read about this on my own. Thanks! =)

    • @coreyms
      @coreyms  6 років тому +19

      Good to hear! Glad you found it helpful

    • @coreyms
      @coreyms  6 років тому +19

      BTW, is that Cooper's Rock in your profile picture? I used to live in Morgantown, WV and it looks very familiar

    • @mrshopd7663
      @mrshopd7663 5 років тому +1

      +

    • @oddnumber8149
      @oddnumber8149 4 роки тому +2

      maybe you have dyslexia..people with dyslexia learn easily throw videos

    • @LindsaySummers124
      @LindsaySummers124 4 роки тому +9

      @@coreyms haha yes! It sure is =)

  • @MrWumboloni
    @MrWumboloni 2 роки тому +14

    It's been 5 years after you've made this tutorial and your work has still managed to help someone learn. Thank you Corey

  • @MyTube4Utoo
    @MyTube4Utoo 7 років тому +107

    Amazing how Corey can teach the same concepts, but his tutorials are head and shoulders above all others!

  • @AlexJaeger716
    @AlexJaeger716 4 роки тому +7

    every time I want to learn the important parts of a module without having to read its documentation, I come straight to your page. You do an incredible job Corey.. Thank you!

  • @samfiske6673
    @samfiske6673 6 років тому +26

    Told me exactly what I needed to know, saved me from any more searching through forums. Thank you!

  • @RanZivkovic
    @RanZivkovic 5 років тому +26

    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

  • @tonisun4785
    @tonisun4785 3 роки тому +2

    import random as rd
    1:00 random float number between excl. (0 and 1) --> rd.random()
    1:55 random float number berween excl.(1 and 10) --> rd.uniform(1, 10)
    2:30 random int number between incl. [1 and 10] --> rd.randint(1, 10)
    3:30 random list element between incl. [1 and 10] --> rd.choice(list)
    4:20 random list elements-list between incl. [1 and 10] --> rd.choices(list, k=100)
    5:40 rd.choices(list, weights=[8.0987,0,10,2,7,5.7,10,20,25,5.2], k=100)

  • @ibiamapexcel
    @ibiamapexcel 4 роки тому +1

    Honestly, stumbling into your tutorials has been a great blessing to me. Your wealth of knowledge shows a versatility of experiences that is far advanced. I have learnt alot from savouring your tutorials and am really grateful for all you do.
    Thanks a lot sir.
    Kind regards from Nigeria

  • @danielazulay_9158
    @danielazulay_9158 5 років тому +8

    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!!

  • @neolithian
    @neolithian 5 років тому +13

    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!

  • @sarahalhambra2431
    @sarahalhambra2431 2 роки тому

    I'm here in 2022 and this was so helpful. The explanation is clear and precise, no fluff. Thank you.

  • @tj9382
    @tj9382 2 роки тому +3

    Corey you are so good at teaching this stuff. I love the way you deal with each particular topic, it makes it much easier to understand. So many videos seem to jump from one topic to another. I know this stuff isn’t easy to teach but you make easier. Thanks man.

    • @easydatascience2508
      @easydatascience2508 2 роки тому

      see mine too. A full Python crash course tutorials step by step, sequentially, with all the source files downloadble too.

  • @vaibhavkhobragade9773
    @vaibhavkhobragade9773 5 років тому +6

    You are my favorite python tutor and I always recommend to people your channel. Because you explain the very easiest and efficient way than spending too much time reading python documentation. Thank you @corey.

  • @7daedalus7
    @7daedalus7 4 роки тому +3

    I've been struggling my way through some classes at my community college and your videos have saved my bacon on more than one occasion. Thank you for the clear and concise content. Keep it up!

  • @AI_waaala
    @AI_waaala 5 років тому +2

    Learnt more about Python more from ur videos than entire college year

  • @devilhunter1555
    @devilhunter1555 5 років тому +1

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

  • @indrasiswanto1161
    @indrasiswanto1161 7 років тому

    There are lot people are excellent at programming languages, and thanks to them for willingness to teach others. However, there are only an handful that could explain and teach others. You are small number of the later category. Thank you.

  • @c.michaelfisher1123
    @c.michaelfisher1123 7 років тому +5

    Holy cow! Thank you, Mr. Schafer. I was able to use this to understand how to generate a useful (and DRY!) function to handle skill rolls in my TBS Game!

  • @Primeaux182
    @Primeaux182 3 роки тому +2

    The random.choices part connected with the weights/probabilities is super helpful :) In another tutorial this command was not mentioned. I love it :)

  • @ayoadeolayiwola7195
    @ayoadeolayiwola7195 4 роки тому +2

    Thank you so much Corey for making time out to make all these available. You make learning python very smooth. I will definitely follow through all of these contents and practice and practice and practice. Thanks once again

  • @Broncx91
    @Broncx91 7 років тому +1

    Hey Corey,
    Thank you so much for this! I was working on a "random" number generator for a few days and was pulling aspects from multiple sources. I happened to stumble onto your video and realized that I needed to make the code into smaller pieces rather than larger ones. I was trying to have the random function that I was using do more things than it was designed to and thus kept getting an error of some sort. When I first wrote it, I couldn't get the program to loop and then if I had to roll higher than the numbers present it ran into an error due to population size so I had to find how replacement worked. Now it works. However, it has "none" twice when the program ends but that is minor. I believe it is because of my while loop and having to place return at the end in order for the loop to exit when I have do not have anymore rolls to do.
    Thank you again!

  • @binitagusain5222
    @binitagusain5222 4 роки тому +1

    This was Amazing!
    After seeing this video I was very excited to use this random function in my computer.
    Thanks for this useful information.

  • @mafrahamackie7074
    @mafrahamackie7074 3 роки тому +1

    After so many searches, I found yours.. Thank you so much! Really simple explanation 👌👌

  • @enfieldli9296
    @enfieldli9296 3 роки тому +1

    I can't say enough Thanks in every video you created on python and more!

  • @naseem5622
    @naseem5622 2 роки тому +1

    bro i learned more in 10 min than in 5 days.. thanks a lot

  • @evansoronje2351
    @evansoronje2351 4 роки тому +2

    Very awesome lesson on random numbers in under 14 minutes!!!

  • @kayaqueen6982
    @kayaqueen6982 2 роки тому

    wow this chanel is a real deal . i learned a lot from one 10 mins videos i can't believe this

  • @SHONNER
    @SHONNER 7 років тому +2

    Cool. Thanks for this one. I been dirtying-up the code a bit to make the mailing addresses look even more randomly typed out.

  • @lucascaceres3505
    @lucascaceres3505 3 роки тому

    With your videos! I learn python and practice my english too! You are awesome ️man!

  • @TheMRTIMBUK2
    @TheMRTIMBUK2 6 років тому +3

    Never seen such an amazing teacher ever in my whole life. God Bless you .

  • @soumyazyx
    @soumyazyx 5 років тому +2

    Thank you Corey for your time and effort. Keep spreading the knowledge.

  • @pardhasaradhivasamsetty7693
    @pardhasaradhivasamsetty7693 3 роки тому

    It is the one of the best tutorial that I found and best recommended for beginners. Appreicate the efforts and God bless

  • @senpaiofursensie3675
    @senpaiofursensie3675 3 роки тому

    U solved the error i spent a week on in one minute true legend

  • @apollosuggs7836
    @apollosuggs7836 5 років тому +3

    This really really helped me out a lot, thanks so much for this tutorial! I'm a FAN! You rock! 💯💯💯💯

  • @GLDN06
    @GLDN06 4 роки тому

    your videos help me a lot during CS.s Exam

  • @cptmand
    @cptmand 2 роки тому +1

    This is such a good channel. Really well done.

  • @learner7273
    @learner7273 2 роки тому

    Thank you, Corey. This is simple and straight to the point.

  • @cyberbooth1585
    @cyberbooth1585 4 роки тому +1

    Great video with lots of informations. Thank you Corey.

  • @reemabdella4443
    @reemabdella4443 Рік тому +1

    You are doing really great thank you so much!

  • @ExcelTutorials1
    @ExcelTutorials1 Рік тому

    Dude, this is awesome. Very helpful and well explained! Thank you!!!

  • @dev4life
    @dev4life 6 років тому

    Big bro, your teaching style is just awesome, after Bucky from the new Boston, you are my favorite programming teacher... God bless you

  • @sofianenm6852
    @sofianenm6852 5 років тому +1

    Thank you, best course of python I have ever found

  • @halobro27
    @halobro27 3 роки тому +1

    This was incredibly helpful and very simple. Thank you!!

  • @fredrickadhinga1609
    @fredrickadhinga1609 5 років тому +5

    I loved this man...can you do another one explaining how to link the random data to a user's input to consider a lucky winner.

  • @s.baskaravishnu22
    @s.baskaravishnu22 5 років тому +1

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

  • @zigirsbiz910
    @zigirsbiz910 3 роки тому

    Super fast! Understand everything! No bullshit around
    Thanks!

  • @iabhishek1984
    @iabhishek1984 4 роки тому +1

    Great teaching Corey!

  • @akashnaveen7475
    @akashnaveen7475 3 роки тому

    amazing video brro you just clear all my doubts soo easily.......thank you sooo much

  • @bratwurst8931
    @bratwurst8931 5 років тому +3

    this was exactly what I was looking for, thanks!

  • @HypnosisBear
    @HypnosisBear 3 роки тому +1

    So cool 😎😎. Love this tutorial!
    And you earned a sub. 👍

  • @SelmanAy
    @SelmanAy 7 років тому +5

    Hellooo! , probably Im the first one and opened sublime text immediately to do what you are gonna teach :) ty from now ^^

  • @cochogod
    @cochogod Рік тому

    Very clear and straightforward, +1

  • @skeoals
    @skeoals Рік тому +1

    this was simply amazing. thank you!!

  • @vaibhavkrkm
    @vaibhavkrkm 4 роки тому +2

    Nice tutorial 👍🤗 u are my one of the most favorite teachers whom I came across :)
    Anyways, I think we can leave range without list function also right, cuz it already returns a list of the given range..🤔
    Love from India :)

  • @choporo2005
    @choporo2005 4 роки тому +1

    Great Video! You helped me a lot. Thanks👍

  • @IgorPicoli
    @IgorPicoli 3 роки тому

    Very good! Thanks for that

  • @eddw123
    @eddw123 3 роки тому

    Awesome 😎 just straight forward to the point 👍👍💯 Now, I am loyal subscriber to your channel✅

  • @jongcheulkim7284
    @jongcheulkim7284 2 роки тому

    This is awesome!! Thank you so much!!

  • @Mariam-gd4to
    @Mariam-gd4to 6 років тому +2

    Thanks a lot, this tutorial is so clear and helpful

  • @shuklanikhil62
    @shuklanikhil62 2 роки тому

    Nice & Easy Concepts ,Thanks for the informative video

  • @lavo8404
    @lavo8404 3 роки тому +1

    Omg thanks. I have learnt thanks to you

  • @saidzbita6073
    @saidzbita6073 5 років тому +2

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

  • @GHOST-qg1em
    @GHOST-qg1em 5 років тому +2

    I learned a lot in this video, thank you so much for this video. :)

  • @TFB2K-baubau
    @TFB2K-baubau Рік тому

    that actually helped a lot man.
    thanks

  • @ZdenoMucina
    @ZdenoMucina 5 років тому +1

    very clear explanation :) thank you :)

  • @nadeemsuleman4326
    @nadeemsuleman4326 4 роки тому

    Thank you for enabling to generate data for practicing

  • @kennethha8173
    @kennethha8173 3 роки тому +1

    Omg , yes I finally found what I was looking for , thanks a lot

  • @smitpatel1358
    @smitpatel1358 3 роки тому

    You rock, Corey! I wish I found you earlier. That's one more amazing explanation.

  • @MrSeinfeldFan2
    @MrSeinfeldFan2 4 роки тому

    Oh boy, I needed this video! THANKS.

  • @teslan2009
    @teslan2009 4 роки тому

    very precise and super insightful

  • @edwinaldrin6820
    @edwinaldrin6820 3 роки тому

    good one , easily understood
    thanks man

  • @MemoThePanda
    @MemoThePanda 2 роки тому

    Goddamm, that was a lot of info and I understood it all.
    Mighty impressed, and I'm usely super slow to understanding this kind of stuff

  • @Mister___HACKER
    @Mister___HACKER Рік тому

    Thanks bro it helped me lot as a beginner

  • @foxygaming557
    @foxygaming557 5 років тому +2

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

  • @garimajain474
    @garimajain474 Рік тому

    No words to say thankuu... Awesome content🙇🙇🙇🙇🙇🙇🙇🙇🙇🙇🙇🙇🙇🙇

  • @MarkoBrainz
    @MarkoBrainz 4 роки тому +1

    Thank you very much, sir! You helped me a lot! You gave me a few steps to my dream to be a programmer :0

  • @xxalienxxyt9337
    @xxalienxxyt9337 5 років тому +1

    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.

  • @Azj6dx
    @Azj6dx 6 років тому +2

    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!

  • @gameboardgames
    @gameboardgames 2 роки тому

    This was ribeye quality, thanks!

  • @balintmucsanyi6322
    @balintmucsanyi6322 4 роки тому

    great video, happy that I' ve found your channel

  • @tymothylim6550
    @tymothylim6550 3 роки тому

    Thank you very much for this video :) It was very helpful!

  • @koodauskanava9096
    @koodauskanava9096 7 років тому +1

    Thanks, very useful information. Really like the weight parameter and sample method, didn't knew it before :)

  • @caydanphoenix9067
    @caydanphoenix9067 4 роки тому

    Example🔥...loved this idea🔥

  • @Potato-ff9ms
    @Potato-ff9ms 3 роки тому

    you can get integers in the uniform method by placing and "int(random.uniform(1, 10))"

  • @erfanahmadi2569
    @erfanahmadi2569 4 роки тому

    I really needed this. Thank you so much

  • @raghavnegi713
    @raghavnegi713 4 роки тому

    Thanks a lot. This help me fixed the bug in my game.

  • @susmitamazumder8390
    @susmitamazumder8390 5 років тому

    I have learned a lot from your video. you are amazing. you are simly the best.and you have really nice voice. Its so cool!!!!

  • @aaliyahjardien
    @aaliyahjardien 2 роки тому

    This was very useful, thank you 😊

  • @Endlessvoidsutidos
    @Endlessvoidsutidos 5 років тому +2

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

  • @conqueror346
    @conqueror346 2 роки тому +1

    Nice explanation 👍👍

  • @bam.polyglot
    @bam.polyglot 4 роки тому +1

    I got to say: Thank you a lot! You got one more subscriber too.

    • @xfire3778
      @xfire3778 4 роки тому

      Do you know what a subscript is

    • @bam.polyglot
      @bam.polyglot 4 роки тому

      @@xfire3778Oh, I am not a native speaker LOL I think it should be subscriber, right?

    • @xfire3778
      @xfire3778 4 роки тому

      Elber Almeida yeah 👍

  • @vijayg3248
    @vijayg3248 Рік тому

    Excellent work keep on work like this broo❤😊

  • @Potato-ff9ms
    @Potato-ff9ms 3 роки тому

    to get better list you can do:
    list {
    'random1',
    "ramdon2,"
    }
    still love this video, so ill probably sub

  • @RDhiru001
    @RDhiru001 6 років тому +3

    thank u sir ,for such a amazing video

  • @eveningr6179
    @eveningr6179 3 роки тому

    thank you so much, i really need this to make my application, Hacking Simulation

  • @avikahimalyan1280
    @avikahimalyan1280 4 роки тому

    Thanks bro only your trick is helpful
    Insane 🔥🔥🔥🔥🔥
    😎😎😎😎

  • @TheZeroDay
    @TheZeroDay 4 роки тому

    dang it! Some Useful content finally. Thanks

  • @77sbea
    @77sbea 2 роки тому

    It was a great video. Thank you very much!!

  • @MrSunshanmu
    @MrSunshanmu 2 роки тому +1

    Hi Corey, amazing vid. I'm completely new to python and I'm not a coder but was able to follow along and even try things on my own. Thank you.
    I need your help. I'm trying to create a number generator in a sort of unconventional way. So after watching this video I came up with 2 approaches and I'm getting sort of the results that I want, but it's incomplete and I don't know where to go next.
    Approach 1
    col1 = [1,2,3,4,5,7,8,10,13,16]
    col2 = [2,4,5,6,7,9,11,13,14,15,17,18,19,20,21,22]
    col3 = [7,8,10,12,13,14,15,16,17,18,19,23,24,25,27,29,30]
    col4 = [9,12,17,18,19,20,22,23,24,25,26,28,30,31,33]
    col5 = [20,21,24,25,26,28,30,32,33,34,35]
    b1 = [1,3,5,7,8]
    b2 = [2,4,6,7,9,10,11,12]
    drum1 = random.sample(col1 + col2 + col3 + col4 + col5, k=5)
    drum2 = random.sample(b1 + b2, k=2)
    draw1 = random.sample(drum1, k=5)
    draw2 = random.sample(drum2, k=2)
    main_draw = draw1 + draw2
    main_draw
    [30, 13, 28, 5, 30, 2, 4]
    Approach 2
    for num in range(4):
    first = random.choice(col1)
    second = random.choice(col2)
    third = random.choice(col3)
    forth = random.choice(col4)
    fifth = random.choice(col5)
    bonus1 = random.choice(b1)
    bonus2 = random.choice(b2)
    draw = f'{first} {second} {third} {forth} {fifth} {bonus1} {bonus2}'
    print(draw)
    7 6 7 23 21 5 2
    13 11 14 20 30 8 9
    4 6 23 17 20 5 11
    5 22 23 23 20 7 11
    In one I used the .choice and the other .sample.
    1. My first goal is to get unique random numbers from the range of numbers I manually enter and not from say, random(1,30). I would like to give it the numbers I want it to rand between. But I want each number to be unique and as you can see from the results, that's not the case.
    2. My second goal is to develop this further so that from the numbers I provide in each column as you noticed, some of the numbers in different columns are the same, I want it that way, however, I would also like it to count up, so if it chose 7 from col1 then it should start from a number greater than 7 in col2 and if it is, say 10, then it should choose one of the numbers in col3 that is greater than 10 and so on until col5.
    Can you please help me?
    Thank you

  • @presaid57
    @presaid57 6 років тому +1

    Amazing thanks very much
    You get all the respect bro
    ❤️❤️❤️❤️