1 2 #include <../src/sys/classes/random/randomimpl.h> 3 #if defined(PETSC_HAVE_STDLIB_H) 4 #include <stdlib.h> 5 #endif 6 7 #undef __FUNCT__ 8 #define __FUNCT__ "PetscRandomSeed_Rand48" 9 PetscErrorCode PetscRandomSeed_Rand48(PetscRandom r) 10 { 11 PetscFunctionBegin; 12 srand48(r->seed); 13 PetscFunctionReturn(0); 14 } 15 16 #undef __FUNCT__ 17 #define __FUNCT__ "PetscRandomGetValue_Rand48" 18 PetscErrorCode PetscRandomGetValue_Rand48(PetscRandom r,PetscScalar *val) 19 { 20 PetscFunctionBegin; 21 #if defined(PETSC_USE_COMPLEX) 22 if (r->iset) { 23 *val = PetscRealPart(r->width)*(PetscReal)drand48() + PetscRealPart(r->low) + (PetscImaginaryPart(r->width)*(PetscReal)drand48() + PetscImaginaryPart(r->low)) * PETSC_i; 24 } else { 25 *val = (PetscReal)drand48() + (PetscReal)drand48()*PETSC_i; 26 } 27 #else 28 if (r->iset) *val = r->width * drand48() + r->low; 29 else *val = drand48(); 30 #endif 31 PetscFunctionReturn(0); 32 } 33 34 #undef __FUNCT__ 35 #define __FUNCT__ "PetscRandomGetValueReal_Rand48" 36 PetscErrorCode PetscRandomGetValueReal_Rand48(PetscRandom r,PetscReal *val) 37 { 38 PetscFunctionBegin; 39 #if defined(PETSC_USE_COMPLEX) 40 if (r->iset) *val = PetscRealPart(r->width)*drand48() + PetscRealPart(r->low); 41 else *val = drand48(); 42 #else 43 if (r->iset) *val = r->width * drand48() + r->low; 44 else *val = drand48(); 45 #endif 46 PetscFunctionReturn(0); 47 } 48 49 static struct _PetscRandomOps PetscRandomOps_Values = { 50 /* 0 */ 51 PetscRandomSeed_Rand48, 52 PetscRandomGetValue_Rand48, 53 PetscRandomGetValueReal_Rand48, 54 0, 55 /* 5 */ 56 0 57 }; 58 59 /*MC 60 PETSCRAND48 - access to the basic Unix drand48() random number generator 61 62 Options Database Keys: 63 . -random_type <rand,rand48,sprng> 64 65 Level: beginner 66 67 .seealso: RandomCreate(), RandomSetType(), PETSCRAND, PETSCSPRNG 68 M*/ 69 70 EXTERN_C_BEGIN 71 #undef __FUNCT__ 72 #define __FUNCT__ "PetscRandomCreate_Rand48" 73 PetscErrorCode PetscRandomCreate_Rand48(PetscRandom r) 74 { 75 PetscErrorCode ierr; 76 77 PetscFunctionBegin; 78 ierr = PetscMemcpy(r->ops,&PetscRandomOps_Values,sizeof(PetscRandomOps_Values));CHKERRQ(ierr); 79 /* r->bops->publish = PetscRandomPublish; */ 80 /* r->petscnative = PETSC_TRUE; */ 81 82 ierr = PetscObjectChangeTypeName((PetscObject)r,PETSCRAND48);CHKERRQ(ierr); 83 PetscFunctionReturn(0); 84 } 85 EXTERN_C_END 86