Actual source code: rand48.c
1: #define PETSC_DLL
3: #include "../src/sys/random/randomimpl.h"
4: #if defined (PETSC_HAVE_STDLIB_H)
5: #include <stdlib.h>
6: #endif
10: PetscErrorCode PetscRandomSeed_Rand48(PetscRandom r)
11: {
13: srand48(r->seed);
14: return(0);
15: }
19: PetscErrorCode PetscRandomGetValue_Rand48(PetscRandom r,PetscScalar *val)
20: {
22: #if defined(PETSC_USE_COMPLEX)
23: if (r->iset) {
24: *val = PetscRealPart(r->width)*drand48() + PetscRealPart(r->low) +
25: (PetscImaginaryPart(r->width)*drand48() + PetscImaginaryPart(r->low)) * PETSC_i;
26: } else {
27: *val = drand48() + drand48()*PETSC_i;
28: }
29: #else
30: if (r->iset) *val = r->width * drand48() + r->low;
31: else *val = drand48();
32: #endif
33: return(0);
34: }
38: PetscErrorCode PetscRandomGetValueReal_Rand48(PetscRandom r,PetscReal *val)
39: {
41: #if defined(PETSC_USE_COMPLEX)
42: if (r->iset) *val = PetscRealPart(r->width)*drand48() + PetscRealPart(r->low);
43: else *val = drand48();
44: #else
45: if (r->iset) *val = r->width * drand48() + r->low;
46: else *val = drand48();
47: #endif
48: return(0);
49: }
51: static struct _PetscRandomOps PetscRandomOps_Values = {
52: /* 0 */
53: PetscRandomSeed_Rand48,
54: PetscRandomGetValue_Rand48,
55: PetscRandomGetValueReal_Rand48,
56: 0,
57: /* 5 */
58: 0
59: };
61: /*MC
62: PETSCRAND48 - access to the basic Unix drand48() random number generator
64: Options Database Keys:
65: . -random_type <rand,rand48,sprng>
67: Level: beginner
69: .seealso: RandomCreate(), RandomSetType(), PETSCRAND, PETSCSPRNG
70: M*/
75: PetscErrorCode PetscRandomCreate_Rand48(PetscRandom r)
76: {
80: PetscMemcpy(r->ops,&PetscRandomOps_Values,sizeof(PetscRandomOps_Values));
81: /* r->bops->publish = PetscRandomPublish; */
82: /* r->petscnative = PETSC_TRUE; */
84: PetscObjectChangeTypeName((PetscObject)r,PETSCRAND48);
85: PetscPublishAll(r);
86: return(0);
87: }