Log in to save your progress.
<< Previous
Home
Note! The window is too narrow to use Pythonlab.dev. Use a device with a keyboard!

Reference

Click on a function to read more.
Basics part 1
Syntax
Short description
break
break interrupts code that is repeated
continue
continue interrupts a repetition and starts the next
elif
elif is a combination of else and if
else
else can be used last in an if-statement
float()
Converts to type float (decimal number)
if
if controls the code to do different things depending on a condition
input()
Receives input from the user
int()
Converts to type int (integer)
randint()
Generates a random integer
round()
Rounds a number
str()
Converts to type string
while
while is used to repeat code

Random Numbers

In this section you will learn to

How does a computer create random numbers?

When it comes to random numbers, we must distinguish between true random numbers and pseudorandom numbers.

According to the laws of physics, true random numbers exist in nature and can be measured. For some advanced applications, such as bank security, random numbers that are impossible to guess are needed. There is expensive hardware that claims to create true random numbers. True random numbers are, however, impossible to create in a regular computer.

In a regular computer, random numbers are created using a mathematical formula. To prevent it from generating the same sequence of numbers every time, a seed (English: seed) is used. The seed is usually taken from the system time, i.e., what time it is on the computer when the random number generator starts. These random numbers are called pseudorandom numbers; they are not truly random but good enough for most applications.

The Random Number Generator in Python

In the random module (Swedish: random), there are a number of functions for creating random numbers. We will use the randint function. This function creates random integers within a specified range. If you write randint(1,6), a random number will be created, which is at least one and at most six.

Example

How the randint function is used. Try running the program multiple times.

from random import randint # import the randint function from random
random_number = randint(1,6) # create a random number 1-6

Login or create an account to save your progress and your code.

What does the code do?

Read the code below and try to figure out what the program prints. Run the program after you answer and see if you were correct.

-- Output from your program will be here --

Question: What will the program print?

A number that is at least 0 and at most 101
A number that is at least: 1 and at most 100

Login or create an account to save your progress and your code.

Change the code

Create a variable random_number that gets a new random value 1-3 for each new loop. The idea of the program is to create 1000 random numbers with values 1-3 and then print how many we got of each.

-- Output from your program will be here --

Login or create an account to save your progress and your code.

Create: Sum of two dice rolls

Write code that prints the sum of two dice rolls with a regular die (only print the sum).

-- Output from your program will be here --

Random numbers between 0 and 1

There are other functions in the random module, and one of them is called random. The function thus has the same name as the module. The random function creates random numbers of type float between 0 and 1. This can, for example, be useful in connection with probabilities.

Example

There is about a 21% chance of winning something on a Triss lottery ticket. The code randomizes a value between 0 and 1 and simulates whether you won or not on a ticket.

Consider: does it matter much if we use p < 0.21 or p <= 0.21?

from random import random
p = random()
if p < 0.21:
  print('You won something!')
else:
  print('No win.')
Status
You have finished all tasks in this section!
🎉