Actual source code: rand48.c
2: #include <../src/sys/random/randomimpl.h>
3: #if defined (PETSC_HAVE_STDLIB_H)
4: #include <stdlib.h>
5: #endif
9: PetscErrorCode PetscRandomSeed_Rand48(PetscRandom r)
10: {
12: srand48(r->seed);
13: return(0);
14: }
18: PetscErrorCode PetscRandomGetValue_Rand48(PetscRandom r,PetscScalar *val)
19: {
21: #if defined(PETSC_USE_COMPLEX)
22: if (r->iset) {
23: *val = PetscRealPart(r->width)*(PetscReal)drand48() + PetscRealPart(r->low) +
24: (PetscImaginaryPart(r->width)*(PetscReal)drand48() + PetscImaginaryPart(r->low)) * PETSC_i;
25: } else {
26: *val = (PetscReal)drand48() + (PetscReal)drand48()*PETSC_i;
27: }
28: #else
29: if (r->iset) *val = r->width * drand48() + r->low;
30: else *val = drand48();
31: #endif
32: return(0);
33: }
37: PetscErrorCode PetscRandomGetValueReal_Rand48(PetscRandom r,PetscReal *val)
38: {
40: #if defined(PETSC_USE_COMPLEX)
41: if (r->iset) *val = PetscRealPart(r->width)*drand48() + PetscRealPart(r->low);
42: else *val = drand48();
43: #else
44: if (r->iset) *val = r->width * drand48() + r->low;
45: else *val = drand48();
46: #endif
47: return(0);
48: }
50: static struct _PetscRandomOps PetscRandomOps_Values = {
51: /* 0 */
52: PetscRandomSeed_Rand48,
53: PetscRandomGetValue_Rand48,
54: PetscRandomGetValueReal_Rand48,
55: 0,
56: /* 5 */
57: 0
58: };
60: /*MC
61: PETSCRAND48 - access to the basic Unix drand48() random number generator
63: Options Database Keys:
64: . -random_type <rand,rand48,sprng>
66: Level: beginner
68: .seealso: RandomCreate(), RandomSetType(), PETSCRAND, PETSCSPRNG
69: M*/
74: PetscErrorCode PetscRandomCreate_Rand48(PetscRandom r)
75: {
79: PetscMemcpy(r->ops,&PetscRandomOps_Values,sizeof(PetscRandomOps_Values));
80: /* r->bops->publish = PetscRandomPublish; */
81: /* r->petscnative = PETSC_TRUE; */
83: PetscObjectChangeTypeName((PetscObject)r,PETSCRAND48);
84: return(0);
85: }