Similar presentations:
Random, Randint и Randrange
1.
Random, Randint и Randrange2.
module RANDOMSuch a program (actually a module or function) is called a
pseudo-random number generator. The Python standard library
includes the random module. It contains a lot of functions
related to the emulation of randomness (for example,
"shuffling" the elements of a sequence), and not just functions
for generating pseudo-random numbers. This lesson will look
at the random(), randrange() and randint() functions from the
module RANDOM.
3.
module RANDOMTo access functions, you need to import the random module:
import random
Or import individual functions from it:
from random import random, randrange, randint
4.
Functions for getting integer "random"numbers - randint() and randrange()
The randint() and randrange() functions generate pseudorandom integers. The first of them is the simplest and always
takes only two arguments - the limits of the integer range
from which any number is selected:
In the case of randint(), both boundaries
are included in the range, i.e. in the
language of mathematics, the segment is
described as [a; b].
5.
Randrange()The randrange() function is more complicated. It can take one
argument, two or even three. If only one is specified, then it returns a
random number between 0 and the specified argument. Moreover, the
argument itself is not included in the range. In the language of
mathematics, this is [0; a).
6.
Randrange()• If two arguments are passed to randrange() , it works like randint()
with one exception. The upper bound is out of range, i.e. [a; b).
7.
Randrange()If three arguments are passed to randrange(), then the first two are the
range limits, as in the case of two arguments, and the third is the socalled step. If, for example, the function is called as randrange(10, 20,
3), then the "random" number will be chosen from the numbers 10, 13,
16, 19:
8.
The random() function - "random" realnumbers
To get a random real number, or, as they say, a floating point number,
you should use the random () function from the Python random
module of the same name. It takes no arguments and returns a number
between 0 and 1, not including 1:
9.
Round()The result contains many decimal places. To round it up, you can use
Python's built-in round() function:
10.
WhileWith the while loop we can execute a set of statements as long as a
condition is true.
11.
Break statementWith the break statement we can stop the loop even if the while
condition is true:
12.
Continue statementWith the continue statement we can stop the current iteration, and
continue with the next:
13.
Else statementWith the else statement we can run a block of code once when the
condition no longer is true:
14.
Tasks• Используя функцию randrange() получите псевдослучайное четное
число в пределах от 6 до 12. Также получите число кратное пяти в
пределах от 5 до 100.
• Using the randrange() function, get a pseudo-random even number
between 6 and 12. Also get a multiple of five between 5 and 100.
• Write a program that asks the user for the limits of the range. Displays
a suitable random number on the screen.
• Напишите программу, которая запрашивает у пользователя
границы диапазона. Выводит на экран подходящее случайное
число.