利用迭代法产生随机数
/*============================================================================
Name : Exercise.c
Author : Haier
Version : 1.01
Copyright : Copyright (c) 2014
Description : Random number generator in C, Compile by Code:Block
============================================================================ */
#include <stdio.h>
#include <sys/timeb.h>
#define Alpha (3.90)
/****************************************************************************
* Function : InitialRandomSeed
* Description : Initialize Random Seed
* Input : void
* Return : Random Seed
*****************************************************************************/
double InitialRandomSeed()
{
double RandSeed;
struct timeb TimebPtr;
ftime(&TimebPtr);
RandSeed=TimebPtr.millitm*0.9876543*0.001;
return RandSeed;
}
/****************************************************************************
* Function : random
* Description : Generator Random
* Input : void
* Return : Rand Number
*****************************************************************************/
double random()
{
static double RandNum=-1.0;
if(RandNum==-1.0)
{
RandNum=InitialRandomSeed();
}
else
{
RandNum=Alpha*RandNum*(1.0-RandNum);
}
return RandNum;
}
int main()
{
int i;
double randNum;
printf("%lf\n",randNum=random());
return 0;
}