python generates random function random

random is a built-in function that generates random numbers

import module:

import random 

 

 

Then you can call the functions under the random module. Use dir(random) to view the functions under the random module. The results are as follows:

1 >>> dir(random)  
2 ['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', '_BuiltinMethodType', '_MethodType', '_Sequence  
3 ','_Set', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_acos', '_bisect  
4 ', '_ceil','_cos', '_e', '_exp', '_inst', '_itertools', '_log', '_pi', '_random', '_sha512', '_sin', '_sqrt', '_test', '_test_generator', '_urandom'  
5 , '_warn', 'betavariate', 'choice', 'choices', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'lognormvariate', 'normalvariate',   
6 'paretovariate', '_dating, 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvaria  
7 te'] 

 

 

The most commonly used functions are as follows:

 random.randint

1 random.randint (1,10)

The meaning of the statement is to generate a random number (integer int type) from 1 to 10 (including 1 and 10). (The parameter is an integer, not a floating-point number, otherwise an error will be reported)

1 random.randint(20, 10) #This statement is wrong. Lower bound must be less than or equal to upper bound  

 

random.random

1 random.random() 

Generates a random floating point number between 0 and 1, including 0 but not 1, i.e. [0.0, 1.0).

 

random.uniform

1 random.uniform(a, b)

Generate a random floating point number between a and b. However, unlike randint, a and b can be non-integers and do not need to consider the size.

which is

1   random.uniform(3.65,10.56) #can be like this
1 random.uniform(10.56, 3.65) #Also possible  

 

   

 

random.choice

1 random.choice(seq)

Pick an element at random from the sequence. seq needs to be a sequence, such as list, tuple, string.

1 random.choice([1, 2, 3, 5, 8, 13]) # list   
2    
3 random.choice( ' hello ' ) #string 4 5 random.choice  
 ([ ' hello ' , ' world ' ]) #List of strings   6 7 random.choice ((1, 2, 3)) #tuple   

   

are all possible uses.

 

random.randrange

1 random.randrange (start, stop, step)

Generate a random integer from start to stop (excluding stop) with an interval of steps. start, stop, and step must be integers, and start<stop. Such as:

1   random.randrange(0, 21, 2)) #Randomly generate even numbers between 0-20

 

 

random.sample

1 random.sample(p, k) 

From the p sequence, randomly obtain k elements to generate a new sequence. sample does not change the original sequence.

This module is very 666, and also supports triangular, beta distribution, exponential distribution, gamma distribution, Gaussian distribution and other very professional random algorithms.

 

random.shuffle

1 random.shuffle(x)  

Shuffle the elements in the sequence x. Shuffle directly changes the original sequence. Such as:

1 import random  
2 a=[1,2,3,4,5,6]  
3 random.shuffle(a)  
4 print(a

The result is as follows:

1 [5, 1, 3, 6, 4, 2]

Novices may encounter some errors when using this function, as follows:

1 import random  
2 a=[1,2,3,4,5,6]  
3 print(random.shuffle(a))  

Using this method will result in None, because random.shuffle() is used to shuffle the list elements and has no return value, so you cannot use print(random.shuffle(a)) to output the shuffle

the sequence after

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324733163&siteId=291194637