Actual source code: rand.c

petsc-3.7.3 2016-08-01
Report Typos and Errors
  2: #include <../src/sys/classes/random/randomimpl.h>

  6: PetscErrorCode  PetscRandomSeed_Rand(PetscRandom r)
  7: {
  9:   srand(r->seed);
 10:   return(0);
 11: }

 13: #define RAND_WRAP ((PetscReal)((rand()/(double)((unsigned int)RAND_MAX+1))))
 16: PetscErrorCode  PetscRandomGetValue_Rand(PetscRandom r,PetscScalar *val)
 17: {
 19: #if defined(PETSC_USE_COMPLEX)
 20:   if (r->iset) *val = PetscRealPart(r->width)*RAND_WRAP + PetscRealPart(r->low) + (PetscImaginaryPart(r->width)*RAND_WRAP + PetscImaginaryPart(r->low)) * PETSC_i;
 21:   else *val = RAND_WRAP + RAND_WRAP*PETSC_i;
 22: #else
 23:   if (r->iset) *val = r->width * RAND_WRAP + r->low;
 24:   else         *val = RAND_WRAP;
 25: #endif
 26:   return(0);
 27: }

 31: PetscErrorCode  PetscRandomGetValueReal_Rand(PetscRandom r,PetscReal *val)
 32: {
 34: #if defined(PETSC_USE_COMPLEX)
 35:     if (r->iset) *val = PetscRealPart(r->width)*RAND_WRAP + PetscRealPart(r->low);
 36:     else         *val = RAND_WRAP;
 37: #else
 38:   if (r->iset) *val = r->width * RAND_WRAP + r->low;
 39:   else         *val = RAND_WRAP;
 40: #endif
 41:   return(0);
 42: }

 44: static struct _PetscRandomOps PetscRandomOps_Values = {
 45:   /* 0 */
 46:   PetscRandomSeed_Rand,
 47:   PetscRandomGetValue_Rand,
 48:   PetscRandomGetValueReal_Rand,
 49:   0,
 50:   /* 5 */
 51:   0
 52: };

 54: /*MC
 55:    PETSCRAND - access to the basic Unix random number generator

 57:    Options Database Keys:
 58: . -random_type <rand,rand48,sprng>

 60:   Level: beginner

 62: .seealso: RandomCreate(), RandomSetType(), PETSCRAND48, PETSCSPRNG
 63: M*/

 67: PETSC_EXTERN PetscErrorCode PetscRandomCreate_Rand(PetscRandom r)
 68: {

 72:   PetscMemcpy(r->ops,&PetscRandomOps_Values,sizeof(PetscRandomOps_Values));
 73:   PetscObjectChangeTypeName((PetscObject)r,PETSCRAND);
 74:   return(0);
 75: }