Actual source code: rand.c
petsc-3.14.6 2021-03-30
2: #include <../src/sys/classes/random/randomimpl.h>
4: PetscErrorCode PetscRandomSeed_Rand(PetscRandom r)
5: {
7: srand(r->seed);
8: return(0);
9: }
11: #define RAND_WRAP ((PetscReal)((rand()/(double)((unsigned int)RAND_MAX+1))))
12: PetscErrorCode PetscRandomGetValue_Rand(PetscRandom r,PetscScalar *val)
13: {
15: #if defined(PETSC_USE_COMPLEX)
16: if (r->iset) *val = PetscRealPart(r->width)*RAND_WRAP + PetscRealPart(r->low) + (PetscImaginaryPart(r->width)*RAND_WRAP + PetscImaginaryPart(r->low)) * PETSC_i;
17: else *val = RAND_WRAP + RAND_WRAP*PETSC_i;
18: #else
19: if (r->iset) *val = r->width * RAND_WRAP + r->low;
20: else *val = RAND_WRAP;
21: #endif
22: return(0);
23: }
25: PetscErrorCode PetscRandomGetValueReal_Rand(PetscRandom r,PetscReal *val)
26: {
28: #if defined(PETSC_USE_COMPLEX)
29: if (r->iset) *val = PetscRealPart(r->width)*RAND_WRAP + PetscRealPart(r->low);
30: else *val = RAND_WRAP;
31: #else
32: if (r->iset) *val = r->width * RAND_WRAP + r->low;
33: else *val = RAND_WRAP;
34: #endif
35: return(0);
36: }
38: static struct _PetscRandomOps PetscRandomOps_Values = {
39: /* 0 */
40: PetscRandomSeed_Rand,
41: PetscRandomGetValue_Rand,
42: PetscRandomGetValueReal_Rand,
43: NULL,
44: /* 5 */
45: NULL
46: };
48: /*MC
49: PETSCRAND - access to the basic Unix random number generator
51: Options Database Keys:
52: . -random_type <rand,rand48,sprng>
54: Level: beginner
56: .seealso: RandomCreate(), RandomSetType(), PETSCRAND48, PETSCSPRNG
57: M*/
59: PETSC_EXTERN PetscErrorCode PetscRandomCreate_Rand(PetscRandom r)
60: {
64: PetscMemcpy(r->ops,&PetscRandomOps_Values,sizeof(PetscRandomOps_Values));
65: PetscObjectChangeTypeName((PetscObject)r,PETSCRAND);
66: return(0);
67: }