This is the 20th day of my participation in the Genwen Challenge

Part of our inner fear is caused by past wounds, but part of it is caused by our imagination and magnification of fear.

In C we can use the rand and srand functions to generate random numbers, and these functions need to use the

library.

So what libraries do we need in Python? Is it harder to use than C? Or will it be more interesting to use than C language? So many people say that learning C language is boring. Xiaobian has been wondering whether programming really can only be boring? Or how to combine programming languages with reality? To achieve these certainly is the need for very high technical content, So, continue to walk forward, just as the Internet said that young people do not speak martial ethics, directly open dry ~

The Random library

The Random library is a Python standard library that uses random numbers. (Install python library, open CMD, type: PIP install library name)

Using/calling the Random library: import random

The random() method returns a randomly generated real number in the range [0,1).

Random common functions

Contains two types of functions, eight of which are commonly used:

Basic random number functions: seed(),random()

Random extension functions: Randint (), Getrandbits (), Uniform (), Randrange (), Choice (), Shuffle ()

Of course, one of the questions is why do we generate random numbers? Or how do random numbers come about?

Computers need a random number seed To generate random numbers, so there is a good chance that you will be lucky enough To win a lottery

The seed of random number determines the generation of random sequence. For example, given a random number seed 10, a random sequence is generated by Masen rotation algorithm, then every number in the random sequence is a random number. As long as the seeds are the same, both each number and the relationship between the numbers are determined. That is, the random number seed determines the generation of random numbers.

Introduction to correlation functions

Introduction to 2 basic random functions:

1. Seed (a=None) : initializes the given random number seed, which defaults to the current system time

Import random# reference random. Seed (10) import random# reference randomCopy the code

2. Random (): Generates a random decimal between [0.0, 1.0]

random.random() 
random.seed(10)
Copy the code

As long as the seeds are the same, the random numbers generated are also the same, also called pseudo-random numbers

Output:

print(random.random())
Copy the code

Six extended random number functions:

1. Randint ():randint(a,b): generates an integer between [a,b]

The random. Randint (10100).Copy the code

2. Randrange ():randrange(m,n[,k]): generates a random number of steps between [m,n)

The random randrange (10100, 10)Copy the code

3. Getrandbits (): Getrandbits (k): generates an integer k bits long

random.getrandbits(16)
Copy the code

Uniform ():uniform(A, B): generate a random decimal between [A, B]

The random. Unifom (10100).Copy the code

5. Choice (): Choice (SEQ): Selects a random element from the sequence SEq

The random chioce (,2,3,4,5,6,7,8,9 [1])Copy the code

Shuffle ():shuffle(seq): Randomly arranges the elements in the sequence SEQ and returns the shuffled sequence

S =,2,3,4,5,6,7,8,9 [1]; random.shuffle(s); print(s)Copy the code

The above codes are random number functions, and the output results may not be the same (except for seed numbers), so the relevant running results are not shown.

(Python series) To continue…