📄 franuni.c
字号:
/* Copyright (c) Colorado School of Mines, 2003.*//* All rights reserved. *//*********************** self documentation **********************//*****************************************************************************FRANUNI - Functions to generate a pseudo-random float uniformly distributed on [0,1); i.e., between 0.0 (inclusive) and 1.0 (exclusive)franuni return a random floatsranuni seed random number generator******************************************************************************Function Prototypes:float franuni (void);void sranuni (int seed);******************************************************************************franuni:Input: (none)Returned: pseudo-random floatsranuni:seed different seeds yield different sequences of random numbers.******************************************************************************Notes:Adapted from subroutine uni in Kahaner, Moler, and Nash (1988). This book references a set of unpublished notes byMarsaglia.According to the reference, this randomnumber generator "passes all known tests and has a period that is ...approximately 10^19".******************************************************************************References:"Numerical Methods and Software", D. Kahaner, C. Moler, S. Nash,Prentice Hall, 1988. Marsaglia G., "Comments on the perfect uniform random number generator",Unpublished notes, Wash S. U.******************************************************************************Author: Dave Hale, Colorado School of Mines, 12/30/89*****************************************************************************//**************** end self doc ********************************/#include "cwp.h"/* constants used to generate random numbers (16777216=2^24) */#define CS 362436.0/16777216.0#define CD 7654321.0/16777216.0#define CM 16777213.0/16777216.0#define NBITS 24/* internal state variables */static int i=16,j=4;static float c=CS;static float u[]={ 0.8668672834288F, 0.3697986366357F, 0.8008968294805F, 0.4173889774680F, 0.8254561579836F, 0.9640965269077F, 0.4508667414265F, 0.6451309529668F, 0.1645456024730F, 0.2787901807898F, 0.06761531340295F, 0.9663226330820F, 0.01963343943798F, 0.02947398211399F, 0.1636231515294F, 0.3976343250467F, 0.2631008574685F};floatfranuni (void)/*****************************************************************************return a pseudo-random float between 0.0 (inclusive) and 1.0 (exclusive)******************************************************************************Returned: pseudo-random float*****************************************************************************/{ float uni; /* basic generator is Fibonacci */ uni = u[i]-u[j]; if (uni<0.0) uni += 1.0; u[i] = uni; i--; if (i<0) i = 16; j--; if (j<0) j = 16; /* second generator is congruential */ c -= CD; if (c<0.0) c += CM; /* combination generator */ uni -= c; if (uni<0.0) uni += 1.0; return uni;}voidsranuni (int seed)/*****************************************************************************seed random number generator******************************************************************************Input:seed different seeds yield different sequences of random numbers.*****************************************************************************/{ int ii,jj,i1,j01,k1,l1,m1; float s,t; /* convert seed to four smallish positive integers */ i1 = (ABS(seed)%177)+1; j01 = (ABS(seed)%167)+1; k1 = (ABS(seed)%157)+1; l1 = (ABS(seed)%147)+1; /* generate random bit pattern in array based on given seed */ for (ii=0; ii<17; ii++) { s = 0.0; t = 0.5; /* loop over bits in the float mantissa */ for (jj=0; jj<NBITS; jj++) { m1 = (((i1*j01)%179)*k1)%179; i1 = j01; j01 = k1; k1 = m1; l1 = (53*l1+1)%169; if (((l1*m1)%64)>=32) s += t; t *= 0.5; } u[ii] = s; } /* initialize generators */ i = 16; j = 4; c = CS;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -