Python__random Library Basic Introduction

Source: Internet
Author: User
Tags shuffle types of functions

The random library is a Python standard library that uses stochastic numbers

From the perspective of probability, random numbers are randomly generated data (such as coin toss), but it is impossible for a computer to produce random values, and the true random number is a definite value produced under certain conditions, but these conditions are not understood or beyond our comprehension. A computer does not produce a true random number, so a pseudo-random number is called a random number.

Pseudo-random number: (pseudo) random sequence elements generated by using the Mason rotation algorithm in a computer

The library for generating pseudo-random numbers in Python is random

Because it is a standard library, only import random is required when using

The random library contains two types of functions, a total of 8 common

--Basic random function: seed (), random ()

--Extended Random function: Randint (), Getrandbits (), Uniform (), Randrange (), Choice (), Shuffle ()

Basic Random number

In Python, random numbers are generated using random number seeds (as long as the seed is the same, the resulting random sequence, regardless of the relationship between the number and number is deterministic, so the random number seed determines the generation of the random sequence)

Random sequence of the random number seed Mason rotation algorithm

10 0.5714025946899135

Each number in a random sequence is a random number.

Basic random functions
Function

Describe

Seed (A=none)

Initializes the given random number seed, tacitly considers the current system time

>>>random.seed (#产生种子10对应的序列)

Random ()

Generates a random decimal between [0.0,1.0]

>>>random.random ()

0.5714025946899135 #随机数产生与种子有关, if the seed is 1 Oh, the first number must be this one

The benefit of using a random number seed is a program that can re-existing random numbers

Extended Random number function

In the random library, the most basic is the seed and the random function, but the function is relatively single, resulting in 6 extended random number function

Extended Random number function
Function Describe
Randint (A, B)

Generates an integer between [a, b]

>>>random.randint (10,100)

Randrange (M,n[,k])

Generates a random integer with a K-step between [M,n]

>>>random.randrange (10,100,10)

Getrandbits (k)

Generate a random integer with K-specific features

>>>random.getrandbits (16)

37885

Uniform (A, B)

Generates a random decimal between [a, b]

>>>random.uniform (10,100)

16.848041210321334

Choice (seq)

Sequence correlation

Randomly select an element from the sequence

>>>random.choice ([1, 2, 3, 4, 5, 6, 7, 8, 9])

8

Shuffle (SEQ)

Sequence correlation

Randomly arranges elements in a sequence seq to return scrambled sequences

>>>s=[1, 2, 3, 4, 5, 6, 7, 8, 9]; Random.shuffle (s); Print (s)

[9, 4, 6, 3, 5, 2, 8, 7, 1]

Key points for using random number functions:

--Ability to generate "OK" pseudo-random number seed with random number seed, random function to generate stochastic number

--ability to generate random integers

--ability to randomly manipulate sequence types

Python__random Library Basic Introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.