28

I know that an easy way to create a NxN array full of zeroes in Python is with:

[[0]*N for x in range(N)]

However, let's suppose I want to create the array by filling it with random numbers:

[[random.random()]*N for x in range(N)]

This doesn't work because each random number that is created is then replicated N times, so my array doesn't have NxN unique random numbers.

Is there a way of doing this in a single line, without using for loops?

2
  • I know this is 6 years late, but an even more efficient way to create an N-square matrix of zeroes is [[0]*N]*N. Commented Sep 4, 2020 at 16:34
  • The simplest (and fastest) way of creating a N x N size matrix using numpy is simply np.zeros((N, N)). For any other value, one can use np.full((N, N), fill_value). Commented Aug 14, 2024 at 11:02

7 Answers 7

53

You could use a nested list comprehension:

>>> N = 5
>>> import random
>>> [[random.random() for i in range(N)] for j in range(N)]
[[0.9520388778975947, 0.29456222450756675, 0.33025941906885714, 0.6154639550493386, 0.11409250305307261], [0.6149070141685593, 0.3579148659939374, 0.031188652624532298, 0.4607597656919963, 0.2523207155544883], [0.6372935479559158, 0.32063181293207754, 0.700897108426278, 0.822287873035571, 0.7721460935656276], [0.31035121801363097, 0.2691153671697625, 0.1185063432179293, 0.14822226436085928, 0.5490604341460457], [0.9650509333411779, 0.7795665950184245, 0.5778752066273084, 0.3868760955504583, 0.5364495147637446]]

Or use numpy (non-stdlib but very popular):

>>> import numpy as np
>>> np.random.random((N,N))
array([[ 0.26045197,  0.66184973,  0.79957904,  0.82613958,  0.39644677],
       [ 0.09284838,  0.59098542,  0.13045167,  0.06170584,  0.01265676],
       [ 0.16456109,  0.87820099,  0.79891448,  0.02966868,  0.27810629],
       [ 0.03037986,  0.31481138,  0.06477025,  0.37205248,  0.59648463],
       [ 0.08084797,  0.10305354,  0.72488268,  0.30258304,  0.230913  ]])

(P.S. It's a good idea to get in the habit of saying list when you mean list and reserving array for numpy ndarrays. There's actually a built-in array module with its own array type, so that confuses things even more, but it's relatively seldom used.)

Sign up to request clarification or add additional context in comments.

2 Comments

What if you want the numbers to be sequential integers, instead of random?
@FaCoffee, suppose you want integers in [A, B), you can do (A + np.random.random((N,N)) * (B - A)).astype(int)
21

Use this simple function from numpy:

Array of size (4,4) filled with numbers 1-4

 np.random.randint(1, 5, size=(4, 4))


 [1 2 1 2]
 [2 2 2 4]
 [4 1 1 2]
 [4 2 2 4]

1 Comment

How do I add infinity to this array?
6

This is how you create a 2d array:

k = np.random.random ([3,4]) * 10
k.astype(int)

Comments

4

Just use [random.random() for i in range(N)] inside your list comprehension.

Demo:

>>> import random
>>> N = 3
>>> [random.random() for i in range(N)]
[0.24578599816668256, 0.34567935734766164, 0.6482845150243465]
>>> M = 3
>>> [[random.random() for i in range(N)] for j in range(M)]
[[0.9883394519621589, 0.6533595743059281, 0.866522328922242], [0.5906410405671291,         0.4429977939796209, 0.9472377762689498], [0.6883677407216132,     0.8215813727822125, 0.9770711299473647]]

Comments

3

You can use list comprehensions.

[[random.random() for x in xrange(N)] for y in xrange(N)]

https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions

For large multi dimensional arrays, I suggest you use numpy though.

Comments

3

It can be done without a loop. Try this simple line of code for generating a 2 by 3 matrix of random numbers with mean 0 and standard deviation 1.

The syntax is :

import numpy

numpy.random.normal(mean, standard deviation, (rows,columns))

example :

numpy.random.normal(0,1,(2,3))

2 Comments

Usually you want your random numbers randomly distributed, not clustered about a mean as in a normal distribution.
So try instead: "numpy.random.uniform(low=0.0, high=1.0, size=None)¶ [...] Samples are uniformly distributed over the half-open interval [low, high)"
1
import numpy as np  #np is shortcut of numpy

#Syntax : np.random.randint(the range for ex if you choose 100 then your array elements will be within the range 0 to 100, size = (row size, col size)

a = np.random.randint(100, size = (5,4)) #a is a variable(object)
print(a)

OUTPUT

[[49 81 57 96]
 [64 95 54 53]
 [63 77 68 74]
 [96 38 29 41]
 [13 39 99 43]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.