[Python] How to display random numbers (random module)

[Python] How to display random numbers (random module)

A summary of the basic functions of the random module.

  1. [Integer](# integer)
  2. Smaller range (randrange) (# smaller range randrange)
  3. [above and below (randint)](# and below randint)
  4. [Decimal point](# Decimal point)
  5. [Random greater than 0 and less than 1](random greater than # 0 and less than 1)
  6. [uniform](# uniform)
  7. [Specify a range separately (choice)](#Specify a range separately (choice))

integer

① Smaller range (rand range)

Specify smaller in the range specification. random.randrange (initial value, end value, step)

** ▼ Specify only the end value ** For one argument, specify the ending value (less than).

print(random.randrange(1))
print(random.randrange(10))
print(random.randrange(10000))

#output
0
4
5310

-If the argument is 1, the corresponding integer is only 0. ・ In the case of 10, 0 to 9 ・ Minus cannot be specified

If negative, error


print(random.randrange(-10))

#output
# ValueError: empty range for randrange()

** ▼ Specify only the initial value and the end value ** In the case of two arguments, it is greater than or equal to A and less than or equal to B. └ Include initial value in range └ You can also specify a negative range
print(random.randrange(5,10))
print(random.randrange(-5,10))
print(random.randrange(-5,-1))

#output
5
3
-5

** ▼ Specify initial value, end value, step ** In the case of three arguments, only those that correspond to the step among the numerical values that are A or more and smaller than B are selected. └ Include initial value in range └ You can also specify minus
print(random.randrange(6,10,2))
print(random.randrange(-6,10,2))
print(random.randrange(-6,-10,-2))

#output
8
0
-6

** Error if step is out of range **

Steps out of range are in error


print(random.randrange(-6,-10,2))
print(random.randrange(6,10,-2))

#output
# ValueError: empty range for randrange()

### ② Above and below (randint) `random.randint (initial value, end value)` └ Only two arguments can be specified └ Step cannot be specified └ You can also specify a negative range
print(random.randint(6,10))
print(random.randint(-6,10))

#output
7
-5

** ▼ Step cannot be specified **

Specifying a step is an error


print(random.randint(6,10,2))

#output
TypeError: randint() takes 3 positional arguments but 4 were given

## Decimal point ### ① Range greater than 0 and less than 1 (random) `random.random()` └ No arguments required

python


print(random.random())

#output
0.006672554597154434

** ▼ Error when passing arguments **

Error when passing arguments


print(random.random(3))

#output
TypeError: random() takes no arguments (1 given)

② Range specification (uniform)

random.uniform (initial value, end value) └ Above and below (including) └ Minus is OK └ Initial value and end value can be exchanged (automatic judgment)

python


print(random.uniform(1,3))
print(random.uniform(3,1))
print(random.uniform(-3,-1))
print(random.uniform(-1,-3))

#output
2.5435117820109165
1.0971805105781995
-1.8504872730735842
-2.7854383306809494

## Specify the range separately (choice) `random.choice()` └ Negative numbers can be specified └ Decimal point can be specified
x = range(10)
y = [1, 6] 
z = [1.1, 6.7]
w = range(-20, -4)
v = [-2, -7]

print(random.choice([3,5,6,8]))
print(random.choice(x))
print(random.choice(y))
print(random.choice(z))
print(random.choice(w))
print(random.choice(v)) 

#output
8
5
1
6.7
-10
-7

The official page is [here](https://docs.python.org/ja/3/library/random.html)

Recommended Posts

[Python] How to display random numbers (random module)
How to create random numbers with NumPy's random module
python3 How to install an external module
How to display multiplication table in python
How to display python Japanese with lolipop
How to add python module to anaconda environment
[Python] How to deal with module errors
How to display Hello world in python
How to install Python
How to install python
[python] How to display list elements side by side
[Python] How to change the date format (display format)
How to add a Python module search path
[2020.8 latest] How to install Python
How to install Python [Windows]
How Python module import works
python3: How to use bottle (2)
[Python] How to use list 1
How to update Python Tkinter to 8.6
How to use the Raspberry Pi relay module Python
How to convert floating point numbers to binary numbers in Python
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
How to add page numbers to PDF files (in Python)
How to run Notepad ++ Python
How to change Python version
python, php, ruby How to convert decimal numbers to n-ary numbers
How to develop in Python
[python] How to judge scalar
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
Make the display of Python module exceptions easier to understand
[Ev3dev] How to display bmp image on LCD with python
Display numbers and letters assigned to variables in python print
Python learning basics ~ How to output (display) a character string? ~
How to install python using anaconda
How to write a Python class
[Python] How to FFT mp3 data
[Python] How to do PCA in Python
Python: How to use async with
[Rails] How to display Google Map
Testing with random numbers in Python
How to use the optparse module
[Python] How to derive nCk (ABC156-D)
[Python] How to use Pandas Series
How to collect images in Python
How to use Requests (Python Library)
How to use SQLite in Python
[Introduction to Python] How to parse JSON
How to use Python's logging module
How to get the Python version
How to get started with Python
[Python] How to import the library
[Python] How to use list 3 Added
How to use Mysql in python
How to use OpenPose's Python API