# 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
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/sys/classes/random/interface/random.c.html#PetscRandomGetValue">src/sys/classes/random/interface/random.c</A>

## Examples
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/sys/classes/random/tutorials/ex1.c.html">src/sys/classes/random/tutorials/ex1.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/sys/classes/random/tutorials/ex2.c.html">src/sys/classes/random/tutorials/ex2.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/vec/vec/utils/tagger/tutorials/ex1.c.html">src/vec/vec/utils/tagger/tutorials/ex1.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/mat/tutorials/ex2.c.html">src/mat/tutorials/ex2.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/dm/field/tutorials/ex1.c.html">src/dm/field/tutorials/ex1.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex11.c.html">src/ksp/ksp/tutorials/ex11.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/ksp/ksp/tutorials/ex11f.F90.html">src/ksp/ksp/tutorials/ex11f.F90.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/tao/unconstrained/tutorials/spectraladjointassimilation.c.html">src/tao/unconstrained/tutorials/spectraladjointassimilation.c.html</A><BR>

## Implementations

<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/sys/classes/random/impls/rand/rand.c.html#PetscRandomGetValue_Rand">PetscRandomGetValue_Rand in src/sys/classes/random/impls/rand/rand.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/sys/classes/random/impls/rand48/rand48.c.html#PetscRandomGetValue_Rand48">PetscRandomGetValue_Rand48 in src/sys/classes/random/impls/rand48/rand48.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/sys/classes/random/impls/rander48/rander48.c.html#PetscRandomGetValue_Rander48">PetscRandomGetValue_Rander48 in src/sys/classes/random/impls/rander48/rander48.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/sys/classes/random/impls/random123/random123.c.html#PetscRandomGetValue_Random123">PetscRandomGetValue_Random123 in src/sys/classes/random/impls/random123/random123.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/sys/classes/random/impls/sprng/sprng.c.html#PetscRandomGetValue_Sprng">PetscRandomGetValue_Sprng in src/sys/classes/random/impls/sprng/sprng.c</A><BR>


---
[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](/docs/manualpages/index.md)  
[Index of all manual pages](/docs/manualpages/singleindex.md)  
