summaryrefslogtreecommitdiff
path: root/packages/base/src/Internal/C
diff options
context:
space:
mode:
Diffstat (limited to 'packages/base/src/Internal/C')
-rw-r--r--packages/base/src/Internal/C/vector-aux.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/packages/base/src/Internal/C/vector-aux.c b/packages/base/src/Internal/C/vector-aux.c
index 9dbf536..1cef27d 100644
--- a/packages/base/src/Internal/C/vector-aux.c
+++ b/packages/base/src/Internal/C/vector-aux.c
@@ -945,6 +945,8 @@ int vectorScan(char * file, int* n, double**pp){
945/* Windows use thread-safe random 945/* Windows use thread-safe random
946 See: http://stackoverflow.com/questions/143108/is-windows-rand-s-thread-safe 946 See: http://stackoverflow.com/questions/143108/is-windows-rand-s-thread-safe
947*/ 947*/
948#if defined (__APPLE__) || (__FreeBSD__)
949
948inline double urandom() { 950inline double urandom() {
949 /* the probalility of matching will be theoretically p^3(in fact, it is not) 951 /* the probalility of matching will be theoretically p^3(in fact, it is not)
950 p is matching probalility of random(). 952 p is matching probalility of random().
@@ -959,6 +961,22 @@ inline double urandom() {
959 return (double)nrand48(state) / (double)max_random; 961 return (double)nrand48(state) / (double)max_random;
960} 962}
961 963
964#else
965
966#define _CRT_RAND_S
967inline double urandom() {
968 unsigned int number;
969 errno_t err;
970 err = rand_s(&number);
971 if (err!=0) {
972 printf("something wrong\n");
973 return -1;
974 }
975 return (double)number / (double)UINT_MAX;
976}
977
978#endif
979
962double gaussrand(int *phase, double *pV1, double *pV2, double *pS) 980double gaussrand(int *phase, double *pV1, double *pV2, double *pS)
963{ 981{
964 double V1=*pV1, V2=*pV2, S=*pS; 982 double V1=*pV1, V2=*pV2, S=*pS;
@@ -985,6 +1003,35 @@ double gaussrand(int *phase, double *pV1, double *pV2, double *pS)
985 1003
986} 1004}
987 1005
1006#if defined(_WIN32) || defined(WIN32)
1007
1008int random_vector(unsigned int seed, int code, DVEC(r)) {
1009 int phase = 0;
1010 double V1,V2,S;
1011
1012 srand(seed);
1013
1014 int k;
1015 switch (code) {
1016 case 0: { // uniform
1017 for (k=0; k<rn; k++) {
1018 rp[k] = urandom();
1019 }
1020 OK
1021 }
1022 case 1: { // gaussian
1023 for (k=0; k<rn; k++) {
1024 rp[k] = gaussrand(&phase,&V1,&V2,&S);
1025 }
1026 OK
1027 }
1028
1029 default: ERROR(BAD_CODE);
1030 }
1031}
1032
1033#else
1034
988int random_vector(unsigned int seed, int code, DVEC(r)) { 1035int random_vector(unsigned int seed, int code, DVEC(r)) {
989 int phase = 0; 1036 int phase = 0;
990 double V1,V2,S; 1037 double V1,V2,S;
@@ -1010,6 +1057,8 @@ int random_vector(unsigned int seed, int code, DVEC(r)) {
1010 } 1057 }
1011} 1058}
1012 1059
1060#endif
1061
1013#else 1062#else
1014 1063
1015inline double urandom(struct random_data * buffer) { 1064inline double urandom(struct random_data * buffer) {