When you generate a generate many random numbers, you'll expect to see a random distribution of numbers and some areas where the same number appears many times in a row. # Randomly generates a number in the range 1-10, including the end points: This means that the probability of getting any specific number when running random.randint(1, 10) is only 10% - since each of the numbers 1-10 are each 10% likely to show up. The random.randint function will always generate numbers with equal probability for each number within the range. This means that you might get 2 and then another 2 (just like it's possible to roll 2 twice in a row), but it's more likely that the second number you get will not be a 2. With the random library, this is possible: # Randomly generates a number in the range 1-6, including the end points:Ī new random number will be generated every time this code runs. For example, if I want to generate a number to simulate the roll of a six-sided die, I need to generate a number in the range 1-6 (including the endpoints 1 and 6). One of the primary ways we generate random numbers in Python is to generate a random integer (whole number) within a specified range. Import random Generating Random Numbers Using random.randint # Import the random library, allowing the use of functions that generate random numbers
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |