程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Pythons built-in random module [source code attached]

編輯:Python

Python One of the built-in modules random

======================

random Kuo is Python Standard library for generating random numbers in , The list of included functions is as follows :

  • Basic random functions :seedrandomgetstatesetstate;
  • Extended random functions :randintgetrandbitsrandrangechoiceshufflesample;
  • Distributed random function :uniformtriangularbetavariateexpovariategammavariategausslognormvariatenormalvariatevonmisesvariateparetovariateweibullvariate.undefined Find words variate The frequency of occurrence is relatively high , But it's a variable .

Basic random functions


seed And random function

seed Function initializes a random seed , The default is the current system time .

random function Generate a [0.0,1.0) Between random decimals .

The specific code is as follows :

import random
random.seed(10)
x = random.random()
print(x)

What needs to be explained is random.seed function , adopt seed function You can generate the same random number each time , For example, the following code :

import random
random.seed(10)
x = random.random()
print(x)
random.seed(10)
y = random.random()
print(y)

The values obtained on different codes are different , however x And y It's the same .

0.5714025946899135
0.5714025946899135

12.1.2 getstate() and setstate(state)

getstate Function to record the state of the random number generator ,setstate Function to restore the generator to the last recorded state .

# Record the state of the generator
state_tuple = random.getstate()
for i in range(4):
print(random.random())
print("*"*10)
# After passing in the parameter, the previous state is restored
random.setstate(state_tuple)
for j in range(4):
print(random.random())

The output random number is consistent twice .

0.10043296140791758
0.6183668665504062
0.6964328590693109
0.6702494141830372
**********
0.10043296140791758
0.6183668665504062
0.6964328590693109
0.6702494141830372

Extended random functions


random There are several extended random functions :

randint`、`getrandbits`、`randrange`、`choice`、`shuffle`、`sample

randint and randrange

randint Generate a [x,y] Integers within an interval .

randrange Generate a [m,n) In the interval with k Is a random integer of step size .

The test code is as follows :

x = random.randint(1,10)
print(x)
y = random.randrange(1,10,2)
print(y)

These two functions are relatively simple ,randint The function prototype is as follows :

random.randint(start,stop)

Parameters start Represents the minimum value , Parameters stop Represents the maximum value , It's closed at both ends , That is to say start and stop Can be obtained .

randrange The function prototype is as follows :

random.randrange(start,stop,step)

If the function is called with only one parameter , The default is 0 To the parameter value , The function and randint The difference lies in , The function is left closed and right open , The last parameter is the step size .

Look up the effect , You can copy the following code to run :

for i in range(3):
print("*"*20)
print(random.randrange(10))
print(random.randrange(5,10))
print(random.randrange(5,100,5))

getrandbits(k) and choice(seq)

getrandbits Generate a k Random integers that are longer than special , The actual output is k A decimal number converted from a binary number .

choice Randomly select an element from a sequence .

x = random.getrandbits(5)
print(x)
# The length of the generation is 00000-11111

getrandbits(k) The function can be simply described as follows : Output one $[0,2^k-1]$ A random integer in the range ,k It means 2 The number of decimal digits .

choice It's simpler , Returns a random element from the list .

import random
my_list = ["a", "b", "c"]
print(random.choice(my_list))

shuffle(seq) and sample(pop,k)

shuffle The function is used to sort elements in a sequence at random , And the original sequence was modified .

sample Functions are used to randomly select... From a sequence or set k A choice , The original sequence is the same .

my_list = [1,2,3,4,5,6,7,8,9]
random.shuffle(my_list)
print(my_list)

shuffle Functions can only be used for variable sequences , Immutable sequence ( As tuple ) There will be errors .

my_list = [" Dream ", " Eraser ", 1, 2, [3, 4]]
print(my_list)
ls = random.sample(my_list, 4)
print(ls)

Distributed random function


This part involves more , Focus on some important and common functions .

uniform(a,b) 、betavariate and triangular function

uniform Generate a [a,b] Between random decimals , Using equal probability distribution .

betavariate Generate a [0,1] Between random decimals , use beta Distribution .

triangular Generate a [low,high] Between random decimals , Using a triangular distribution .

In the use of uniform You need to pay attention to , If a<b, Then generate a b-a Decimal between .

for i in range(3):
print(random.uniform(4, 1))

Other distributed random functions

Here's how to generate random numbers , It's just that the underlying core algorithms are different .

、、、、、、、.

  1. expovariate: Generate a (0,∞) Random integer between , An index distribution ;
  2. gammavariate: use gamma Distribution ;
  3. gauss: Using Gauss ( young cute boy ) Distribution ;
  4. lognormvariate: Lognormal distribution ;
  5. normalvariate: normal distribution ;
  6. vonmisesvariate: Von Mises distribution ;
  7. paretovariate: Pareto distribution ;
  8. weibullvariate: Weber distribution .

The summary of this blog


This blog has learned Python Knowledge points related to random numbers in , I hope it helped you .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved