# PetscRandomCreate
Creates a context for generating random numbers, and initializes the random-number generator. 
## Synopsis
```
#include "petscsys.h" 
PetscErrorCode PetscRandomCreate(MPI_Comm comm, PetscRandom *r)
```
Collective


## Input Parameters

- ***comm -*** MPI communicator



## Output Parameter

- ***r  -*** the random number generator context





## Notes
The random type has to be set by `PetscRandomSetType()`.

This is only a primitive "parallel" random number generator, it should NOT
be used for sophisticated parallel Monte Carlo methods since it will very likely
not have the correct statistics across processors. You can provide your own
parallel generator using PetscRandomRegister();

If you create a `PetscRandom()` using `PETSC_COMM_SELF` on several processors then
the SAME random numbers will be generated on all those processors. Use `PETSC_COMM_WORLD`
or the appropriate parallel communicator to eliminate this issue.

Use `VecSetRandom()` to set the elements of a vector to random numbers.


## Example of Usage
```none
      PetscRandomCreate(PETSC_COMM_SELF,&r);
      PetscRandomSetType(r,PETSCRAND48);
      PetscRandomGetValue(r,&value1);
      PetscRandomGetValueReal(r,&value2);
      PetscRandomDestroy(&r);
```



## See Also
 `PetscRandomSetType()`, `PetscRandomGetValue()`, `PetscRandomGetValueReal()`, `PetscRandomSetInterval()`,
`PetscRandomDestroy()`, `VecSetRandom()`, `PetscRandomType`, `PetscRandom`

## Level
intermediate

## Location
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/sys/classes/random/interface/randomc.c.html#PetscRandomCreate">src/sys/classes/random/interface/randomc.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/vec/vec/tutorials/ex19.c.html">src/vec/vec/tutorials/ex19.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/vec/vec/tutorials/performance.c.html">src/vec/vec/tutorials/performance.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/mat/tutorials/ex9.c.html">src/mat/tutorials/ex9.c.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/dm/tutorials/swarm_ex3.c.html">src/dm/tutorials/swarm_ex3.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/pc/tutorials/ex3.c.html">src/ksp/pc/tutorials/ex3.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>

## Implementations

<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/sys/classes/random/impls/curand/curand.c.html#PetscRandomCreate_CURAND">PetscRandomCreate_CURAND in src/sys/classes/random/impls/curand/curand.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/sys/classes/random/impls/rand/rand.c.html#PetscRandomCreate_Rand">PetscRandomCreate_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#PetscRandomCreate_Rand48">PetscRandomCreate_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#PetscRandomCreate_Rander48">PetscRandomCreate_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#PetscRandomCreate_Random123">PetscRandomCreate_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#PetscRandomCreate_Sprng">PetscRandomCreate_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/randomc.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)  
