ไม่สามารถเล่นวิดีโอนี้
ขออภัยในความไม่สะดวก

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

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 ส.ค. 2017
  • 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

ความคิดเห็น • 427

  • @LindsaySummers124
    @LindsaySummers124 5 ปีที่แล้ว +356

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

    • @coreyms
      @coreyms  5 ปีที่แล้ว +19

      Good to hear! Glad you found it helpful

    • @coreyms
      @coreyms  5 ปีที่แล้ว +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 4 ปีที่แล้ว +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 =)

  • @MyTube4Utoo
    @MyTube4Utoo 6 ปีที่แล้ว +106

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

  • @MrWumboloni
    @MrWumboloni 2 ปีที่แล้ว +9

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

  • @ranzivkovic290
    @ranzivkovic290 5 ปีที่แล้ว +25

    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

  • @AlexJaeger716
    @AlexJaeger716 3 ปีที่แล้ว +6

    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 5 ปีที่แล้ว +26

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

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

  • @tj9382
    @tj9382 ปีที่แล้ว +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 ปีที่แล้ว

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

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

  • @sarahalhambra2431
    @sarahalhambra2431 ปีที่แล้ว

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

  • @tonisun4785
    @tonisun4785 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)

  • @vaibhavkhobragade9773
    @vaibhavkhobragade9773 4 ปีที่แล้ว +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.

  • @indrasiswanto1161
    @indrasiswanto1161 6 ปีที่แล้ว

    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.

  • @AI_waaala
    @AI_waaala 4 ปีที่แล้ว +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

  • @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 :)

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

  • @Broncx91
    @Broncx91 6 ปีที่แล้ว +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!

  • @mafrahamackie7074
    @mafrahamackie7074 3 ปีที่แล้ว +1

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

  • @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.

  • @evansoronje2351
    @evansoronje2351 4 ปีที่แล้ว +2

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

  • @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

  • @lucascaceres3505
    @lucascaceres3505 2 ปีที่แล้ว

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

  • @fredrickadhinga1609
    @fredrickadhinga1609 4 ปีที่แล้ว +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.

  • @enfieldli9296
    @enfieldli9296 2 ปีที่แล้ว +1

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

  • @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.

  • @kayaqueen6982
    @kayaqueen6982 ปีที่แล้ว

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

  • @naseem5622
    @naseem5622 ปีที่แล้ว +1

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

  • @senpaiofursensie3675
    @senpaiofursensie3675 2 ปีที่แล้ว

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

  • @TheMRTIMBUK2
    @TheMRTIMBUK2 6 ปีที่แล้ว +3

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

  • @apollosuggs7836
    @apollosuggs7836 4 ปีที่แล้ว +3

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

  • @GladanDsouza
    @GladanDsouza 3 ปีที่แล้ว

    your videos help me a lot during CS.s Exam

  • @zigirsbiz910
    @zigirsbiz910 2 ปีที่แล้ว

    Super fast! Understand everything! No bullshit around
    Thanks!

  • @soumyazyx
    @soumyazyx 4 ปีที่แล้ว +2

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

  • @cochogod
    @cochogod ปีที่แล้ว

    Very clear and straightforward, +1

  • @cptmand
    @cptmand ปีที่แล้ว +1

    This is such a good channel. Really well done.

  • @iabhishek1984
    @iabhishek1984 4 ปีที่แล้ว +1

    Great teaching Corey!

  • @skeoals
    @skeoals ปีที่แล้ว +1

    this was simply amazing. thank you!!

  • @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

  • @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 :)

  • @cyberbooth1585
    @cyberbooth1585 4 ปีที่แล้ว +1

    Great video with lots of informations. Thank you Corey.

  • @learner7273
    @learner7273 ปีที่แล้ว

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

  • @halobro27
    @halobro27 2 ปีที่แล้ว +1

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

  • @eddw123
    @eddw123 2 ปีที่แล้ว

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

  • @bratwurst8931
    @bratwurst8931 4 ปีที่แล้ว +3

    this was exactly what I was looking for, thanks!

  • @IgorPicoli
    @IgorPicoli 3 ปีที่แล้ว

    Very good! Thanks for that

  • @teslan2009
    @teslan2009 3 ปีที่แล้ว

    very precise and super insightful

  • @HypnosisBear
    @HypnosisBear 3 ปีที่แล้ว +1

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

  • @sofianenm6852
    @sofianenm6852 4 ปีที่แล้ว +1

    Thank you, best course of python I have ever found

  • @choporo2005
    @choporo2005 4 ปีที่แล้ว +1

    Great Video! You helped me a lot. Thanks👍

  • @reemabdella4443
    @reemabdella4443 11 หลายเดือนก่อน +1

    You are doing really great thank you so much!

  • @edwinaldrin6820
    @edwinaldrin6820 3 ปีที่แล้ว

    good one , easily understood
    thanks man

  • @lavo8404
    @lavo8404 3 ปีที่แล้ว +1

    Omg thanks. I have learnt thanks to you

  • @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.

  • @nadeemsuleman4326
    @nadeemsuleman4326 3 ปีที่แล้ว

    Thank you for enabling to generate data for practicing

  • @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 ^^

    • @beardslittlecorner121
      @beardslittlecorner121 7 ปีที่แล้ว +1

      Selman Ay, you could be first, but not alone v(=∩_∩=)フ

  • @ExcelTutorials1
    @ExcelTutorials1 ปีที่แล้ว

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

  • @shuklanikhil62
    @shuklanikhil62 ปีที่แล้ว

    Nice & Easy Concepts ,Thanks for the informative video

  • @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.

  • @TFB2024
    @TFB2024 8 หลายเดือนก่อน

    that actually helped a lot man.
    thanks

  • @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

  • @smitpatel1358
    @smitpatel1358 3 ปีที่แล้ว

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

  • @Potato-ff9ms
    @Potato-ff9ms 3 ปีที่แล้ว

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

  • @saidzbita6073
    @saidzbita6073 5 ปีที่แล้ว +2

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

  • @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

  • @GHOST-qg1em
    @GHOST-qg1em 4 ปีที่แล้ว +2

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

  • @foxygaming557
    @foxygaming557 5 ปีที่แล้ว +2

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

  • @Mariam-gd4to
    @Mariam-gd4to 5 ปีที่แล้ว +2

    Thanks a lot, this tutorial is so clear and helpful

  • @ZdenoMucina
    @ZdenoMucina 4 ปีที่แล้ว +1

    very clear explanation :) thank you :)

  • @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

  • @amirtaba8600
    @amirtaba8600 4 ปีที่แล้ว +1

    Thank you, This was exactly what I was looking for.

  • @bishalacharya2582
    @bishalacharya2582 3 ปีที่แล้ว

    thanks it really helped me i am making an voice assistant so i wanted to add open song feature but every time it just played same song at first but now its random .

  • @gameboardgames
    @gameboardgames 2 ปีที่แล้ว

    This was ribeye quality, thanks!

  • @conqueror346
    @conqueror346 ปีที่แล้ว +1

    Nice explanation 👍👍

  • @nanasuuakiaaperrotruco8209
    @nanasuuakiaaperrotruco8209 4 ปีที่แล้ว +1

    Hey im trying to generate a random number between 1 and 100000 and save in a txt file and the problem is that is an int, not a str. How do i convert it?

  • @Azj6dx
    @Azj6dx 5 ปีที่แล้ว +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!

  • @garimajain474
    @garimajain474 ปีที่แล้ว

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

  • @caydanphoenix9067
    @caydanphoenix9067 3 ปีที่แล้ว

    Example🔥...loved this idea🔥

  • @tymothylim6550
    @tymothylim6550 3 ปีที่แล้ว

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

  • @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

  • @kennethha8173
    @kennethha8173 3 ปีที่แล้ว +1

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

  • @aaliyahjardien
    @aaliyahjardien ปีที่แล้ว

    This was very useful, thank you 😊

  • @balintmucsanyi6322
    @balintmucsanyi6322 4 ปีที่แล้ว

    great video, happy that I' ve found your channel

  • @koodauskanava9096
    @koodauskanava9096 7 ปีที่แล้ว +1

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

  • @akashnaveen7475
    @akashnaveen7475 3 ปีที่แล้ว

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

  • @eveningr6179
    @eveningr6179 2 ปีที่แล้ว

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

  • @MrSeinfeldFan2
    @MrSeinfeldFan2 3 ปีที่แล้ว

    Oh boy, I needed this video! THANKS.

  • @vijayg3248
    @vijayg3248 ปีที่แล้ว

    Excellent work keep on work like this broo❤😊

  • @21leowheeler
    @21leowheeler 2 ปีที่แล้ว

    Im making a discord bot that is suppose to imitate a bowl you put rolled up pieces of paper into to pick at random from and throw away, after watching this video i feel halfway there, gonna look up a data base explanation video after which i feel i should be all set, thanks a bunch.

  • @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

  • @erfanahmadi2569
    @erfanahmadi2569 3 ปีที่แล้ว

    I really needed this. Thank you so much

  • @kevintad-y3010
    @kevintad-y3010 2 ปีที่แล้ว

    How do you code for a simulation of dice roll of two unique die and compare the outcome of those rolls (i.e. percentage that Die A won) in as little lines as possible? I watched the itertools video and this one to try to figure it out.

  • @johnbenstokes2100
    @johnbenstokes2100 5 ปีที่แล้ว +3

    thank u sir ,for such a amazing video

  • @avikahimalyan1280
    @avikahimalyan1280 3 ปีที่แล้ว

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

  • @Thewisesamurai
    @Thewisesamurai 2 ปีที่แล้ว

    Hey Corey, many thanks for the video, I wonder why for me the "K" for repetitions and "Weight" are not working

  • @ashirbadpaikaray5286
    @ashirbadpaikaray5286 2 ปีที่แล้ว

    Amazing video...But, I have a question for you...Is there a chance of any duplicate values( same output in 2 cases) in the list of 100 outputs in the last part of the video?

  • @raghavnegi713
    @raghavnegi713 3 ปีที่แล้ว

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