diff options
author | Alberto Ruiz <aruiz@um.es> | 2014-12-01 13:35:54 +0100 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2014-12-01 13:35:54 +0100 |
commit | b774669268341acc22f352f845b6bc34f0f6d418 (patch) | |
tree | 3a6aad370875259079662787a82782a26a8f32a3 /packages/base/src/C | |
parent | ebba19f190e629adf71f0baeb67de8d8e2bb8f98 (diff) |
change rand() to drand48_r
(I have tried also random_r but got segfaults on exit) (?)
Diffstat (limited to 'packages/base/src/C')
-rw-r--r-- | packages/base/src/C/vector-aux.c | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/packages/base/src/C/vector-aux.c b/packages/base/src/C/vector-aux.c index a7eaa08..d5b6d4d 100644 --- a/packages/base/src/C/vector-aux.c +++ b/packages/base/src/C/vector-aux.c | |||
@@ -700,8 +700,15 @@ int saveMatrix(char * file, char * format, KDMAT(a)){ | |||
700 | 700 | ||
701 | //////////////////////////////////////////////////////////////////////////////// | 701 | //////////////////////////////////////////////////////////////////////////////// |
702 | 702 | ||
703 | inline double urandom(struct drand48_data * buffer) { | ||
704 | double res; | ||
705 | drand48_r(buffer,&res); | ||
706 | return res; | ||
707 | } | ||
708 | |||
709 | |||
703 | // http://c-faq.com/lib/gaussian.html | 710 | // http://c-faq.com/lib/gaussian.html |
704 | double gaussrand() | 711 | double gaussrand(struct drand48_data * buffer) |
705 | { | 712 | { |
706 | static double V1, V2, S; | 713 | static double V1, V2, S; |
707 | static int phase = 0; | 714 | static int phase = 0; |
@@ -709,8 +716,8 @@ double gaussrand() | |||
709 | 716 | ||
710 | if(phase == 0) { | 717 | if(phase == 0) { |
711 | do { | 718 | do { |
712 | double U1 = (double)rand() / RAND_MAX; | 719 | double U1 = urandom(buffer); |
713 | double U2 = (double)rand() / RAND_MAX; | 720 | double U2 = urandom(buffer); |
714 | 721 | ||
715 | V1 = 2 * U1 - 1; | 722 | V1 = 2 * U1 - 1; |
716 | V2 = 2 * U2 - 1; | 723 | V2 = 2 * U2 - 1; |
@@ -726,19 +733,20 @@ double gaussrand() | |||
726 | return X; | 733 | return X; |
727 | } | 734 | } |
728 | 735 | ||
729 | int random_vector(int seed, int code, DVEC(r)) { | 736 | int random_vector(unsigned int seed, int code, DVEC(r)) { |
730 | srand(seed); | 737 | struct drand48_data buffer; |
738 | srand48_r(seed,&buffer); | ||
731 | int k; | 739 | int k; |
732 | switch (code) { | 740 | switch (code) { |
733 | case 0: { // uniform | 741 | case 0: { // uniform |
734 | for (k=0; k<rn; k++) { | 742 | for (k=0; k<rn; k++) { |
735 | rp[k] = (double)rand()/RAND_MAX; | 743 | rp[k] = urandom(&buffer); |
736 | } | 744 | } |
737 | OK | 745 | OK |
738 | } | 746 | } |
739 | case 1: { // gaussian | 747 | case 1: { // gaussian |
740 | for (k=0; k<rn; k++) { | 748 | for (k=0; k<rn; k++) { |
741 | rp[k] = gaussrand(); | 749 | rp[k] = gaussrand(&buffer); |
742 | } | 750 | } |
743 | OK | 751 | OK |
744 | } | 752 | } |