:orphan:
# VecSetRandom
Sets all components of a vector to random numbers.
## Synopsis
```
#include "petscvec.h"
PetscErrorCode VecSetRandom(Vec x, PetscRandom rctx)
```
Logically Collective
## Input Parameters
- ***x -*** the vector
- ***rctx -*** the random number context, formed by `PetscRandomCreate()`, or use `NULL` and it will create one internally.
## Output Parameter
- ***x -*** the vector
## Example of Usage
```none
PetscRandomCreate(PETSC_COMM_WORLD,&rctx);
VecSetRandom(x,rctx);
PetscRandomDestroy(&rctx);
```
## See Also
[](ch_vectors), `Vec`, `VecSet()`, `VecSetValues()`, `PetscRandomCreate()`, `PetscRandomDestroy()`
## Level
intermediate
## Location
src/vec/vec/interface/vector.c
## Examples
src/ksp/ksp/tutorials/ex10.c
src/ksp/ksp/tutorials/ex11.c
src/ksp/ksp/tutorials/ex11f.F90
src/ksp/ksp/tutorials/ex18.c
src/ksp/ksp/tutorials/ex2f.F90
src/ksp/ksp/tutorials/ex46.c
src/ksp/ksp/tutorials/ex5.c
src/ksp/ksp/tutorials/ex52.c
src/ksp/ksp/tutorials/ex57f.F90
src/ksp/ksp/tutorials/ex59.c
src/ksp/ksp/tutorials/ex5f.F90
## Implementations
VecSetRandom_Nest in src/vec/vec/impls/nest/vecnest.c
VecSetRandom_Seq in src/vec/vec/impls/seq/bvec2.c
VecSetRandom_SeqKokkos in src/vec/vec/impls/seq/kokkos/veckok.kokkos.cxx
VecSetRandom_SeqViennaCL in src/vec/vec/impls/seq/seqviennacl/vecviennacl.cxx
---
[Edit on GitLab](https://gitlab.com/petsc/petsc/-/edit/release/src/vec/vec/interface/vector.c)
[Index of all Vec routines](index.md)
[Table of Contents for all manual pages](/manualpages/index.md)
[Index of all manual pages](/manualpages/singleindex.md)