:orphan: # MatSetRandom Sets all components of a matrix to random numbers. ## Synopsis ``` #include "petscmat.h" PetscErrorCode MatSetRandom(Mat x, PetscRandom rctx) ``` Logically Collective ## Input Parameters - ***x -*** the matrix - ***rctx -*** the `PetscRandom` object, formed by `PetscRandomCreate()`, or `NULL` and it will create one internally. ## Example ```none PetscRandomCreate(PETSC_COMM_WORLD,&rctx); MatSetRandom(x,rctx); PetscRandomDestroy(rctx); ``` ## Notes For sparse matrices that have been preallocated but not been assembled it randomly selects appropriate locations, for sparse matrices that already have locations it fills the locations with random numbers. It generates an error if used on sparse matrices that have not been preallocated. ## See Also [](ch_matrices), `Mat`, `PetscRandom`, `PetscRandomCreate()`, `MatZeroEntries()`, `MatSetValues()`, `PetscRandomCreate()`, `PetscRandomDestroy()` ## Level intermediate ## Location src/mat/interface/matrix.c ## Examples src/ksp/ksp/tutorials/ex76.c
src/ksp/ksp/tutorials/ex76f.F90
src/ksp/ksp/tutorials/ex77.c
src/ksp/ksp/tutorials/ex77f.F90
src/ksp/ksp/tutorials/ex79.c
src/ksp/ksp/tutorials/ex81.c
src/mat/tutorials/ex9.c
src/tao/tutorials/ex4.c
## Implementations MatSetRandom_MPIAIJ in src/mat/impls/aij/mpi/mpiaij.c
MatSetRandom_SeqAIJ in src/mat/impls/aij/seq/aij.c
MatSetRandom_MPIDense in src/mat/impls/dense/mpi/mpidense.c
MatSetRandom_SeqDense in src/mat/impls/dense/seq/dense.c
MatSetRandom_Nest in src/mat/impls/nest/matnest.c
MatSetRandom_MPISELL in src/mat/impls/sell/mpi/mpisell.c
--- [Edit on GitLab](https://gitlab.com/petsc/petsc/-/edit/release/src/mat/interface/matrix.c) [Index of all Mat routines](index.md) [Table of Contents for all manual pages](/manualpages/index.md) [Index of all manual pages](/manualpages/singleindex.md)