Actual source code: rand48.c
petsc-3.7.3 2016-08-01
1: #define PETSC_DESIRE_FEATURE_TEST_MACROS /* for drand48 with c89 */
2: #include <../src/sys/classes/random/randomimpl.h>
6: PetscErrorCode PetscRandomSeed_Rand48(PetscRandom r)
7: {
9: srand48(r->seed);
10: return(0);
11: }
15: PetscErrorCode PetscRandomGetValue_Rand48(PetscRandom r,PetscScalar *val)
16: {
18: #if defined(PETSC_USE_COMPLEX)
19: if (r->iset) {
20: *val = PetscRealPart(r->width)*(PetscReal)drand48() + PetscRealPart(r->low) + (PetscImaginaryPart(r->width)*(PetscReal)drand48() + PetscImaginaryPart(r->low)) * PETSC_i;
21: } else {
22: *val = (PetscReal)drand48() + (PetscReal)drand48()*PETSC_i;
23: }
24: #else
25: if (r->iset) *val = r->width * drand48() + r->low;
26: else *val = drand48();
27: #endif
28: return(0);
29: }
33: PetscErrorCode PetscRandomGetValueReal_Rand48(PetscRandom r,PetscReal *val)
34: {
36: #if defined(PETSC_USE_COMPLEX)
37: if (r->iset) *val = PetscRealPart(r->width)*drand48() + PetscRealPart(r->low);
38: else *val = drand48();
39: #else
40: if (r->iset) *val = r->width * drand48() + r->low;
41: else *val = drand48();
42: #endif
43: return(0);
44: }
46: static struct _PetscRandomOps PetscRandomOps_Values = {
47: /* 0 */
48: PetscRandomSeed_Rand48,
49: PetscRandomGetValue_Rand48,
50: PetscRandomGetValueReal_Rand48,
51: 0,
52: /* 5 */
53: 0
54: };
56: /*MC
57: PETSCRAND48 - access to the basic Unix drand48() random number generator
59: Options Database Keys:
60: . -random_type <rand,rand48,sprng>
62: Level: beginner
64: .seealso: RandomCreate(), RandomSetType(), PETSCRAND, PETSCSPRNG
65: M*/
69: PETSC_EXTERN PetscErrorCode PetscRandomCreate_Rand48(PetscRandom r)
70: {
74: PetscMemcpy(r->ops,&PetscRandomOps_Values,sizeof(PetscRandomOps_Values));
75: PetscObjectChangeTypeName((PetscObject)r,PETSCRAND48);
76: return(0);
77: }