乱数発生例1

乱数発生例題1(list_12.c)
#include <stdio.h>
#include <math.h>

/* 0..1.0 までの適当な実数値の出力 */
double drandom(double wqmax,unsigned long* idum)
{
    *idum=(*idum)*1103515245 + 12345;
    return wqmax*((double)(*idum/65536 % 32768) / (double)32767);
}

int main()
{
    int count=0;
    unsigned long idum=time(NULL); /* 乱数発生用 */
    double L,minth,wq;
    double wqmax = 10;
    
    /* L,minth を適当な値に設定 */
    L     = 3.2445;
    minth = 1.4890;

    for(;;){
        wq = drandom(wqmax,&idum);
        if(pow((1-wq),L+1)-1 < minth) break; /* 注意!! */
        count++;
    }
    printf("count<%d>:wq = %lf\n",count,wq);
  
    return 0;
}
実行結果
Gami[798]% list_12.exe
count<3>:wq = 0.881680
Gami[799]%
inserted by FC2 system