简体   繁体   中英

How can I generate in Python a matrix with n random elements between 1 and 30 and the rest of the elements equal to zeros?

I have to generate a matrix with of shape 10^4 x 10^4 with 100 integer items randomly chosen 1 and 30 and the rest of the elements equals to 0. Can anybody help me? :pray

Create the matrix with zeros, and then add the random numbers, or create an empty matrix, add the random numbers, and fill the empty values with 0.

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