简体   繁体   中英

How to generate 2D array for random numbers?

I am a beginner here and want to generate a random number of float type in the range 0 to 1 with 3*4 size.

Later I want also to slice the first row and last column of the array.

I tried this one but it's not good way.

 import random
a=random.sample(range(0, 1))

Best

import numpy as np
import random
k=[[random.random() for i in range(1)] for j in range(12)]
j=np.array(k)
j.resize(3,4)
#output
array([[0.36682137, 0.97574318, 0.57012999, 0.22509075],
       [0.33362092, 0.60195643, 0.26148279, 0.67400063],
       [0.36204424, 0.04435383, 0.25686463, 0.53178748]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM