📄 r250c.sh01
字号:
---- Cut Here and unpack ----#!/bin/sh# shar: Shell Archiver (v1.22)## # ## Run the following text with /bin/sh to create:# r250.c# r250.h# randlcg.h# randlcg.c#if test -r s2_seq_.tmpthen echo "Must unpack archives in sequence!" next=`cat s2_seq_.tmp`; echo "Please unpack part $next next" exit 1; fiecho "x - extracting r250.c (Text)"sed 's/^X//' << 'SHAR_EOF' > r250.c &&X/* r250.c the r250 uniform random number algorithmXX Kirkpatrick, S., and E. Stoll, 1981; "A Very FastX Shift-Register Sequence Random Number Generator",X Journal of Computational Physics, V.40XX also:XX see W.L. Maier, DDJ May 1991XXXX*/XXstatic char rcsid[] = "@(#)r250.c 1.2 15:50:31 11/21/94 EFC";XXX#include <limits.h>XX#include "r250.h"XX/* set the following if you trust rand(), otherwise the minimal standardX generator is usedX*/X/* #define TRUST_RAND */XXX#ifndef TRUST_RANDX#include "randlcg.h"X#endifXX/* defines to allow for 16 or 32 bit integers */X#define BITS 31XXX#if WORD_BIT == 32X#ifndef BITSX#define BITS 32X#endifX#elseX#ifndef BITSX#define BITS 16X#endifX#endifXX#if BITS == 31X#define MSB 0x40000000LX#define ALL_BITS 0x7fffffffLX#define HALF_RANGE 0x20000000LX#define STEP 7X#endifXX#if BITS == 32X#define MSB 0x80000000LX#define ALL_BITS 0xffffffffLX#define HALF_RANGE 0x40000000LX#define STEP 7X#endifXX#if BITS == 16X#define MSB 0x8000X#define ALL_BITS 0xffffX#define HALF_RANGE 0x4000X#define STEP 11X#endifXXstatic unsigned int r250_buffer[ 250 ];Xstatic int r250_index;XX#ifdef NO_PROTOXvoid r250_init(sd)Xint seed;X#elseXvoid r250_init(int sd)X#endifX{X int j, k;X unsigned int mask, msb;XX#ifdef TRUST_RAND XX#if BITS == 32 || BITS == 31 X srand48( sd );X#elseX srand( sd );X#endif XXX#elseX set_seed( sd );X#endifX X r250_index = 0;X for (j = 0; j < 250; j++) /* fill r250 buffer with BITS-1 bit values */X#ifdef TRUST_RANDX#if BITS == 32 || BITS == 31X r250_buffer[j] = (unsigned int)lrand48();X#elseX r250_buffer[j] = rand();X#endifX#elseX r250_buffer[j] = randlcg();X#endifXXX for (j = 0; j < 250; j++) /* set some MSBs to 1 */X#ifdef TRUST_RANDX if ( rand() > HALF_RANGE )X r250_buffer[j] |= MSB;X#elseX if ( randlcg() > HALF_RANGE )X r250_buffer[j] |= MSB;X#endifXXX msb = MSB; /* turn on diagonal bit */X mask = ALL_BITS; /* turn off the leftmost bits */XX for (j = 0; j < BITS; j++)X {X k = STEP * j + 3; /* select a word to operate on */X r250_buffer[k] &= mask; /* turn off bits left of the diagonal */X r250_buffer[k] |= msb; /* turn on the diagonal bit */X mask >>= 1;X msb >>= 1;X }XX}XXunsigned int r250() /* returns a random unsigned integer */X{X register int j;X register unsigned int new_rand;XX if ( r250_index >= 147 )X j = r250_index - 147; /* wrap pointer around */X elseX j = r250_index + 103;XX new_rand = r250_buffer[ r250_index ] ^ r250_buffer[ j ];X r250_buffer[ r250_index ] = new_rand;XX if ( r250_index >= 249 ) /* increment pointer for next time */X r250_index = 0;X elseX r250_index++;XX return new_rand;XX}XXXdouble dr250() /* returns a random double in range 0..1 */X{X register int j;X register unsigned int new_rand;XX if ( r250_index >= 147 )X j = r250_index - 147; /* wrap pointer around */X elseX j = r250_index + 103;XX new_rand = r250_buffer[ r250_index ] ^ r250_buffer[ j ];X r250_buffer[ r250_index ] = new_rand;XX if ( r250_index >= 249 ) /* increment pointer for next time */X r250_index = 0;X elseX r250_index++;XX return (double)new_rand / ALL_BITS;XX}XX#ifdef MAINXX/* test driver prints out either NMR_RAND values or a histogram */XX#include <stdio.h>XX#define NMR_RAND 5000X#define MAX_BINS 500XX#ifdef NO_PROTOXvoid main(argc, argv)Xint argc;Xchar **argv;X#elseXvoid main(int argc, char **argv)X#endifX{X int j,k,nmr_bins,seed;X int bins[MAX_BINS];X double randm, bin_inc;X double bin_limit[MAX_BINS];XX if ( argc != 3 )X {X printf("Usage -- %s nmr_bins seed\n", argv[0]);X exit(1);X }XX nmr_bins = atoi( argv[1] );X if ( nmr_bins > MAX_BINS )X {X printf("ERROR -- maximum number of bins is %d\n", MAX_BINS);X exit(1);X }XX seed = atoi( argv[2] );XX r250_init( seed );XX if ( nmr_bins < 1 ) /* just print out the numbers */X {X for (j = 0; j < NMR_RAND; j++)X printf("%f\n", dr250() );X exit(0);X }X X bin_inc = 1.0 / nmr_bins;X for (j = 0; j < nmr_bins; j++) /* initialize bins to zero */X {X bins[j] = 0;X bin_limit[j] = (j + 1) * bin_inc;X }XX bin_limit[nmr_bins-1] = 1.0e7; /* make sure all others are in last bin */XX for (j = 0; j < NMR_RAND; j++)X {X randm = r250() / (double)ALL_BITS;X for (k = 0; k < nmr_bins; k++)X if ( randm < bin_limit[k] )X {X bins[k]++;X break;X }X }XXX for (j = 0; j < nmr_bins; j++)X printf("%d\n", bins[j]);X X}XX#endifXSHAR_EOFchmod 0444 r250.c || echo "restore of r250.c fails"echo "x - extracting r250.h (Text)"sed 's/^X//' << 'SHAR_EOF' > r250.h &&X/* r250.h prototypes for r250 random number generator,XX Kirkpatrick, S., and E. Stoll, 1981; "A Very FastX Shift-Register Sequence Random Number Generator",X Journal of Computational Physics, V.40XX also:XX see W.L. Maier, DDJ May 1991XXX*/XX#ifndef _R250_H_X#define _R250_H_ 1.2XX#ifdef __cplusplusXextern "C" {X#endifXX#ifdef NO_PROTOXvoid r250_init();Xunsigned int r250();Xdouble dr250();XX#elseXvoid r250_init(int seed);Xunsigned int r250( void );Xdouble dr250( void );X#endifXX#ifdef __cplusplusX}X#endifXX#endifSHAR_EOFchmod 0444 r250.h || echo "restore of r250.h fails"echo "x - extracting randlcg.h (Text)"sed 's/^X//' << 'SHAR_EOF' > randlcg.h &&XX/* randlcg.h prototypes for the minimal standard random number generator,XX Linear Congruential Method, the "minimal standard generator"X Park & Miller, 1988, Comm of the ACM, 31(10), pp. 1192-1201XXX rcsid: @(#)randlcg.h 1.1 15:48:09 11/21/94 EFCXX*/XX#ifndef _RANDLCG_H_X#define _RANDLCG_H_ 1.1XX#ifdef __cplusplusXextern "C" {X#endifXX#ifdef NO_PROTOXlong set_seed();Xlong get_seed();Xunsigned long int randlcg();XX#elseXlong set_seed(long);Xlong get_seed(long);Xunsigned long int randlcg();XX#endifXX#ifdef __cplusplusX}X#endifXX#endifSHAR_EOFchmod 0444 randlcg.h || echo "restore of randlcg.h fails"echo "x - extracting randlcg.c (Text)"sed 's/^X//' << 'SHAR_EOF' > randlcg.c &&X/* rndlcg Linear Congruential Method, the "minimal standard generator"X Park & Miller, 1988, Comm of the ACM, 31(10), pp. 1192-1201XX*/XXstatic char rcsid[] = "@(#)randlcg.c 1.1 15:48:15 11/21/94 EFC";XX#include <math.h>X#include <limits.h>XX#define ALL_BITS 0xffffffffXXstatic long int quotient = LONG_MAX / 16807L;Xstatic long int remainder = LONG_MAX % 16807L;XXstatic long int seed_val = 1L;XXlong set_seed(long int sd)X{X return seed_val = sd;X}XXlong get_seed()X{X return seed_val;X}XXXunsigned long int randlcg() /* returns a random unsigned integer */X{X if ( seed_val <= quotient )X seed_val = (seed_val * 16807L) % LONG_MAX;X elseX {X long int high_part = seed_val / quotient;X long int low_part = seed_val % quotient;XX long int test = 16807L * low_part - remainder * high_part;XX if ( test > 0 )X seed_val = test;X elseX seed_val = test + LONG_MAX;XX }XX return seed_val;X}XXXXXSHAR_EOFchmod 0444 randlcg.c || echo "restore of randlcg.c fails"exit 0
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -