Recently, I have searched all the functions of Python and found that it is impossible to directly generate random two-dimensional arrays, including various methods related to random(), but I have not obtained the desired result. Finally, I was inspired in a blog to obtain a random two-dimensional array through list parsing.
The details are as follows:
a = [[(1, 4) for j in range(1, 3)] for i in range(1, 11)] print(array(a))
where (1, 4) is used to generate a random integer. At this time, an array of 10 rows and 2 columns is created.
[[1 3] [1 2] [3 1] [1 2] [1 2] [3 3] [3 1] [1 3] [1 2] [3 3]]
Then summarize the various methods in random(), as follows:
1、choice()
Get a random element from the sequence
(['a','b','c'])
The result is ‘b’
2、randint()
Used to generate an integer in a specified range, which can obtain the boundary value.
(1, 2)
The result is '1'
3、random()
Used to generate a random number of characters from 0 to 1: 0 <= n < 1.0
()
The result is ‘0.29892210378218154’
4、randrange()
Get a random number from the sequence
(10,100,2)
The result is '68'
At this time, if 'module' has no attribute 'randrange' appears
Remember to add import random at the beginning
5、sample()
Randomly get fragments of the specified length from the specified sequence without changing the original sequence order
list2 = [i**2 for i in range(0, 10)] print((list2, 3))
The result is ‘[16, 9, 4]’
See if list2 has changed
The result is ‘[0, 1, 4, 9, 16, 25, 36, 49, 64, 81] '
No change, so the sample function will not modify the original sequence
6、shuffle()
Breaking elements in a list
list1 = [1,2,3,4,5,6] (list1) print(list1)
The result is ‘[5, 3, 2, 1, 4, 6]'
7、uniform()
Used to generate a random number of characters in a specified range
a=(2,3)
The result is ‘2.046090433068011’
The above detailed explanation of the examples of random two-dimensional arrays of python is all the content I share with you. I hope you can give you a reference and I hope you can support me more.