?? random.c
字號:
/*====*====*====*====*====*====*====*====*====*====*====*====*====*====*====* 抄薦 積己GENERAL DESCRIPTION 抄薦 積己INITIALIZATION AND SEQUENCING REQUIREMENTS Copyright (c) 2007 by ARTLAB, ICU. All Rights Reserved.*====*====*====*====*====*====*====*====*====*====*====*====*====*====*====*//*=========================================================================== EDIT HISTORY FOR MODULEwhen who what, where, why-------- --- ----------------------------------------------------------07/01/03 khpark created=========================================================================== */#include <math.h>#include <stdlib.h>#include <time.h>#include "random.h"#define FALSE 0#define TRUE 1unsigned char is_initialized = FALSE;/*=======================================================================FUNCTION generate_randomDESCRIPTIONDEPENDENCIESRETURN VALUESIDE EFFECTS========================================================================== */void generate_random(int *buffer, int size){ int i = 0; /* Seed the random-number generator with current time so that * the numbers will be different every time we run. */ if( is_initialized == FALSE ) { srand( (unsigned)time( NULL ) ); is_initialized = TRUE; } for( i = 0; i < size; i++ ) { *buffer++ = UniformRand() > 0.5? 1 : 0 ; }}/*=======================================================================FUNCTION UniformRandDESCRIPTION Generate Uniform random number [0, 1]DEPENDENCIESRETURN VALUESIDE EFFECTS========================================================================== */double UniformRand(void){ double rv; /* Seed the random-number generator with current time so that * the numbers will be different every time we run. */ if( is_initialized == FALSE ) { srand( (unsigned)time( NULL ) ); is_initialized = TRUE; } rv = (double)rand()/(double)RAND_MAX; return (rv);}/*=======================================================================FUNCTION GenerateOneRealWithUniformOverDESCRIPTION Generate one real number using a uniform dist. over [Min - Max]DEPENDENCIESRETURN VALUESIDE EFFECTS========================================================================== */double GenerateOneRealWithUniformOver(double min, double max){ double RV; RV = (max-min)*UniformRand() + min; return RV;}/*=======================================================================FUNCTION GaussRandwithMeanVarDESCRIPTION Gaussian Random Number Generator with any mean and any varianceDEPENDENCIESRETURN VALUESIDE EFFECTS========================================================================== */double GaussRandwithMeanVar(double Mean, double Var){ static double t=0.; double x, v1, v2, r; /* Seed the random-number generator with current time so that * the numbers will be different every time we run. */ if( is_initialized == FALSE ) { srand( (unsigned)time( NULL ) ); is_initialized = TRUE; } if (t==0) { do { v1 = 2.*UniformRand() - 1.; v2 = 2.*UniformRand() - 1.; r = v1*v1 + v2*v2; }while ( r>=1.); r=sqrt(-2.*log(r)/r); t = v2*r; return (Mean+sqrt(Var)*v1*r); } else { x=t; t=0.; return (Mean+sqrt(Var)*x); }}/*=======================================================================FUNCTION GaussRandDESCRIPTION Gaussian Random Number Generator with mean 0 and variance 1DEPENDENCIESRETURN VALUESIDE EFFECTS========================================================================== */double GaussRand(void) {/* static int sw=0; static double r1, r2, s; if (sw==0) { sw=1; do { r1 =2*rnd()-1; r2 =2*rnd()-1; s=r1*r1 + r2*r2; }while ( s>1 || s==0); s=sqrt(-2*log(s)/s); return r1*s; }else{ sw=0; return r2*s; }*/ static double t=0.; double x, v1, v2, r; /* Seed the random-number generator with current time so that * the numbers will be different every time we run. */ if( is_initialized == FALSE ) { srand( (unsigned)time( NULL ) ); is_initialized = TRUE; } if (t==0) { do { v1 = 2.*UniformRand() - 1.; v2 = 2.*UniformRand() - 1.; r = v1*v1 + v2*v2; }while ( r>=1.); r=sqrt(-2.*log(r)/r); t = v2*r; return v1*r; } else { x=t; t=0.; return x; }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -