From 6be4f3d37617350cb4d5e16118945dcab655d29d Mon Sep 17 00:00:00 2001 From: Bill Somerville Date: Mon, 14 Dec 2015 19:40:44 +0000 Subject: [PATCH] Rationalize random number seeding in C and Fortran git-svn-id: svn+ssh://svn.code.sf.net/p/wsjt/wsjt/branches/wsjtx@6272 ab8295b8-cf94-4d9e-aec4-7959e3be5d79 --- CMakeLists.txt | 3 +++ lib/code426.f90 | 2 ++ lib/jt4sim.f90 | 2 ++ lib/jt65sim.f90 | 5 ++--- lib/sfrsd/rstest.c | 7 +++++-- lib/sfrsd/sfrsd.c | 14 ++++---------- lib/sgran.c | 13 +++---------- lib/t1.f90 | 1 + lib/test_init_random_seed.f90 | 8 ++++++++ lib/wsprd/wsprd.c | 5 ++++- 10 files changed, 34 insertions(+), 26 deletions(-) create mode 100644 lib/test_init_random_seed.f90 diff --git a/CMakeLists.txt b/CMakeLists.txt index a5de2a761..c6d19c6a9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -336,6 +336,7 @@ set (wsjt_FSRCS lib/hspec.f90 lib/image.f90 lib/indexx.f90 + lib/init_random_seed.f90 lib/interleave4.f90 lib/interleave63.f90 lib/interleave9.f90 @@ -417,6 +418,7 @@ set (wsjt_CSRCS lib/ftrsd/ftrsd2.c lib/gran.c lib/igray.c + lib/init_random_seed.c lib/wsprd/nhash.c lib/tab.c lib/tmoonsub.c @@ -446,6 +448,7 @@ set (wsprd_CSRCS lib/wsprd/fano.c lib/wsprd/tab.c lib/wsprd/nhash.c + lib/init_random_seed.c ) set (wsjtx_UISRCS diff --git a/lib/code426.f90 b/lib/code426.f90 index 7acf03acd..111c54923 100644 --- a/lib/code426.f90 +++ b/lib/code426.f90 @@ -17,6 +17,8 @@ program code426 call getarg(2,arg) read(arg,*) iters + call init_random_seed() + open(13,file='code426.out',status='unknown') write(*,1002) nmsgs,iters diff --git a/lib/jt4sim.f90 b/lib/jt4sim.f90 index 4b96c2bde..1950ce327 100644 --- a/lib/jt4sim.f90 +++ b/lib/jt4sim.f90 @@ -45,6 +45,8 @@ program jt4sim freq=f0 !Silence compiler warning dphi=0.0 !Silence compiler warning + call init_random_seed() + h=default_header(12000,npts) if(message(1:3).eq.'sin') read(message(4:),*) sinfreq diff --git a/lib/jt65sim.f90 b/lib/jt65sim.f90 index 449e49efa..04f3ab62c 100644 --- a/lib/jt65sim.f90 +++ b/lib/jt65sim.f90 @@ -35,6 +35,8 @@ program jt65sim go to 999 endif + call init_random_seed(1) + csubmode='A' call getarg(1,csubmode) mode65=1 @@ -62,9 +64,6 @@ program jt65sim h=default_header(12000,npts) dfsig=2000.0/nsigs !Freq spacing between sigs in file (Hz) -! generate new random number seed for each run using /dev/urandom on linux and os x -! nerr=sgran() - do ifile=1,nfiles !Loop over requested number of files write(fname,1002) ifile !Output filename 1002 format('000000_',i4.4) diff --git a/lib/sfrsd/rstest.c b/lib/sfrsd/rstest.c index 979f8ffc4..11c67e1ea 100644 --- a/lib/sfrsd/rstest.c +++ b/lib/sfrsd/rstest.c @@ -17,6 +17,7 @@ Information-carrying channel symbols #include #include #include "rs.h" +#include "init_random_seed.h" static void *rs; @@ -35,6 +36,8 @@ int main(){ int mrsym[63],mrprob[63],mr2sym[63],mr2prob[63]; int nsec2,ncount,dat4[12]; + init_random_seed(); + datfile=fopen("kvasd.dat","rb"); if( !datfile ) { printf("Unable to open kvasd.dat\n"); @@ -96,11 +99,11 @@ int main(){ printf("num_errors = %d\n",num_errors); for( i=0; i #include #include "rs.h" +#include "init_random_seed.h" static void *rs; @@ -175,12 +176,9 @@ int main(int argc, char *argv[]){ // "soft" distance between each codeword and the received word is // used to decide which codeword is "best". // - // srandom(time(NULL)); -#ifdef WIN32 - srand(0xdeadbeef); -#else - srandom(0xdeadbeef); -#endif + + init_random_seed(); + float p_erase; int thresh, nsum; ncandidates=0; @@ -207,11 +205,7 @@ int main(int argc, char *argv[]){ } thresh = p_erase*100; long int ir; -#ifdef WIN32 ir=rand(); -#else - ir=random(); -#endif if( ((ir % 100) < thresh ) && numera < 51 ) { era_pos[numera]=indexes[62-i]; numera=numera+1; diff --git a/lib/sgran.c b/lib/sgran.c index 697408360..e3ac9a804 100644 --- a/lib/sgran.c +++ b/lib/sgran.c @@ -1,15 +1,8 @@ -#include -#include -#include +#include "init_random_seed.h" -/* seed rand using urandom */ +/* seed rand */ float sgran_() { - unsigned int seed; - FILE *urandom; - urandom = fopen ("/dev/urandom","r"); - fread (&seed, sizeof (seed), 1, urandom); - srand (seed); - + init_random_seed(); return(0); } diff --git a/lib/t1.f90 b/lib/t1.f90 index f866005fc..97dc73ba1 100644 --- a/lib/t1.f90 +++ b/lib/t1.f90 @@ -23,6 +23,7 @@ program t1 call getarg(3,arg) read(arg,*) iters + call init_random_seed() call random_number(r) itone=0 where(r.gt.0.5) itone=1 diff --git a/lib/test_init_random_seed.f90 b/lib/test_init_random_seed.f90 new file mode 100644 index 000000000..3b9dad1f2 --- /dev/null +++ b/lib/test_init_random_seed.f90 @@ -0,0 +1,8 @@ +program test_init_random_seed + real :: r(10,4) + call init_random_seed() + call random_number(r) + do i =1,10 + write (*, *) (r(i,j),j=1,4) + end do +end program test_init_random_seed diff --git a/lib/wsprd/wsprd.c b/lib/wsprd/wsprd.c index ebae1739c..19d7d7e7b 100644 --- a/lib/wsprd/wsprd.c +++ b/lib/wsprd/wsprd.c @@ -40,6 +40,7 @@ #include "nhash.h" #include "wsprd_utils.h" #include "wsprsim_utils.h" +#include "lib/init_random_seed.h" #define max(x,y) ((x) > (y) ? (x) : (y)) // Possible PATIENCE options: FFTW_ESTIMATE, FFTW_ESTIMATE_PATIENT, @@ -614,7 +615,9 @@ int main(int argc, char *argv[]) memset(allcalls,0,sizeof(char)*100*13); int uniques=0, noprint=0; - + + init_random_seed(); + // Parameters used for performance-tuning: maxcycles=10000; //Fano timeout limit double minsync1=0.10; //First sync limit