xref: /petsc/src/sys/classes/random/impls/rand48/rand48.c (revision 3e1910f1ab6113d8365e15c6b8c907ccce7ce4ea)
1 
2 #include <../src/sys/classes/random/randomimpl.h>
3 
4 #undef __FUNCT__
5 #define __FUNCT__ "PetscRandomSeed_Rand48"
6 PetscErrorCode  PetscRandomSeed_Rand48(PetscRandom r)
7 {
8   PetscFunctionBegin;
9   srand48(r->seed);
10   PetscFunctionReturn(0);
11 }
12 
13 #undef __FUNCT__
14 #define __FUNCT__ "PetscRandomGetValue_Rand48"
15 PetscErrorCode  PetscRandomGetValue_Rand48(PetscRandom r,PetscScalar *val)
16 {
17   PetscFunctionBegin;
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   PetscFunctionReturn(0);
29 }
30 
31 #undef __FUNCT__
32 #define __FUNCT__ "PetscRandomGetValueReal_Rand48"
33 PetscErrorCode  PetscRandomGetValueReal_Rand48(PetscRandom r,PetscReal *val)
34 {
35   PetscFunctionBegin;
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   PetscFunctionReturn(0);
44 }
45 
46 static struct _PetscRandomOps PetscRandomOps_Values = {
47   /* 0 */
48   PetscRandomSeed_Rand48,
49   PetscRandomGetValue_Rand48,
50   PetscRandomGetValueReal_Rand48,
51   0,
52   /* 5 */
53   0
54 };
55 
56 /*MC
57    PETSCRAND48 - access to the basic Unix drand48() random number generator
58 
59    Options Database Keys:
60 . -random_type <rand,rand48,sprng>
61 
62   Level: beginner
63 
64 .seealso: RandomCreate(), RandomSetType(), PETSCRAND, PETSCSPRNG
65 M*/
66 
67 #undef __FUNCT__
68 #define __FUNCT__ "PetscRandomCreate_Rand48"
69 PETSC_EXTERN PetscErrorCode PetscRandomCreate_Rand48(PetscRandom r)
70 {
71   PetscErrorCode ierr;
72 
73   PetscFunctionBegin;
74   ierr = PetscMemcpy(r->ops,&PetscRandomOps_Values,sizeof(PetscRandomOps_Values));CHKERRQ(ierr);
75   ierr = PetscObjectChangeTypeName((PetscObject)r,PETSCRAND48);CHKERRQ(ierr);
76   PetscFunctionReturn(0);
77 }
78