Python Random Number Module Details

  • 2021-12-12 04:58:38
  • OfStack

Directory 1, Generate Random Numbers 1.1 random. random () Method 1.2 random. randint Method 1.3 random. uniform Method 1.4 random. randrange Method 2, Function for Sequence 2.1 random. choice (seq) 2.2 random. shuffle () Method 2.3 random. sample () Method


The random number module implements various distributed pseudo-random number generators. For integers, there is a choice of unity 1 from the range. For sequences, there are unified 1 selections of random elements, functions for generating random permutations of lists, and functions for random sampling without substitution.

1. Generate random numbers

1.1 random. random () Method

Returns a randomly generated real number in the range [0, 1].

Grammatical structure:

import random  #  Import  random  Module 


1.2 random. randint method

Syntax format:


Function returns a number N , N For a To b The number between ( a <= N <= b ), containing a And b

1.3 random. uniform method

Syntax format:


The function returns a random floating-point number N , when N 0 Hour a <= N <= b , when b < a Hour b <= N <= a .

1.4 random. randrange method

Grammatical structure:

random.randrange(start, stop[, step])

start: Count from start Here we go. The default is 0. For example randrange(5) Equivalent to range(0, 5); stop: Count to stop End, but not include stop . For example: randrange(0, 5) Yes [0, 1, 2, 3, 4] No 5 step: Step size, default to 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 random floating-point numbers 

randrange_value = random.randrange(1, 100, 2)  # 1 To 100 Step length is 2 (You can only get it here 100 Odd number thought) 

The results obtained from each run are different.

2. Functions for sequences

2.1 random.choice(seq)

Returns 1 random element from a non-empty sequence, and throws an exception if the sequence is empty

Sample code:

import random

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

It seems that this method is needed to shuffle cards

Generate a small case of verification code:

import random

# 1 Small cases of generating verification numbers 
all_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
captcha = ''
for _ in range(4):
    a = random.choice(all_chars)
    captcha += a  #  Connection string 

2.2 random. shuffle () Method

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

Sample code:

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


2.3 random. sample () Method

random.sample(sequence, k) Gets a random fragment of the specified length from the 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)

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

Related articles: