summaryrefslogtreecommitdiff
path: root/packages/base/src/C
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2014-12-29 14:05:42 +0100
committerAlberto Ruiz <aruiz@um.es>2014-12-29 14:05:42 +0100
commit5b03c7d6cbc2d9d42f515d34a94e6739d59fd6bc (patch)
tree98435bb14a254187e50c191d9a7a43bd749884f3 /packages/base/src/C
parentcc38f9a8e6d51b2e96037375d338546cc606640d (diff)
parent692ffdc55e7aa7d6304b1ed782f8bf24c3c7b4e5 (diff)
Merge pull request #101 from i-kiwamu/use-random
use random() instead of drand48_r
Diffstat (limited to 'packages/base/src/C')
-rw-r--r--packages/base/src/C/vector-aux.c20
1 files changed, 14 insertions, 6 deletions
diff --git a/packages/base/src/C/vector-aux.c b/packages/base/src/C/vector-aux.c
index 946d069..dda47cb 100644
--- a/packages/base/src/C/vector-aux.c
+++ b/packages/base/src/C/vector-aux.c
@@ -700,10 +700,15 @@ int saveMatrix(char * file, char * format, KDMAT(a)){
700 700
701//////////////////////////////////////////////////////////////////////////////// 701////////////////////////////////////////////////////////////////////////////////
702 702
703#ifdef OSX 703#ifdef __APPLE__
704 704
705#pragma message "randomVector is not thread-safe in OSX" 705#pragma message "randomVector is not thread-safe in OSX"
706 706
707inline double urandom() {
708 const long max_random = 2147483647; // 2**31 - 1
709 return (double)random() / (double)max_random;
710}
711
707double gaussrand(int *phase, double *pV1, double *pV2, double *pS) 712double gaussrand(int *phase, double *pV1, double *pV2, double *pS)
708{ 713{
709 double V1=*pV1, V2=*pV2, S=*pS; 714 double V1=*pV1, V2=*pV2, S=*pS;
@@ -711,8 +716,8 @@ double gaussrand(int *phase, double *pV1, double *pV2, double *pS)
711 716
712 if(*phase == 0) { 717 if(*phase == 0) {
713 do { 718 do {
714 double U1 = (double)random() / (double)RAND_MAX; 719 double U1 = urandom();
715 double U2 = (double)random() / (double)RAND_MAX; 720 double U2 = urandom();
716 721
717 V1 = 2 * U1 - 1; 722 V1 = 2 * U1 - 1;
718 V2 = 2 * U2 - 1; 723 V2 = 2 * U2 - 1;
@@ -733,12 +738,14 @@ double gaussrand(int *phase, double *pV1, double *pV2, double *pS)
733int random_vector(unsigned int seed, int code, DVEC(r)) { 738int random_vector(unsigned int seed, int code, DVEC(r)) {
734 int phase = 0; 739 int phase = 0;
735 double V1,V2,S; 740 double V1,V2,S;
741
742 srandom(seed);
736 743
737 int k; 744 int k;
738 switch (code) { 745 switch (code) {
739 case 0: { // uniform 746 case 0: { // uniform
740 for (k=0; k<rn; k++) { 747 for (k=0; k<rn; k++) {
741 rp[k] = (double)random() / (double)RAND_MAX; 748 rp[k] = urandom();
742 } 749 }
743 OK 750 OK
744 } 751 }
@@ -771,7 +778,7 @@ double gaussrand(struct random_data *buffer,
771 778
772 if(*phase == 0) { 779 if(*phase == 0) {
773 do { 780 do {
774 double U1 = urandom(buffer); 781 double U1 = urandom(buffer);
775 double U2 = urandom(buffer); 782 double U2 = urandom(buffer);
776 783
777 V1 = 2 * U1 - 1; 784 V1 = 2 * U1 - 1;
@@ -787,6 +794,7 @@ double gaussrand(struct random_data *buffer,
787 *pV1=V1; *pV2=V2; *pS=S; 794 *pV1=V1; *pV2=V2; *pS=S;
788 795
789 return X; 796 return X;
797
790} 798}
791 799
792int random_vector(unsigned int seed, int code, DVEC(r)) { 800int random_vector(unsigned int seed, int code, DVEC(r)) {
@@ -801,7 +809,7 @@ int random_vector(unsigned int seed, int code, DVEC(r)) {
801 809
802 int phase = 0; 810 int phase = 0;
803 double V1,V2,S; 811 double V1,V2,S;
804 812
805 int k; 813 int k;
806 switch (code) { 814 switch (code) {
807 case 0: { // uniform 815 case 0: { // uniform