Skip to main content
`Ctrl`+`K`

# 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

          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/mat/tutorials/ex2.c
src/tao/unconstrained/tutorials/spectraladjointassimilation.c
src/vec/vec/utils/tagger/tutorials/ex1.c
src/ksp/ksp/tutorials/ex11f.F90
src/sys/classes/random/tutorials/ex1.c
src/sys/classes/random/tutorials/ex2.c
src/dm/field/tutorials/ex1.c
src/ksp/ksp/tutorials/ex11.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

Index of all Sys routines
Table of Contents for all manual pages
Index of all manual pages