summaryrefslogtreecommitdiff
path: root/packages
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2014-12-01 13:35:54 +0100
committerAlberto Ruiz <aruiz@um.es>2014-12-01 13:35:54 +0100
commitb774669268341acc22f352f845b6bc34f0f6d418 (patch)
tree3a6aad370875259079662787a82782a26a8f32a3 /packages
parentebba19f190e629adf71f0baeb67de8d8e2bb8f98 (diff)
change rand() to drand48_r
(I have tried also random_r but got segfaults on exit) (?)
Diffstat (limited to 'packages')
-rw-r--r--packages/base/THANKS.md6
-rw-r--r--packages/base/src/C/vector-aux.c22
2 files changed, 20 insertions, 8 deletions
diff --git a/packages/base/THANKS.md b/packages/base/THANKS.md
index fcbdb8e..90e6199 100644
--- a/packages/base/THANKS.md
+++ b/packages/base/THANKS.md
@@ -171,5 +171,9 @@ module reorganization, monadic mapVectorM, and many other improvements.
171 171
172- Niklas Hambüchen improved the documentation. 172- Niklas Hambüchen improved the documentation.
173 173
174- "erdeszt" optimized "conv" using a direct vector reverse 174- "erdeszt" optimized "conv" using a direct vector reverse.
175
176- John Shahbazian added support for openBLAS.
177
178- "yongqli" reported the bug in randomVector (rand() is not thread-safe).
175 179
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
703inline 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
704double gaussrand() 711double 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
729int random_vector(int seed, int code, DVEC(r)) { 736int 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 }