Actual source code: rand48.c

  1: #define PETSC_DESIRE_FEATURE_TEST_MACROS /* for drand48() */
  2: #include <petsc/private/randomimpl.h>

  4: PetscErrorCode PetscRandomSeed_Rand48(PetscRandom r)
  5: {
  6:   srand48(r->seed);
  7:   return 0;
  8: }

 10: PetscErrorCode PetscRandomGetValue_Rand48(PetscRandom r, PetscScalar *val)
 11: {
 12: #if defined(PETSC_USE_COMPLEX)
 13:   if (r->iset) {
 14:     *val = PetscRealPart(r->width) * (PetscReal)drand48() + PetscRealPart(r->low) + (PetscImaginaryPart(r->width) * (PetscReal)drand48() + PetscImaginaryPart(r->low)) * PETSC_i;
 15:   } else {
 16:     *val = (PetscReal)drand48() + (PetscReal)drand48() * PETSC_i;
 17:   }
 18: #else
 19:   if (r->iset) *val = r->width * drand48() + r->low;
 20:   else *val = drand48();
 21: #endif
 22:   return 0;
 23: }

 25: PetscErrorCode PetscRandomGetValueReal_Rand48(PetscRandom r, PetscReal *val)
 26: {
 27: #if defined(PETSC_USE_COMPLEX)
 28:   if (r->iset) *val = PetscRealPart(r->width) * drand48() + PetscRealPart(r->low);
 29:   else *val = drand48();
 30: #else
 31:   if (r->iset) *val = r->width * drand48() + r->low;
 32:   else *val = drand48();
 33: #endif
 34:   return 0;
 35: }

 37: static struct _PetscRandomOps PetscRandomOps_Values = {
 38:   PetscDesignatedInitializer(seed, PetscRandomSeed_Rand48),
 39:   PetscDesignatedInitializer(getvalue, PetscRandomGetValue_Rand48),
 40:   PetscDesignatedInitializer(getvaluereal, PetscRandomGetValueReal_Rand48),
 41: };

 43: /*MC
 44:    PETSCRAND48 - access to the basic Unix drand48() random number generator

 46:    Options Database Keys:
 47: . -random_type <rand,rand48,sprng> - select the random number generator at runtime

 49:   Level: beginner

 51:   Note:
 52:   Not recommended because it may produce different results on different systems.

 54: .seealso: `PetscRandomCreate()`, `PetscRandomSetType()`, `PETSCRAND`, `PETSCSPRNG`, `PetscRandomSetFromOptions()`
 55: M*/

 57: PETSC_EXTERN PetscErrorCode PetscRandomCreate_Rand48(PetscRandom r)
 58: {
 59:   PetscMemcpy(r->ops, &PetscRandomOps_Values, sizeof(PetscRandomOps_Values));
 60:   PetscObjectChangeTypeName((PetscObject)r, PETSCRAND48);
 61:   return 0;
 62: }