Este vídeo pertenece al curso Programmieren lernen mit Python de openHPI. ¿Quiere ver más?
An error occurred while loading the video player, or it takes a long time to initialize. You can try clearing your browser cache. Please try again later and contact the helpdesk if the problem persists.
Scroll to current position
- 00:00We'll move on to the next lesson.
- 00:02In this one, we will tell you what random numbers are and how you can create them in Python.
- 00:07Simon and Leonie have just finished found an old Parcheesi game they'd like to play.
- 00:14Unfortunately the game lacks the dice.
- 00:16Therefore they decided to write a small program, which randomly gives them a number between 1 and 6, thus imitating a dice.
- 00:23For this there is the library random, random is English and means translated randomly.
- 00:29This means that we do not know beforehand what result we will get, and this result can always be different.
- 00:36The library random provides among other things the functions random and randint ready.
- 00:41So in the first line of our code we say from random import random.
- 00:45The first random refers to the library and the second to the function in this library.
- 00:51In the second line we call the function randomly once, which then returns a random number between 0 and 1, which we can then spend.
- 01:01That is, as an output received such as the number 0.107.
- 01:06But the special thing about random numbers is now, that when the same program is run repeatedly we can get into other output, for example, like 0.63.
- 01:16But we actually wanted to imitate a cube, for this there is the method randint, which Selina just mentioned.
- 01:24So we can write again from random import randint.
- 01:30If we then call this method in the second line and, for example, give the two values 1 and 6, we get a number, a random number between these two values.
- 01:41So we now get whole numbers between the values, which we specify there and there are the two values, that we're showing off there, including.
- 01:49So, for example, I can get a five, if I call the whole thing up again.
- 01:54Now we have just simulated our cube, but how do we know if it's fair, that is, whether all the numbers are thrown approximately equally often.
- 02:04Next we want to test the whole thing briefly, by rolling the dice a total of 600 times.
- 02:09So we could run the program 600 times and always write down the number we rolled.
- 02:14But that could take quite a long time.
- 02:17Instead, we want to write a small program for it, that means we first have to import the function again and then create a dictionary number.
- 02:26We then want to store in this dictionary, how often which number was rolled.
- 02:31This means that we have as key the different numbers from 1 to 6, and as assigned values, how often this number was rolled.
- 02:40Since we did not roll the dice at the beginning, it is always 0.
- 02:43Since we want to roll the dice 600 times, we represent the whole thing via a for loop, which iterates over the values 0 to 599, thus specifying for i in range (600).
- 02:56Because if we remember, range has always iterated the value from 0 to the value we specify.
- 03:02In this for loop, we do that, what Lina just described.
- 03:07So now we generate ourselves again with randint a random number between 1 and 6, because we always want to throw a number between 1 and 6 and store them in the variable x.
- 03:18Now, if we have thrown a certain number, a number x, we increase the value in our dictionary for this number.
- 03:28So let's throw a 3, for example, the value behind the 3 is increased by 1.
- 03:34For example, it would then no longer be 3:0 at the very beginning, but 3:1 at the very beginning of the dictionary.
- 03:42If we've done this 600 times, gone through our for loop 600 times, we are finished and finally want to print our dictionary again.
- 03:52So here we have to consider again, that we put the print statement back on the far left, so we're not moving in anymore.
- 03:59So we are now receiving the most diverse values, how often the different eye values, eye numbers have been thrown and see that the cube was actually handled fairly.
- 04:09But we can use random numbers to imitate not only dice, but also the trees, for example, which you have already learned about in the lesson on functions, randomly colored.
- 04:20The whole thing could look something like this, that is, we now draw these five trees and each time we randomly choose a color and color him in that color.
- 04:31To do this, we must first define our function create_any_color.
- 04:36We want to randomly select one of the colors red, green and blue and set the fillcolor accordingly.
- 04:42This means that in line 5 we must first return a random number between 1 and 3, using the function randint.
- 04:50Then we check whether this random number = 1 and set the fillcolor to "red" in this case.
- 04:56If the random number is a 2, we set the fillcolor to "green ...and blue on a C.
- 05:02The whole thing happens in an if branch.
- 05:05So right now we are already creating any color, and now we still have to draw our tree.
- 05:11But we have already learned how to do this in the branching lesson.
- 05:14Roughly, the pen was lifted and placed in position, where we want to draw the tree, then the pencil was put down again, because now we want to see what we draw.
- 05:25And then the fill color, which was not set in this case and therefore was black, to paint the tree.
- 05:34The tree was then painted, which we can see in the following lines with various calls of forward, left and right.
- 05:41Now we add our method create_any_color there, and thus the fill color is no longer black, but for example we get as seen here, a green tree.
- 05:56Last but not least we can now also call our method, draw_tree, the now always different colored trees at the different positions x and y.
- 06:08So we learned in this lesson what random numbers are and that we can create them randomly using the library.
- 06:21We also got to know the two functions random and randint.
- 06:25random returns a random number between 0 and 1 and randint gets two values passed and throws back a random number between these two.
To enable the transcript, please select a language in the video player settings menu.
Sobre este vídeo
Wertebereich: Der Aufruf der Funktion random()
gibt eine Zahl im Bereich von 0.0 (inklusive) und 1.0 (exklusive) zurück. Anders formuliert: 0.0 <= random() < 1.0
.