First Python 3 ~ Extra: Numerical Random Fantasy ~

Introduction

Please refer to here for notes.

What made me decide to draw this article

I would like to enter an appropriate number for Python calculation.
So I decided to use a random module that refines random numbers.
It's like a memorandum about such a random module.

Welcome to the world of random numbers

Import random module

The random module is included in the Python standard library and will not be downloaded.
However, import is required.

import random #Describe this in the upper row

Random module function

random.random() Generates a floating-point float type random number between 0.0 and 1.0.

import random

print(random.random())

#Execution result ↓
# 0.830963714448
# 0.253643551299
# 0.906046050863 etc.

random.uniform(a,b) Generate a floating point float random number in any range (between a and b).

import random

print(random.uniform(10,20))

#Execution result ↓
# 10.4740789672
# 14.8562079851
# 10.2516560825 etc.

・ The two numbers are in no particular order of size.

import random

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

#Execution result ↓
# -14.6138465193
# -1.29546679922
# 5.63021229752 etc.

・ If the numbers are the same, only that value is returned.

import random

print(random.uniform(10,10))

#Execution result ↓
# 10.0

The argument can also be a floating point float type.

Whether the endpoint value b is included in the range depends on the rounding of the floating point in the equation a + (b-a) * random (). -Python documentation: random --- Quoted from generating pseudo-random numbers

import random

print(random.uniform(1.234,4.321))

#Execution result ↓
# 1.93441022683
# 2.75716839399
# 3.91940634098 etc.

random.randrange(a,b,x) Returns an element (integer int type) randomly selected from the elements of range (a, b, x).
a ... start, x ... step can be omitted.
In that case, a = 0 and x = 1.

import random

print(list(range(10)))

print(random.randrange(10))

#Execution result ↓
# [0,1,2,3,4,5,6,7,8,9]
#7 etc.
x = step setting

You can generate odd and even random integers, and you can generate random integers that are multiples of 3.

import random

print(list(range(2,10,2)))

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

#Execution result ↓
# [2, 4, 6, 8]
#4 etc.

random.randint(a,b) Returns a random integer int greater than or equal to a and less than or equal to b.

import random

print(random.randint(10,100))

#Execution result ↓
#37 etc.
Spill story

If you want to output an integer between 1000 and 10000 with 0s up to the hundreds digit such as 3000 or 6000,
random.randint (1,10) result * 1000.

import random

print(random.randint(1,10) * 1000)

#Execution result ↓
# 3000
#4000 etc.

reference

https://docs.python.org/ja/3/library/random.html#functions-for-integers

Recommended Posts

First Python 3 ~ Extra: Numerical Random Fantasy ~
First time python
First time python
First Python ~ Coding 2 ~
First python [O'REILLY]
First Python 3rd Edition
PyQ ~ Python First Steps ~
First Python image processing
Random walk in Python
[Python] Python / Scikit-learn's first SVM
Random string generation (Python)
First Python miscellaneous notes
[Python] Chapter 01-01 About Python (First Python)
Numerical calculation with Python
First Fabric (Python deployment tool)
Balanced Random Forest in python
First neuron simulation with NEURON + Python
Use Random Forest in Python
[python] Random number generation memorandum
Weighted random choice in python
[Scientific / technical calculation by Python] 2D random walk (drunk walk problem), numerical calculation