Actual source code: sprng.c
2: #include <../src/sys/random/randomimpl.h>
3: #if defined (PETSC_HAVE_STDLIB_H)
4: #include <stdlib.h>
5: #endif
7: #define USE_MPI
8: #define SIMPLE_SPRNG
10: #include <sprng.h>
15: PetscErrorCode PetscRandomSeed_Sprng(PetscRandom r)
16: {
18: init_sprng(r->seed,SPRNG_DEFAULT);
19: return(0);
20: }
24: PetscErrorCode PetscRandomGetValue_Sprng(PetscRandom r,PetscScalar *val)
25: {
27: #if defined(PETSC_USE_COMPLEX)
28: if (r->iset) {
29: *val = PetscRealPart(r->width)*sprng() + PetscRealPart(r->low) +
30: (PetscImaginaryPart(r->width)*sprng() + PetscImaginaryPart(r->low)) * PETSC_i;
31: } else {
32: *val = sprng() + sprng()*PETSC_i;
33: }
34: #else
35: if (r->iset) *val = r->width * sprng() + r->low;
36: else *val = sprng();
37: #endif
38: return(0);
39: }
43: PetscErrorCode PetscRandomGetValueReal_Sprng(PetscRandom r,PetscScalar *val)
44: {
46: #if defined(PETSC_USE_COMPLEX)
47: if (r->iset) *val = PetscRealPart(r->width)*sprng() + PetscRealPart(r->low);
48: else *val = sprng();
49: #else
50: if (r->iset) *val = r->width * sprng() + r->low;
51: else *val = sprng();
52: #endif
53: return(0);
54: }
56: static struct _PetscRandomOps PetscRandomOps_Values = {
57: /* 0 */
58: PetscRandomSeed_Sprng,
59: PetscRandomGetValue_Sprng,
60: PetscRandomGetValueReal_Sprng,
61: 0,
62: /* 5 */
63: 0
64: };
66: /*MC
67: PETSCSPRNG- access to the publically available random number generator sprng
69: Options Database Keys:
70: . -random_type <rand,rand48,sprng>
72: Level: beginner
74: PETSc must have been ./configure with the option --download-sprng to use
75: this random number generator.
77: This is NOT currently using a parallel random number generator. Sprng does have
78: an MPI version we should investigate.
80: .seealso: RandomCreate(), RandomSetType(), PETSCRAND, PETSCRAND48
81: M*/
86: PetscErrorCode PetscRandomCreate_Sprng(PetscRandom r)
87: {
91: PetscMemcpy(r->ops,&PetscRandomOps_Values,sizeof(PetscRandomOps_Values));
92: PetscObjectChangeTypeName((PetscObject)r,PETSCSPRNG);
93: return(0);
94: }