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

Python module tutorial: random number generation

編輯:Python

Python Medium random The module is used to generate random numbers .

Before using this module, you need import random

Several common function usages :

1、random.random

The function prototype :

random.random()

Used to generate a 0 To 1 The number of random characters of : 0 <= n < 1.0

>>> random.random()
0.5578093677010638

2、random.uniform

The function prototype :

random.uniform(a, b)

Used to generate a random number of characters in a specified range , One of the two parameters is the upper limit , One is the lower limit . If a > b, Then the generated random number n: b <= n <= a. If a <b, be a <= n <= b.

>>> random.uniform(10, 20)
16.864972616523794
>>> random.uniform(20, 10)
10.851664722380086

3、random.randint

The function prototype :

random.randint(a, b)

Used to generate an integer in a specified range . The parameter a Is the lower limit , Parameters b Is the upper limit , Generated random number n: a <= n <= b.

>>> random.randint(12, 20)
17
>>> random.randint(20, 20)
20
>>> random.randint(30, 20) # You can't do that , The lower limit must be less than or equal to the upper limit 
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "D:\Software\Anaconda3\lib\random.py", line 221, in randint
return self.randrange(a, b+1)
File "D:\Software\Anaconda3\lib\random.py", line 199, in randrange
raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))

4、random.randrange

The function prototype :

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

From the specified range , Gets a random number from a set incremented by the specified cardinality . Such as :random.randrange(10, 100, 2), The result is equivalent to from [10, 12, 14, 16, … 96, 98] Get a random number in the sequence .random.randrange(10, 100, 2) In the result with random.choice(range(10, 100, 2) equivalent .

>>> random.randrange(10, 100)
29
>>> random.randrange(10, 100, 2)
98

5、random.choice

The function prototype :

random.choice(sequence)

Get a random element from the sequence . among , Parameters sequence Represents an ordered type . Be careful :sequence stay python It's not a specific type , It refers to a series of types .list, tuple, All strings belong to sequence.

''' No one answers the problems encountered in learning ? Xiaobian created a Python Exchange of learning QQ Group :711312441 Looking for small partners who share the same aspiration , Help each other , There are also good video tutorials and PDF e-book ! '''
>>> random.choice('HelloWorld')
'r'
>>> random.choice(['java', 'python', 'C' , 'PHP'])
'python'
>>> random.choice(('list', 'tuple', 'dict'))
'tuple'

6、random.shuffle

The function prototype :

random.shuffle(x[, random])

Used to scramble elements in a list .

>>> l = ['java', 'python', 'C' , 'PHP']
>>> random.shuffle(l)
>>> l
['PHP', 'C', 'java', 'python']

7、random.sample

The function prototype :

random.sample(sequence, k)

Randomly get a fragment of a specified length from a specified sequence .sample Function does not modify the original sequence .

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

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