:orphan:
# PetscRandomGetValue
Generates a random number. Call this after first calling `PetscRandomCreate()`.
## Synopsis
```
#include "petscsys.h"
PetscErrorCode PetscRandomGetValue(PetscRandom r, PetscScalar *val)
```
Not Collective
## Input Parameter
- ***r -*** the random number generator context
## Output Parameter
- ***val -*** the value
## Notes
Use `VecSetRandom()` to set the elements of a vector to random numbers.
When PETSc is compiled for complex numbers this returns a complex number with random real and complex parts.
Use `PetscRandomGetValueReal()` to get a random real number.
To get a complex number with only a random real part, first call `PetscRandomSetInterval()` with a equal
low and high imaginary part. Similarly to get a complex number with only a random imaginary part call
`PetscRandomSetInterval()` with a equal low and high real part.
## Example of Usage
```none
PetscRandomCreate(PETSC_COMM_WORLD,&r);
PetscRandomGetValue(r,&value1);
PetscRandomGetValue(r,&value2);
PetscRandomGetValue(r,&value3);
PetscRandomDestroy(&r);
```
## See Also
`PetscRandom`, `PetscRandomCreate()`, `PetscRandomDestroy()`, `VecSetRandom()`, `PetscRandomGetValueReal()`, `PetscRandomSetInterval()`
## Level
intermediate
## Location
src/sys/classes/random/interface/random.c
## Examples
src/dm/field/tutorials/ex1.c
src/ksp/ksp/tutorials/ex11.c
src/ksp/ksp/tutorials/ex11f.F90
src/mat/tutorials/ex2.c
src/sys/classes/random/tutorials/ex1.c
src/sys/classes/random/tutorials/ex2.c
src/tao/unconstrained/tutorials/spectraladjointassimilation.c
src/vec/vec/utils/tagger/tutorials/ex1.c
## Implementations
PetscRandomGetValue_Rand in src/sys/classes/random/impls/rand/rand.c
PetscRandomGetValue_Rand48 in src/sys/classes/random/impls/rand48/rand48.c
PetscRandomGetValue_Rander48 in src/sys/classes/random/impls/rander48/rander48.c
PetscRandomGetValue_Random123 in src/sys/classes/random/impls/random123/random123.c
PetscRandomGetValue_Sprng in src/sys/classes/random/impls/sprng/sprng.c
---
[Edit on GitLab](https://gitlab.com/petsc/petsc/-/edit/release/src/sys/classes/random/interface/random.c)
[Index of all Sys routines](index.md)
[Table of Contents for all manual pages](/manualpages/index.md)
[Index of all manual pages](/manualpages/singleindex.md)