# 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 on MPI_Comm

### 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 primative "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

          PetscRandomCreate(PETSC_COMM_SELF,&r);
          PetscRandomSetType(r,PETSCRAND48);
          PetscRandomGetValue(r,&value1);
          PetscRandomGetValueReal(r,&value2);
          PetscRandomGetValueImaginary(r,&value3);
          PetscRandomDestroy(r);

### See Also

PetscRandomSetType(), PetscRandomGetValue(), PetscRandomGetValueReal(), PetscRandomGetValueImaginary(), PetscRandomSetInterval(), PetscRandomDestroy(), VecSetRandom()

Level:intermediate
Location:src/sys/random/interface/random.c
Index of all Sys routines
Table of Contents for all manual pages
Index of all manual pages

### Examples

src/ksp/ksp/examples/tutorials/ex2.c.html
src/ksp/ksp/examples/tutorials/ex4.c.html
src/ksp/ksp/examples/tutorials/ex11.c.html
src/ksp/ksp/examples/tutorials/ex2f.F.html
src/ksp/ksp/examples/tutorials/ex11f.F.html