Random function details of Python built-in functions

Time:2022-5-27
catalogue

Import module:

?
1
import random

1、random. Choice (list / tuple / string), select an element randomly in the list or tuple, and select a character randomly if it is a string

?
1
2
num1 = random.choice(['hello',True,1,[1,4,5]])
print(num1)

Output (the result of each output is not necessarily the same):

1

2、random. Random ([start, end, step): returns a random number from [start, end) with step size of step

  • If start is not written, it defaults to 0
  • Multiple steps are not written. The default value is 1
  • But end must have num2=
?
1
2
3
4
5
random.randrange(100)
print(num2)#Head and tail
num3 = random.randrange(80,100,2)
print(num3)#Take a random number from 1 to 6
print(random.randrange(1,7))

Output (the result of each output is not necessarily the same):
83
84
6

3、random. Random (): returns a random number of [0,1], and the result is a floating point number

?
1
2
3
4
5
6
'''
No one answers the problems encountered in learning? Xiaobian created a python learning exchange group: 531509025
Look for like-minded friends to help each other. There are also good video learning tutorials and PDF e-books in the group!
'''
num4 = random.random()
print(num4)

Output (the result of each output is not necessarily the same):
0.8073295394931393

4、random. Shuffle (list): randomly sort all elements in the sequence, directly operate the sequence [sequence changes], and there is no return value

?
1
2
3
4
list1 = [1,2,3,5,6,7]
random.shuffle(list1)
print(random.shuffle(list1)) #The returned result is none
print(list1)

Output (the result of each output is not necessarily the same):
None
[1, 7, 5, 6, 3, 2]

5、random. Uniform (m, n): randomly generate a floating-point number of [M, n]

?
1
print(random.uniform(5,4))

Output (the result of each output is not necessarily the same):
4.697767338612918

6、random.randint(m,n)

Randomly generate an integer of [M, n]

?
1
print(random.randint(-1,4))

Output (the result of each output is not necessarily the same):
0

This is the end of this article about the details of random functions of Python built-in functions. For more information about random functions of Python built-in functions, please search the previous articles of developeppaper or continue to browse the relevant articles below. I hope you will support developeppaper in the future!

Recommended Today

Vue2 technology finishing 3 – Advanced chapter – update completed

3. Advanced chapter preface Links to basic chapters:https://www.cnblogs.com/xiegongzi/p/15782921.html Link to component development:https://www.cnblogs.com/xiegongzi/p/15823605.html 3.1. Custom events of components 3.1.1. Binding custom events There are two implementation methods here: one is to use v-on with vuecomponent$ Emit implementation [PS: this method is a little similar to passing from child to parent]; The other is to use ref […]