ไม่สามารถเล่นวิดีโอนี้
ขออภัยในความไม่สะดวก
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
I just learned more in 10 minutes than I have in 10 days trying to read about this on my own. Thanks! =)
Good to hear! Glad you found it helpful
BTW, is that Cooper's Rock in your profile picture? I used to live in Morgantown, WV and it looks very familiar
+
maybe you have dyslexia..people with dyslexia learn easily throw videos
@@coreyms haha yes! It sure is =)
Amazing how Corey can teach the same concepts, but his tutorials are head and shoulders above all others!
It's been 5 years after you've made this tutorial and your work has still managed to help someone learn. Thank you Corey
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
i agree
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!
Told me exactly what I needed to know, saved me from any more searching through forums. Thank you!
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
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!!
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!
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.
see mine too. A full Python crash course tutorials step by step, sequentially, with all the source files downloadble too.
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!
Holy Beautiful
I'm here in 2022 and this was so helpful. The explanation is clear and precise, no fluff. Thank you.
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)
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.
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.
Learnt more about Python more from ur videos than entire college year
thank you for all the new concepts on random math functions, didn't know there was this much support for mathematical concepts
The random.choices part connected with the weights/probabilities is super helpful :) In another tutorial this command was not mentioned. I love it :)
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!
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!
After so many searches, I found yours.. Thank you so much! Really simple explanation 👌👌
Cool. Thanks for this one. I been dirtying-up the code a bit to make the mailing addresses look even more randomly typed out.
Very awesome lesson on random numbers in under 14 minutes!!!
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
With your videos! I learn python and practice my english too! You are awesome ️man!
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.
I can't say enough Thanks in every video you created on python and more!
This was Amazing!
After seeing this video I was very excited to use this random function in my computer.
Thanks for this useful information.
wow this chanel is a real deal . i learned a lot from one 10 mins videos i can't believe this
bro i learned more in 10 min than in 5 days.. thanks a lot
U solved the error i spent a week on in one minute true legend
Never seen such an amazing teacher ever in my whole life. God Bless you .
This really really helped me out a lot, thanks so much for this tutorial! I'm a FAN! You rock! 💯💯💯💯
your videos help me a lot during CS.s Exam
Super fast! Understand everything! No bullshit around
Thanks!
Thank you Corey for your time and effort. Keep spreading the knowledge.
Very clear and straightforward, +1
This is such a good channel. Really well done.
Great teaching Corey!
this was simply amazing. thank you!!
It is the one of the best tutorial that I found and best recommended for beginners. Appreicate the efforts and God bless
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 :)
Great video with lots of informations. Thank you Corey.
Thank you, Corey. This is simple and straight to the point.
This was incredibly helpful and very simple. Thank you!!
Awesome 😎 just straight forward to the point 👍👍💯 Now, I am loyal subscriber to your channel✅
this was exactly what I was looking for, thanks!
Very good! Thanks for that
very precise and super insightful
So cool 😎😎. Love this tutorial!
And you earned a sub. 👍
Thank you, best course of python I have ever found
Great Video! You helped me a lot. Thanks👍
You are doing really great thank you so much!
good one , easily understood
thanks man
Omg thanks. I have learnt thanks to you
your videos are excellent. Your videos are very much useful to me, Many thanks for that. My warm regards to you.
Thank you for enabling to generate data for practicing
Hellooo! , probably Im the first one and opened sublime text immediately to do what you are gonna teach :) ty from now ^^
Selman Ay, you could be first, but not alone v(=∩_∩=)フ
Dude, this is awesome. Very helpful and well explained! Thank you!!!
Nice & Easy Concepts ,Thanks for the informative video
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.
that actually helped a lot man.
thanks
Big bro, your teaching style is just awesome, after Bucky from the new Boston, you are my favorite programming teacher... God bless you
You rock, Corey! I wish I found you earlier. That's one more amazing explanation.
you can get integers in the uniform method by placing and "int(random.uniform(1, 10))"
Thank you so much for this awesome video, it gives me more idea about the random module. thank you again.
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
Thanks bro it helped me lot as a beginner
I learned a lot in this video, thank you so much for this video. :)
I’m making a blackjack game using this thank you for helping
Thanks a lot, this tutorial is so clear and helpful
very clear explanation :) thank you :)
Thank you very much, sir! You helped me a lot! You gave me a few steps to my dream to be a programmer :0
Thank you, This was exactly what I was looking for.
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 .
This was ribeye quality, thanks!
Nice explanation 👍👍
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?
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!
No words to say thankuu... Awesome content🙇🙇🙇🙇🙇🙇🙇🙇🙇🙇🙇🙇🙇🙇
Example🔥...loved this idea🔥
Thank you very much for this video :) It was very helpful!
another awesome vid you should write a book how to program the
Corey Schafer way best seller in the tech industry fo sure
Omg , yes I finally found what I was looking for , thanks a lot
This was very useful, thank you 😊
great video, happy that I' ve found your channel
Thanks, very useful information. Really like the weight parameter and sample method, didn't knew it before :)
amazing video brro you just clear all my doubts soo easily.......thank you sooo much
thank you so much, i really need this to make my application, Hacking Simulation
Oh boy, I needed this video! THANKS.
Excellent work keep on work like this broo❤😊
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.
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
I really needed this. Thank you so much
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.
thank u sir ,for such a amazing video
Thanks bro only your trick is helpful
Insane 🔥🔥🔥🔥🔥
😎😎😎😎
Hey Corey, many thanks for the video, I wonder why for me the "K" for repetitions and "Weight" are not working
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?
Thanks a lot. This help me fixed the bug in my game.