Please refer to here for notes.
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.
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.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.
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.
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.
https://docs.python.org/ja/3/library/random.html#functions-for-integers
Recommended Posts