# random¶

`sparse.``random`(shape, density=None, nnz=None, random_state=None, data_rvs=None, format='coo', compressed_axes=None, fill_value=None, idx_dtype=None)[source]

Generate a random sparse multidimensional array

Parameters
• shape (Tuple[int]) – Shape of the array

• density (float, optional) – Density of the generated array; default is 0.01. Mutually exclusive with nnz.

• nnz (int, optional) – Number of nonzero elements in the generated array. Mutually exclusive with density.

• random_state (Union[numpy.random.RandomState, int], optional) – Random number generator or random seed. If not given, the singleton numpy.random will be used. This random state will be used for sampling the sparsity structure, but not necessarily for sampling the values of the structurally nonzero entries of the matrix.

• data_rvs (Callable) – Data generation callback. Must accept one single parameter: number of `nnz` elements, and return one single NumPy array of exactly that length.

• format (str) – The format to return the output array in.

• fill_value (scalar) – The fill value of the output array.

Returns

The generated random matrix.

Return type

SparseArray

`scipy.sparse.rand`

Equivalent Scipy function.

`numpy.random.rand`

Similar Numpy function.

Examples

```>>> from sparse import random
>>> from scipy import stats
>>> rvs = lambda x: stats.poisson(25, loc=10).rvs(x, random_state=np.random.RandomState(1))
>>> s = random((2, 3, 4), density=0.25, random_state=np.random.RandomState(1), data_rvs=rvs)
>>> s.todense()
array([[[ 0,  0,  0,  0],
[ 0, 34,  0,  0],
[33, 34,  0, 29]],

[[30,  0,  0, 34],
[ 0,  0,  0,  0],
[ 0,  0,  0,  0]]])
```