# [SciPy-user] Random sparse matrices

David Warde-Farley dwf@cs.toronto....
Tue Apr 22 14:30:49 CDT 2008

```I think the basic algorithm you want is to draw Z nonnegative integers from [0,M) and [0,N) respectively, use those as the indices and create a sparsecoo_matrix from that. I believe there's a way to sample integers with replacement built in to numpy; check help(numpy.random).

This has a slight chance of producing duplicate row/col pairs so you'll end up with less than Z nonzero elements. Another strategy would be to sample on [0,M*N) without replacement and use integer division by M to get goes and modulus by M to get cols.

Cheers,

DWF

-----Original Message-----
From: Mico Filós <elmico.filos@gmail.com>
Sent: April 22, 2008 2:32 PM
To: SciPy Users List <scipy-user@scipy.org>
Subject: Re: [SciPy-user] Random sparse matrices

Well, that's not exactly what I want. Randomness is not only in the
values of the non-empty elements, but also in the position (i,j) of
these non-empty elements. The idea is to draw randomly a fraction of
the M*N possible elements in the matrix (M and N are the number of
rows and columns), and assign to each of these elements a normal
random number.
_______________________________________________
SciPy-user mailing list
SciPy-user@scipy.org
http://projects.scipy.org/mailman/listinfo/scipy-user

```