00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #ifdef HAVE_CONFIG_H
00040 #include <config.h>
00041 #endif
00042
00049
00051
00052
00053
00054
00055 #include <cpl.h>
00056
00057 #include "xsh_model_kernel.h"
00058
00059
00060
00061
00062
00063
00064 #include <limits.h>
00065
00066 #include "xsh_model_r250.h"
00067
00068
00069
00070
00071
00072
00073
00074 #ifndef TRUST_RAND
00075 #include "xsh_model_randlcg.h"
00076 #endif
00077
00078
00079 #define BITS 31
00080 #define WORD_BIT 32
00081
00082 #if WORD_BIT == 32
00083 #ifndef BITS
00084 #define BITS 32
00085 #endif
00086 #else
00087 #ifndef BITS
00088 #define BITS 16
00089 #endif
00090 #endif
00091
00092 #if BITS == 31
00093 #define MSB 0x40000000L
00094 #define ALL_BITS 0x7fffffffL
00095 #define HALF_RANGE 0x20000000L
00096 #define STEP 7
00097 #endif
00098
00099 #if BITS == 32
00100 #define MSB 0x80000000L
00101 #define ALL_BITS 0xffffffffL
00102 #define HALF_RANGE 0x40000000L
00103 #define STEP 7
00104 #endif
00105
00106 #if BITS == 16
00107 #define MSB 0x8000
00108 #define ALL_BITS 0xffff
00109 #define HALF_RANGE 0x4000
00110 #define STEP 11
00111 #endif
00112
00113 static unsigned int r250_buffer[ 250 ];
00114 static int r250_index;
00115
00116 #ifdef NO_PROTO
00117 void xsh_r250_init(sd)
00118 int seed;
00119 #else
00120 void xsh_r250_init(int sd)
00121 #endif
00122 {
00123 int j, k;
00124 unsigned int mask, msb;
00125
00126 #ifdef TRUST_RAND
00127
00128 #if BITS == 32 || BITS == 31
00129 srand48( sd );
00130 #else
00131 srand( sd );
00132 #endif
00133
00134
00135 #else
00136 xsh_set_seed( sd );
00137 #endif
00138
00139 r250_index = 0;
00140 for (j = 0; j < 250; j++)
00141 #ifdef TRUST_RAND
00142 #if BITS == 32 || BITS == 31
00143 r250_buffer[j] = (unsigned int)lrand48();
00144 #else
00145 r250_buffer[j] = rand();
00146 #endif
00147 #else
00148 r250_buffer[j] = xsh_randlcg();
00149 #endif
00150
00151
00152 for (j = 0; j < 250; j++)
00153 #ifdef TRUST_RAND
00154 if ( rand() > HALF_RANGE )
00155 r250_buffer[j] |= MSB;
00156 #else
00157 if ( xsh_randlcg() > HALF_RANGE )
00158 r250_buffer[j] |= MSB;
00159 #endif
00160
00161
00162 msb = MSB;
00163 mask = ALL_BITS;
00164
00165 for (j = 0; j < BITS; j++)
00166 {
00167 k = STEP * j + 3;
00168 r250_buffer[k] &= mask;
00169 r250_buffer[k] |= msb;
00170 mask >>= 1;
00171 msb >>= 1;
00172 }
00173
00174 }
00175
00176 unsigned int r250(void)
00177 {
00178 register int j;
00179 register unsigned int new_rand;
00180
00181 if ( r250_index >= 147 )
00182 j = r250_index - 147;
00183 else
00184 j = r250_index + 103;
00185
00186 new_rand = r250_buffer[ r250_index ] ^ r250_buffer[ j ];
00187 r250_buffer[ r250_index ] = new_rand;
00188
00189 if ( r250_index >= 249 )
00190 r250_index = 0;
00191 else
00192 r250_index++;
00193
00194 return new_rand;
00195
00196 }
00197
00198
00199 double xsh_dr250(void)
00200 {
00201 register int j;
00202 register unsigned int new_rand;
00203
00204 if ( r250_index >= 147 )
00205 j = r250_index - 147;
00206 else
00207 j = r250_index + 103;
00208
00209 new_rand = r250_buffer[ r250_index ] ^ r250_buffer[ j ];
00210 r250_buffer[ r250_index ] = new_rand;
00211
00212 if ( r250_index >= 249 )
00213 r250_index = 0;
00214 else
00215 r250_index++;
00216
00217 return (double)new_rand / ALL_BITS;
00218
00219 }
00220
00221 #ifdef MAIN
00222
00223
00224
00225 #include <stdio.h>
00226
00227 #define NMR_RAND 5000
00228 #define MAX_BINS 500
00229
00230 #ifdef NO_PROTO
00231 void main(argc, argv)
00232 int argc;
00233 char **argv;
00234 #else
00235 void main(int argc, char **argv)
00236 #endif
00237 {
00238 int j,k,nmr_bins,seed;
00239 int bins[MAX_BINS];
00240 double randm, bin_inc;
00241 double bin_limit[MAX_BINS];
00242
00243 if ( argc != 3 )
00244 {
00245 printf("Usage -- %s nmr_bins seed\n", argv[0]);
00246 exit(1);
00247 }
00248
00249 nmr_bins = atoi( argv[1] );
00250 if ( nmr_bins > MAX_BINS )
00251 {
00252 printf("ERROR -- maximum number of bins is %d\n", MAX_BINS);
00253 exit(1);
00254 }
00255
00256 seed = atoi( argv[2] );
00257
00258 xsh_r250_init( seed );
00259
00260 if ( nmr_bins < 1 )
00261 {
00262 for (j = 0; j < NMR_RAND; j++)
00263 printf("%f\n", xsh_dr250() );
00264 exit(0);
00265 }
00266
00267 bin_inc = 1.0 / nmr_bins;
00268 for (j = 0; j < nmr_bins; j++)
00269 {
00270 bins[j] = 0;
00271 bin_limit[j] = (j + 1) * bin_inc;
00272 }
00273
00274 bin_limit[nmr_bins-1] = 1.0e7;
00275
00276 for (j = 0; j < NMR_RAND; j++)
00277 {
00278 randm = r250() / (double)ALL_BITS;
00279 for (k = 0; k < nmr_bins; k++)
00280 if ( randm < bin_limit[k] )
00281 {
00282 bins[k]++;
00283 break;
00284 }
00285 }
00286
00287
00288 for (j = 0; j < nmr_bins; j++)
00289 printf("%d\n", bins[j]);
00290
00291 }
00292
00293 #endif
00294