Python. Functions for generating random floating point numbers




Module random. Functions for generating random floating point numbers. Functions for obtaining random numbers using distributions


Contents


Search other websites:

1. Function random.random(). Get a random number in the range [0; 1]

The random.random() function is used to get a random number in the range [0; 1].

Example.

# Function random.random()
# include the random module
import random

rnd_num = random.random() # rnd_num = 0.445307972457864

 

2. Function random.uniform() . Get a random number in a given interval

The random.uniform() function returns a random floating-point number in a given interval. The general form of the function is as follows

random.uniform(a, b)

here a, b – values specifying the interval within which the resulting random number will be obtained.
The value of the endpoint b is determined by the formula

a+(ba)*random()

Therefore, whether the endpoint b belongs to a given range depends on the rounding of this value for a floating point type.

Regarding the values of a, b, two cases are possible:

  • a<=b. In this case, the random number N is obtained such that it satisfies the condition a<=N<=b;
  • b<a. In this case, a random number N is obtained that satisfies the condition b<=N<=a.

Example.

# Function random.uniform()
# include the random module
import random

# Case 1. a<=b
# get a random number in the range [1.5, 2.7]
a = 1.5 # lower bound
b = 2.7 # upper bound
number = random.uniform(a, b) # number = 1.5904468975189021

# get a random number within [2.8, 3.3]
number = random.uniform(2.8, 3.3) # number = 2.8056183689090615

# Case 2. a>b
# get a random number within [2.7, 1.5]
a = 2.7
b = 1.5
number = random.uniform(2.7, 1.5) # number = 2.492950266273217

# Case 3. Negative numbers
a = -5.2
b = -3.8
number = random.uniform(b, a) # nbumber = -3.8546817374025304

 

3. Function random.triangular(). Get a random number in a given interval and given mode

The random.triangular() function returns a random floating-point number. According to Python documentation, the general form of the function is as follows

random.triangular(low, high, mode)

here

  • low – the smallest possible value of the resulting random number (lower bound). By default, low is zero;
  • high – the maximum possible value of the resulting random number (upper bound). By default, high is 1;
  • mode – argument specifying the output mode of a numerical value. By default, mode is the middle between the low and high arguments.

Example.

# Function random.triangular
import random

l = 1.5
h = 2.2

number = random.triangular(l,h) # number = 1.8082365488322254
number = random.triangular(-3.5, -6.2, 1) # number = -4.23510440860435


 

4. Function random.betavariate(). Get a random number within [0; 1] based on beta distribution

The random.betavariate () function returns a random number within [0; 1] based on the beta distribution. The general form of the function

random.betavariate(alpha, beta)

here – alpha, beta – parameters for which the conditions alpha> 0, beta>0 is fulfilled. The return value is between 0 and 1. If one of the conditions alpha>0 or beta>0 is not met, then the interpreter will throw an error.

Example.

# Function random.betavariate
import random

alpha = 7
beta = 9.5
number = random.betavariate(alpha, beta) # number = 0.22875540655977067

alpha = 3.0
beta = 1.5
number = random.betavariate(alpha, beta) # number = 0.6977893966563389

alpha = 0.1
beta = 0.1
number = random.betavariate(alpha, beta) # number = 0.0008738538852347506

 

5. Function random.expovariate(). Get random number based on exponential distribution

The random.expovariate() function returns a random value based on an exponential distribution. According to Python documentation, the general form of the function is as follows

random.expovariate(lambda)

here lambda – a parameter that is equal to the result of dividing 1.0 by the desired average value. The lambda parameter can be nonzero (lambda≠0). The lambda parameter should not be confused with the Python reserved name of the same name.

The random value that the function returns can be:

  • from 0 to +∞, if lambda> 0;
  • from -∞ to 0, if lambda<0.

Example.

# Function random.expovariate(lambd)
import random

# Case 1. lambd>0
lambd = 5
rnd_num = random.expovariate(lambd) # rnd_num = 0.6234234901590993

# Case 2. lambd<0
lambd = -8.2
rnd_num = random.expovariate(lambd) # rnd_num = -0.41089244332230856

 

6. Function random.gammavariate(). Get random number based on gamma distribution

The random.gammavariate() function returns a random number based on the gamma distribution. The general form of a function declaration

random.gammavariate(alpha, beta)

here alpha, beta – distribution parameters for which conditions alpha>0, beta>0 are satisfied. The resulting probability distribution function is calculated by the formula:

Python. Gamma distribution. Function random.gammavariate()

The above formula uses two functions from the Python library:

  • function math.exp(–x/beta) – calculates the exponent;
  • function math.gamma(alpha) – calculates gamma function.

The random.gammavariate() function should not be confused with the gamma function.

Example.

# Function random.gammavariate(alpha,beta)
import random

alpha = 2.0
beta = 4.5
rnd_num = random.gammavariate(alpha, beta) # rnd_num = 5.046693970249287

alpha = 3.0
beta = 1.1
rnd_num = random.gammavariate(alpha, beta) # rnd_num = 5.797395009650084

 

7. Function random.gauss(). Get a random number based on the Gaussian distribution

The random.gauss() function returns a random number based on the Gaussian distribution. According to the Python documentation, the general form of the function is as follows:

random.gauss(mu, sigma)

here

  • mu – a parameter that determines the average value;
  • sigma – parameter that defines the standard deviation.

Compared to the similar random.normalvariate() function, this function is faster.

Example.

# Function random.gauss()
import random

# Range [1-0.5; 1+0.5]
mu = 1.0
sigma = 0.5
rnd_num = random.gauss(mu, sigma) # rnd_num = 1.0460662110757315

# Range [250-0.3; 250+0.3]
mu = 250
sigma = 0.3
rnd_num = random.gauss(mu, sigma) # rnd_num = 250.09858114288903

 

8. Function random.lognormvariate(). Get a random number based on a logarithmic normal distribution

The random.lognormvariate() function returns a random number based on the logarithmic normal distribution. General form of function

random.lognormvariate(mu, sigma)

where

  • mu – mean;
  • sigma – standard deviation. The sigma value may be greater than zero.

If we take the natural logarithm from the obtained random number, we will get the normal distribution.

Example.

# Function random.lognormvariate()

# include the random module
import random

# include the math module
import math

# Range [3-0.5; 3+0.5]
mu = 3.0
sigma = 0.5
rnd_num = random.lognormvariate(mu, sigma)
number = math.log(rnd_num) # range [3-0.5; 3+0.5]

print("rnd_num = ", rnd_num)
print("log(rnd_num) = ", number)

The result of the program

rnd_num = 13.614067024574224
log(rnd_num) = 2.6111035982231474

 

9. Function random.normalvariate(). Get random number based on normal distribution

The random.normalvariate() function returns a random number based on the normal distribution. General form of function

random.normalvariate(mu, sigma)

here

  • mu – mean;
  • sigma – standard deviation.

Example.

# Function random.normalvariate()
import random

mu = 10.0
sigma = 2.0
rnd_num = random.normalvariate(mu, sigma) # rnd_num = 11.529818975017623

 

10. Function random.vonmisesvariate(). Circular data distribution

The random.vonmisesvariate() function returns a random number based on a circular distribution of data. The general function declaration form is as follows

random.vonmisesvariate(mu, kappa)

here

  • mu – mean angle expressed in radians between the [0; 2·π];
  • kappa – concentration parameter. The value kappa must satisfy the condition kappa≥0. If kappa=0, then the function returns a random angle within [0; 2·π].

Example.

# Function random.vonmisesvariate()
import random

mu = 10.0
kappa = 2.0
rnd_num = random.vonmisesvariate(mu, kappa) # rnd_num = 3.0806812333424087

mu = 1.0 # 1 радиан
kappa = 0
rnd_num = random.vonmisesvariate(mu, kappa) # rnd_num = 0.10073252837746509

 

11. Function random.paretovariate(). Get a random number based on the Pareto distribution

The random.paretovariate() function returns a random number based on the Pareto distribution. The general form of the function is as follows

random.paretovariate(alpha)

here alpha – shape parameter. The value of alpha must be nonzero.

Example.

# Function random.paretovariate()
import random

alpha = 2.3
rnd_num = random.paretovariate(alpha) # rnd_num = 1.1150245760736535

alpha = -2.3
rnd_num = random.paretovariate(alpha) # rnd_num = 0.08129209858766535

 

12. Function random.weibullvariate(). Get a random number based on the Weibull distribution

The random.weibullvariate() function returns a random number based on the Weibull distribution. General form of function

random.weibullvariate(alpha, beta)

here

  • alpha – scale parameter;
  • beta – shape parameter.

Example.

# Function random.weibullvariate()
import random

alpha = 1.5
beta = 2.0
rnd_num = random.weibullvariate(alpha, beta) # rnd_num = 1.5819775812273797

alpha = 500
beta = 50
rnd_num = random.weibullvariate(alpha, beta) # rnd_num = 486.648957741605

 


Related topics