Actual source code: rand48.c
1: #define PETSC_DLL
3: #include "../src/sys/random/randomimpl.h"
4: #include "petscfix.h"
5: #if defined (PETSC_HAVE_STDLIB_H)
6: #include <stdlib.h>
7: #endif
11: PetscErrorCode PetscRandomSeed_Rand48(PetscRandom r)
12: {
14: srand48(r->seed);
15: return(0);
16: }
20: PetscErrorCode PetscRandomGetValue_Rand48(PetscRandom r,PetscScalar *val)
21: {
23: #if defined(PETSC_USE_COMPLEX)
24: if (r->iset) {
25: *val = PetscRealPart(r->width)*drand48() + PetscRealPart(r->low) +
26: (PetscImaginaryPart(r->width)*drand48() + PetscImaginaryPart(r->low)) * PETSC_i;
27: } else {
28: *val = drand48() + drand48()*PETSC_i;
29: }
30: #else
31: if (r->iset) *val = r->width * drand48() + r->low;
32: else *val = drand48();
33: #endif
34: return(0);
35: }
39: PetscErrorCode PetscRandomGetValueReal_Rand48(PetscRandom r,PetscReal *val)
40: {
42: #if defined(PETSC_USE_COMPLEX)
43: if (r->iset) *val = PetscRealPart(r->width)*drand48() + PetscRealPart(r->low);
44: else *val = drand48();
45: #else
46: if (r->iset) *val = r->width * drand48() + r->low;
47: else *val = drand48();
48: #endif
49: return(0);
50: }
54: PetscErrorCode PetscRandomGetValueImaginary_Rand48(PetscRandom r,PetscScalar *val)
55: {
57: #if defined(PETSC_USE_COMPLEX)
58: if (r->iset) *val = (PetscImaginaryPart(r->width)*drand48()+PetscImaginaryPart(r->low))*PETSC_i;
59: else *val = drand48()*PETSC_i;
60: #else
61: if (r->iset) *val = r->width * drand48() + r->low;
62: else *val = drand48();
63: #endif
64: return(0);
65: }
67: static struct _PetscRandomOps PetscRandomOps_Values = {
68: /* 0 */
69: PetscRandomSeed_Rand48,
70: PetscRandomGetValue_Rand48,
71: PetscRandomGetValueReal_Rand48,
72: PetscRandomGetValueImaginary_Rand48,
73: 0,
74: /* 5 */
75: 0
76: };
78: /*MC
79: PETSCRAND48 - access to the basic Unix drand48() random number generator
81: Options Database Keys:
82: . -random_type <rand,rand48,sprng>
84: Level: beginner
86: .seealso: RandomCreate(), RandomSetType(), PETSCRAND, PETSCSPRNG
87: M*/
92: PetscErrorCode PetscRandomCreate_Rand48(PetscRandom r)
93: {
97: PetscMemcpy(r->ops,&PetscRandomOps_Values,sizeof(PetscRandomOps_Values));
98: /* r->bops->publish = PetscRandomPublish; */
99: /* r->petscnative = PETSC_TRUE; */
101: PetscObjectChangeTypeName((PetscObject)r,PETSCRAND48);
102: PetscPublishAll(r);
103: return(0);
104: }