?? noise.c
字號:
sgenrand(seedv, mtNew); /* Initialize with system seed */
for (i = 1; i < 624 ; i += 2) { /* Alter every other state with new seed */
mtNew[i] = ul_seed & 0xffff0000;
ul_seed = 69069 * ul_seed + 1;
mtNew[i] |= (ul_seed & 0xffff0000) >> 16;
ul_seed = 69069 * ul_seed + 1;
}
}
return mtNew;
}
/*
* FUNCTION: double genUDN(ulp_mt)
* PURPOSE:
*
* Generates uniformly distributed noise
* ranging from [0, 1). lp_mt is a ULong
* pointer returned from genRandInit.
*
* DESCRIPTION:
* RETURN VALUE:
* INCLUDE FILES NECESSARY:
* PROBLEMS/SUGGESTIONS:
* GLOBALS/SIDE EFFECTS:
*/
double genUDN(ulp_mt)
ULong *ulp_mt;
{
return genrand(ulp_mt);
}
/*
* FUNCTION: double genWGN(ulp_mt)
* PURPOSE:
*
* Generates white Gaussian noise. lp_mt
* is a ULong pointer returned from genRandInit.
*
* DESCRIPTION:
* RETURN VALUE:
* INCLUDE FILES NECESSARY:
* PROBLEMS/SUGGESTIONS:
* GLOBALS/SIDE EFFECTS:
*/
double genWGN(ulp_mt)
ULong *ulp_mt;
{
static double nextVal = 0.0;
static int useNext = 0;
if (useNext) {
useNext = 0;
return nextVal;
}
else {
double r, s, c;
do {
s = 2.0*genUDN(ulp_mt) - 1.0;
c = 2.0*genUDN(ulp_mt) - 1.0;
r = s*s + c*c;
} while (r >= 1.0);
r = sqrt(-2.0*log(r) / r);
nextVal = c * r;
useNext = 1;
return (s * r);
}
}
/*
* FUNCTION: double genRandBit(ulp_mt, d_prob_zero)
* PURPOSE:
*
* Returns a value equal to 0 or 1. ulp_mt
* is a ULong pointer returned by genRandInit.
*
* DESCRIPTION:
* RETURN VALUE:
* INCLUDE FILES NECESSARY:
* PROBLEMS/SUGGESTIONS:
* GLOBALS/SIDE EFFECTS:
*/
double genRandBit(ulp_mt, d_prob_zero)
ULong *ulp_mt;
double d_prob_zero;
{
return (genUDN(ulp_mt) > d_prob_zero) ? 1.0 : 0.0;
}
/*
* FUNCTION: double genEXP(ulp_mt)
* PURPOSE:
*
* Generates an exponentially distributed Random variable.
*
* DESCRIPTION:
*
* Returns an exponentially distributed, positive, random deviate
* of unit mean, using genUDN(ulp_mt) as the source of
* uniform deviates.
* This function was retrieved from "Numerical Recipies in C"
* by William Press et. al.
*
* RETURN VALUE:
* INCLUDE FILES NECESSARY:
* PROBLEMS/SUGGESTIONS:
* GLOBALS/SIDE EFFECTS:
*/
double genEXP(ulp_mt)
ULong *ulp_mt;
{
double x;
/*
* Returns an exponentially distributed, positive, random
* deviate of unit mean, using noiseUDN(lp_seed) as the
* source of uniform deviates.
*/
x = genUDN(ulp_mt);
if (x == 0.0) x = genUDN(ulp_mt);
return -log(x);
}
/*
* FUNCTION: double genPOISS(ulp_mt, d_mean)
* PURPOSE:
*
* Function for Poisson distribution
*
* DESCRIPTION:
*
* Returns as a floating point number an integer
* value that is a random deviate drawn from a
* poisson distribution of mean xm, using a source
* of uniform random deviates.
* This function was retrieved from Chapter 7 of
* the "Numerical Recipies in C" by Press et. al.
*
* RETURN VALUE:
* INCLUDE FILES NECESSARY:
* PROBLEMS/SUGGESTIONS:
* GLOBALS/SIDE EFFECTS:
*/
double genPOISS(ulp_mt, d_xm)
ULong *ulp_mt;
double d_xm;
{
static double sq, alxm, g;
static double oldm = -1.0;
double em, t, y;
if (d_xm < 12.0) {
if (d_xm != oldm) {
oldm = d_xm;
g = exp(-d_xm);
}
em = -1;
t = 1.0;
do {
em += 1.0;
t *= genUDN(ulp_mt);
} while (t > g);
}
else {
if (d_xm != oldm) {
oldm = d_xm;
sq = sqrt(2.0 * d_xm);
alxm = log(d_xm);
g = d_xm * alxm - gammln(d_xm + 1.0);
}
do {
do {
y = tan(PI * genUDN(ulp_mt));
em = sq * y + d_xm;
} while (em < 0.0);
em = floor(em);
t = 0.9 * (1.0 + y*y) * exp(em * alxm - gammln(em + 1.0) - g);
} while (genUDN(ulp_mt) > t);
}
return em;
}
/*
* A slightly altered version of mt19937 to handle passing
* the random number generator state as an argument to allow
* for multiple generator instances. Below is the original
* comment for the mt19937-2.c file but this is slightly modified
* to handle multiple random generator states. The original
* file is provided at mt19937-2.c
*
*/
/* A C-program for MT19937: Real number version (1999/10/28) */
/* genrand() generates one pseudorandom real number (double) */
/* which is uniformly distributed on [0,1]-interval, for each */
/* call. sgenrand(seed) sets initial values to the working area */
/* of 624 words. Before genrand(), sgenrand(seed) must be */
/* called once. (seed is any 32-bit integer.) */
/* Integer generator is obtained by modifying two lines. */
/* Coded by Takuji Nishimura, considering the suggestions by */
/* Topher Cooper and Marc Rieffel in July-Aug. 1997. */
/* This library is free software under the Artistic license: */
/* see the file COPYING distributed together with this code. */
/* For the verification of the code, its output sequence file */
/* mt19937-1.out is attached (2001/4/2) */
/* Copyright (C) 1997, 1999 Makoto Matsumoto and Takuji Nishimura. */
/* Any feedback is very welcome. For any question, comments, */
/* see http://www.math.keio.ac.jp/matumoto/emt.html or email */
/* matumoto@math.keio.ac.jp */
/* REFERENCE */
/* M. Matsumoto and T. Nishimura, */
/* "Mersenne Twister: A 623-Dimensionally Equidistributed Uniform */
/* Pseudo-Random Number Generator", */
/* ACM Transactions on Modeling and Computer Simulation, */
/* Vol. 8, No. 1, January 1998, pp 3--30. */
#include<stdio.h>
/* Period parameters */
#define N 624
#define M 397
#define MATRIX_A 0x9908b0df /* constant vector a */
#define UPPER_MASK 0x80000000 /* most significant w-r bits */
#define LOWER_MASK 0x7fffffff /* least significant r bits */
/* Tempering parameters */
#define TEMPERING_MASK_B 0x9d2c5680
#define TEMPERING_MASK_C 0xefc60000
#define TEMPERING_SHIFT_U(y) (y >> 11)
#define TEMPERING_SHIFT_S(y) (y << 7)
#define TEMPERING_SHIFT_T(y) (y << 15)
#define TEMPERING_SHIFT_L(y) (y >> 18)
/*
* Not used
static unsigned long mt[N+1]; *//* the array for the state vector */
/*
* Not used
static int mti=N+1; *//* mti==N+1 means mt[N] is not initialized */
/* Initializing the array with a seed */
static void sgenrand(seed, gen_array)
unsigned long seed;
ULong *gen_array;
{
int i;
for (i=0;i<N;i++) {
gen_array[i] = seed & 0xffff0000;
seed = 69069 * seed + 1;
gen_array[i] |= (seed & 0xffff0000) >> 16;
seed = 69069 * seed + 1;
}
gen_array[N] = N;
}
/* Initialization by "sgenrand()" is an example. Theoretically, */
/* there are 2^19937-1 possible states as an intial state. */
/* This function allows to choose any of 2^19937-1 ones. */
/* Essential bits in "seed_array[]" is following 19937 bits: */
/* (seed_array[0]&UPPER_MASK), seed_array[1], ..., seed_array[N-1]. */
/* (seed_array[0]&LOWER_MASK) is discarded. */
/* Theoretically, */
/* (seed_array[0]&UPPER_MASK), seed_array[1], ..., seed_array[N-1] */
/* can take any values except all zeros. */
/* Not used */
#if 0
static void
lsgenrand(seed_array)
unsigned long seed_array[];
/* the length of seed_array[] must be at least N */
{
int i;
for (i=0;i<N;i++)
mt[i] = seed_array[i];
mti=N;
}
#endif
static double /* generating reals */
/* unsigned long */ /* for integer generation */
genrand(gen_array)
unsigned long *gen_array;
{
unsigned long y;
static unsigned long mag01[2]={0x0, MATRIX_A};
long mti = gen_array[N];
/* mag01[x] = x * MATRIX_A for x=0,1 */
if (mti >= N) { /* generate N words at one time */
int kk;
if (mti == N+1) /* if sgenrand() has not been called, */
sgenrand(4357); /* a default initial seed is used */
for (kk=0;kk<N-M;kk++) {
y = (gen_array[kk]&UPPER_MASK)|(gen_array[kk+1]&LOWER_MASK);
gen_array[kk] = gen_array[kk+M] ^ (y >> 1) ^ mag01[y & 0x1];
}
for (;kk<N-1;kk++) {
y = (gen_array[kk]&UPPER_MASK)|(gen_array[kk+1]&LOWER_MASK);
gen_array[kk] = gen_array[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1];
}
y = (gen_array[N-1]&UPPER_MASK)|(gen_array[0]&LOWER_MASK);
gen_array[N-1] = gen_array[M-1] ^ (y >> 1) ^ mag01[y & 0x1];
mti = 0;
}
y = gen_array[mti++];
y ^= TEMPERING_SHIFT_U(y);
y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
y ^= TEMPERING_SHIFT_L(y);
gen_array[N] = mti;
return ( (double)y * 2.3283064370807974e-10 ); /* reals */
/* return y; */ /* for integer generation */
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -