Date: 2022-03-31T04:46:03+00:00

Actual source code: rand.c

      2: #include <petsc/private/randomimpl.h>

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

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

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

     35: static struct _PetscRandomOps PetscRandomOps_Values = {
     36:   PetscDesignatedInitializer(seed,PetscRandomSeed_Rand),
     37:   PetscDesignatedInitializer(getvalue,PetscRandomGetValue_Rand),
     38:   PetscDesignatedInitializer(getvaluereal,PetscRandomGetValueReal_Rand),
     39: };

     41: /*MC
     42:    PETSCRAND - access to the basic Unix random number generator

     44:    Options Database Keys:
     45: . -random_type <rand,rand48,sprng>

     47:   Level: beginner

     49: .seealso: PetscRandomCreate(), PetscRandomSetType(), PETSCRAND48, PETSCSPRNG
     50: M*/

     52: PETSC_EXTERN PetscErrorCode PetscRandomCreate_Rand(PetscRandom r)
     53: {
     54:   PetscMemcpy(r->ops,&PetscRandomOps_Values,sizeof(PetscRandomOps_Values));
     55:   PetscObjectChangeTypeName((PetscObject)r,PETSCRAND);
     56:   return 0;
     57: }