# random - python - 创建随机数矩阵的简单方法

``````
random_matrix = [[random.random() for e in range(2)] for e in range(3)]

``````

``````
weights_h = [[random.random() for e in range(len(inputs[0]))] for e in range(hiden_neurons)]

``````

Docstring : rand(d0 ,d1 ,... ,dn )

``````
>>> import numpy as np

>>> np.random.rand(2,3)

array([[ 0.22568268, 0.0053246 , 0.41282024],

[ 0.68824936, 0.68086462, 0.6854153 ]])

``````

``````
weights_h = [[random.random() for e in inputs[0]] for e in range(hiden_neurons)]

``````

``````
In [9]: numpy.random.random((3, 3))

Out[9]:

array([[ 0.37052381, 0.03463207, 0.10669077],

[ 0.05862909, 0.8515325 , 0.79809676],

[ 0.43203632, 0.54633635, 0.09076408]])

``````

``````
random_matrix = numpy.random.randint(min_val,max_val,(<num_rows>,<num_cols>))

``````

``````
#get a random array of floats between 0 and 1 as Pavel mentioned

W = numpy.random.random((L_out, L_in +1))

#normalize so that it spans a range of twice epsilon

W = W * 2 * epsilon

#shift so that mean is at zero

W = W - epsilon

``````

``````
import numpy

B = numpy.random.random((3, 4)) #its ndArray

C = numpy.matrix(B)# it is matrix

print(type(B))

print(type(C))

print(C)

``````

``````
random_matrix = [[random.random for j in range(collumns)] for i in range(rows)

for i in range(rows):

print random_matrix[i]

``````
``````
x = np.int_(np.random.rand(10) * 10)

``````

``````
map(lambda x: map(lambda y: ran(),range(len(inputs[0]))),range(hiden_neurons))

``````

``````
matrix = np.random.randint(maxVal, size=(rows, columns))

``````

``````
a = np.random.randint(10, size=(2,3))

``````

``````
#this is a function for a square matrix so on the while loop rows does not have to be less than cols.

#you can make your own condition. But if you want your a square matrix, use this code.

import random

import numpy as np

def random_matrix(R, cols):

matrix = []

rows = 0

while rows < cols:

N = random.sample(R, cols)

matrix.append(N)

rows = rows + 1

return np.array(matrix)

print(random_matrix(range(10), 5))

#make sure you understand the function random.sample

``````