From d2ebf68716ad50afc58d008f562d4f960bb8f126 Mon Sep 17 00:00:00 2001 From: Kiwamu Ishikura Date: Sat, 13 Dec 2014 15:13:58 +0900 Subject: use random() instead of drand48_r drand48_r cannot be used in OS X, and also it uses linear congruential generators. --- packages/base/src/C/vector-aux.c | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) (limited to 'packages/base/src/C') diff --git a/packages/base/src/C/vector-aux.c b/packages/base/src/C/vector-aux.c index f8feb37..51bff73 100644 --- a/packages/base/src/C/vector-aux.c +++ b/packages/base/src/C/vector-aux.c @@ -700,24 +700,16 @@ int saveMatrix(char * file, char * format, KDMAT(a)){ //////////////////////////////////////////////////////////////////////////////// -inline double urandom(struct drand48_data * buffer) { - double res; - drand48_r(buffer,&res); - return res; -} - - // http://c-faq.com/lib/gaussian.html -double gaussrand(struct drand48_data *buffer, - int *phase, double *pV1, double *pV2, double *pS) +double gaussrand(int *phase, double *pV1, double *pV2, double *pS) { double V1=*pV1, V2=*pV2, S=*pS; double X; if(*phase == 0) { do { - double U1 = urandom(buffer); - double U2 = urandom(buffer); + double U1 = (double)random() / (double)RAND_MAX; + double U2 = (double)random() / (double)RAND_MAX; V1 = 2 * U1 - 1; V2 = 2 * U2 - 1; @@ -732,11 +724,10 @@ double gaussrand(struct drand48_data *buffer, *pV1=V1; *pV2=V2; *pS=S; return X; + } int random_vector(unsigned int seed, int code, DVEC(r)) { - struct drand48_data buffer; - srand48_r(seed,&buffer); int phase = 0; double V1,V2,S; @@ -744,13 +735,13 @@ int random_vector(unsigned int seed, int code, DVEC(r)) { switch (code) { case 0: { // uniform for (k=0; k Date: Mon, 29 Dec 2014 12:00:03 +0900 Subject: add random to add srandom to set seed --- packages/base/src/C/vector-aux.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'packages/base/src/C') diff --git a/packages/base/src/C/vector-aux.c b/packages/base/src/C/vector-aux.c index 51bff73..d01e788 100644 --- a/packages/base/src/C/vector-aux.c +++ b/packages/base/src/C/vector-aux.c @@ -730,8 +730,10 @@ double gaussrand(int *phase, double *pV1, double *pV2, double *pS) int random_vector(unsigned int seed, int code, DVEC(r)) { int phase = 0; double V1,V2,S; - int k; + + srandom(seed); + switch (code) { case 0: { // uniform for (k=0; k Date: Mon, 29 Dec 2014 14:19:11 +0900 Subject: to set srandom for OSX To set srandom to use seed for OS X --- packages/base/src/C/vector-aux.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'packages/base/src/C') diff --git a/packages/base/src/C/vector-aux.c b/packages/base/src/C/vector-aux.c index 0f1b3c7..1c3fe59 100644 --- a/packages/base/src/C/vector-aux.c +++ b/packages/base/src/C/vector-aux.c @@ -704,6 +704,11 @@ int saveMatrix(char * file, char * format, KDMAT(a)){ #pragma message "randomVector is not thread-safe in OSX" +inline double urandom() { + const long max_random = 2147483647 // 2**31 - 1 + return (double)random() / (double)max_random; +} + double gaussrand(int *phase, double *pV1, double *pV2, double *pS) { double V1=*pV1, V2=*pV2, S=*pS; @@ -711,8 +716,8 @@ double gaussrand(int *phase, double *pV1, double *pV2, double *pS) if(*phase == 0) { do { - double U1 = (double)random() / (double)RAND_MAX; - double U2 = (double)random() / (double)RAND_MAX; + double U1 = urandom(); + double U2 = urandom(); V1 = 2 * U1 - 1; V2 = 2 * U2 - 1; @@ -733,12 +738,14 @@ double gaussrand(int *phase, double *pV1, double *pV2, double *pS) int random_vector(unsigned int seed, int code, DVEC(r)) { int phase = 0; double V1,V2,S; + + srandom(seed); int k; switch (code) { case 0: { // uniform for (k=0; k Date: Mon, 29 Dec 2014 14:25:15 +0900 Subject: use __APPLE__ instead of OSX use __APPLE__ for predefined os detection OSX. --- packages/base/src/C/vector-aux.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/base/src/C') diff --git a/packages/base/src/C/vector-aux.c b/packages/base/src/C/vector-aux.c index 1c3fe59..ec6ec80 100644 --- a/packages/base/src/C/vector-aux.c +++ b/packages/base/src/C/vector-aux.c @@ -700,7 +700,7 @@ int saveMatrix(char * file, char * format, KDMAT(a)){ //////////////////////////////////////////////////////////////////////////////// -#ifdef OSX +#ifdef __APPLE__ #pragma message "randomVector is not thread-safe in OSX" -- cgit v1.2.3 From 692ffdc55e7aa7d6304b1ed782f8bf24c3c7b4e5 Mon Sep 17 00:00:00 2001 From: Kiwamu Ishikura Date: Mon, 29 Dec 2014 14:26:36 +0900 Subject: add semicoron add semicoron --- packages/base/src/C/vector-aux.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/base/src/C') diff --git a/packages/base/src/C/vector-aux.c b/packages/base/src/C/vector-aux.c index ec6ec80..dda47cb 100644 --- a/packages/base/src/C/vector-aux.c +++ b/packages/base/src/C/vector-aux.c @@ -705,7 +705,7 @@ int saveMatrix(char * file, char * format, KDMAT(a)){ #pragma message "randomVector is not thread-safe in OSX" inline double urandom() { - const long max_random = 2147483647 // 2**31 - 1 + const long max_random = 2147483647; // 2**31 - 1 return (double)random() / (double)max_random; } -- cgit v1.2.3