current position:Home>Python notes (21): random number module

Python notes (21): random number module

2022-01-30 16:49:59 A bowl week

Little knowledge , Great challenge ! This article is participating in “ A programmer must have a little knowledge ” Creative activities .

Hello everyone , I am a A bowl week , One doesn't want to be drunk ( Internal volume ) The front end of the . If you are lucky enough to get your favor , I'm very lucky ~

This module implements pseudo-random number generator of various distributions . For integers , There is a unified choice from the scope . For the sequence , There is a uniform selection of random elements 、 A function used to generate a random arrangement of lists 、 And functions for random sampling without replacement .

Generate random number

random.random() Method

Returns a randomly generated real number , It's in [0,1) Within the scope of .

Grammatical structure

import random  #  Import  random  modular 

 Copy code 

random.randint Method

Grammar format

 Copy code 

Function returns a number N ,N by a To b Number between (a <= N <= b), contain a and b

random.uniform Method

Grammar format

 Copy code 

Function returns a random floating-point number N , When a <= b when a <= N <= b , When b < a when b <= N <= a .

random.randrange Method

Grammatical structure

random.randrange(start, stop[, step])
 Copy code 
  • start: Count from start Start . The default is 0 Start . for example randrange(5) Equivalent to range(0, 5);
  • stop: Count to stop end , But does not include stop. for example :randrange(0, 5) yes [0, 1, 2, 3, 4] No, 5
  • step: step , The default is 1. for example :range(0, 5) Equivalent to randrange(0, 5, 1)

Returns a number in a random range

Equivalent to choice(range(start, stop, step))

Sample code

import random

random_value = random.random()

randint_value = random.randint(1, 3)  #  Returns a random integer 

uniform_value = random.uniform(1, 3)  #  Returns a random floating point number 

randrange_value = random.randrange(1, 100, 2)  # 1 To 100 In steps of 2( We can only get 100 Think of an odd number )
 Copy code 

The result of each run is different .

A sequence of functions for


Returns a random element from a non empty sequence , If the sequence is empty , Throw an exception

Sample code

import random

value = random.choice(range(1, 100, 2))
#  This is equal to random.randrange(1, 100, 2)
 Copy code 

It seems that this method is necessary to shuffle cards

Generate a small case of verification code

import random
#  A small case of generating verification number 
all_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
captcha = ''
for _ in range(4):
    a = random.choice(all_chars)
    captcha += a  #  Connection string 
 Copy code 

random.shuffle() Method

shuffle(list) Method randomly sorts all elements of the sequence

Sample code

import random
list1 = ["beautiful" , "cute", "beautiful", 'prefect', "beautiful", " Sweet ", 'lovely']

 Copy code 

random.sample() Method

random.sample(sequence, k), Randomly get a fragment of a specified length from a specified sequence .sample Function does not modify the original sequence . The slice length cannot exceed the original length , Otherwise, an exception will be thrown

Sample code

import random
list1 = ["beautiful", "cute", "beautiful", 'prefect', "beautiful", " Sweet ", 'lovely']
list2 = random.sample(list1, len(list1))
print(" Original list :", list1)
print(" New list :", list2)
 Copy code 

This method can sort the original sequence without destroying the original sequence

copyright notice
author[A bowl week],Please bring the original link to reprint, thank you.

Random recommended