简体   繁体   English

用C语言在直方图中绘制随机数

[英]Plotting random numbers in a historgram in C language

I use a code from "Numerical Recipes in C book" to generate a uniform random number which is named as function float ran1(long *idum) .我使用“C 书中的数字食谱”中的代码生成一个统一的随机数,该数命名为函数float ran1(long *idum) It produces a uniform random number between 0 and 1 .它产生一个介于0 and 1之间的均匀随机数。 I am trying to bin these uniform random numbers and make a histogram.我正在尝试对这些统一的随机数进行分类并制作直方图。 I wrote the below code, however when I plot the bins versus the frequency, I do not get the uniform distribution.我写了下面的代码,但是当我绘制 bin 与频率的关系时,我没有得到均匀分布。

Can someone help me know what the problem is?有人可以帮我知道问题是什么吗?

#include<stdio.h>
#include<math.h>
#include<stdlib.h>

#define IA 16807
#define IM 2147483647
#define AM (1.0/IM)
#define IQ 127773
#define IR 2836
#define NTAB 32
#define NDIV (1+(IM-1)/NTAB)
#define EPS 1.2e-7
#define RNMX (1.0-EPS)


float ran1(long *idum);

int main(){
    
    double delta, x;
    int i , ibin ;
    long idum ;
    int histog[500] ; 
    
    
    delta=0.02 ;
    
    idum=-1 ;// necessary to generate the uniform random number, must be negative integer
    
    FILE *file1;
    
    file1=fopen("guassians.dat","w") ;
    
    
    for (i=0 ; i<=10000 ; ++i) {
        
        x=ran1(&idum) ; // here ran1(&idum) returns a uniform random number
      
        
        ibin=round(x/delta);
        
        if ( abs(ibin) < 250 ) {
            
            histog[ibin]=histog[ibin] + 1 ;
        }
    }
    
    for (ibin=-250 ; ibin <= 250 ; ++ibin){
        
        fprintf(file1, "%0.10lf \t %d \n", ibin*delta , histog[ibin]);
        
    }
    

        
    fclose(file1);
        
}



float ran1(long *idum){
    
    int j ;
    long k;
    static long iy=0;
    static long iv[NTAB];
    float temp ;
    
    if (*idum <= 0 || !iy) {
        
        if (-(*idum) < 1) *idum=1 ;
        else *idum = -(*idum) ;
        for (j=NTAB+7 ; j>=0 ; j--) {
            k=(*idum)/IQ ;
            *idum=IA*(*idum-k*IQ)-IR*k ;
            if (*idum < 0) *idum += IM;
            if (j < NTAB) iv[j] = *idum;
        }
        iy=iv[0] ;
    }
    k=(*idum)/IQ ;
    *idum=IA*(*idum-k*IQ)-IR*k ;
    if (*idum < 0) *idum += IM ;
    j=iy/NDIV ;
    iy=iv[j] ;
    iv[j] = *idum ;
    if ((temp=AM*iy) > RNMX) return RNMX ;
    else return temp;
        
}


在此处输入图片说明

At least these problems至少这些问题

  • int histog[500] is not initialized. int histog[500]未初始化。

  • Indexing histog[ibin] with negative values is undefined behavior .用负值索引histog[ibin]未定义的行为

I found out that I should use pointers instead of arrays.我发现我应该使用指针而不是数组。 Now, the following code generates random numbers between [0:1] (exclusive) which are uniformly distributed,现在,以下代码生成均匀分布的 [0:1](不包括)之间的随机数,

#include<stdio.h>
#include<math.h>
#include<stdlib.h>

#define IA 16807
#define IM 2147483647
#define AM (1.0/IM)
#define IQ 127773
#define IR 2836
#define NTAB 32
#define NDIV (1+(IM-1)/NTAB)
#define EPS 1.2e-7
#define RNMX (1.0-EPS)


float ran1(long *idum){

int j ;
long k;
static long iy=0;
static long iv[NTAB];
float temp ;

if (*idum <= 0 || !iy) {
    
    if (-(*idum) < 1) *idum=1 ;
    else *idum = -(*idum) ;
    for (j=NTAB+7 ; j>=0 ; j--) {
        k=(*idum)/IQ ;
        *idum=IA*(*idum-k*IQ)-IR*k ;
        if (*idum < 0) *idum += IM;
        if (j < NTAB) iv[j] = *idum;
    }
    iy=iv[0] ;
}
k=(*idum)/IQ ;
*idum=IA*(*idum-k*IQ)-IR*k ;
if (*idum < 0) *idum += IM ;
j=iy/NDIV ;
iy=iv[j] ;
iv[j] = *idum ;
if ((temp=AM*iy) > RNMX) return RNMX ;
else return temp;
    
}

int main(){

double delta ,x ;
int i , ibin, maxbin;
long idum=-1 ;
int *p ;


maxbin=100 ;

delta=1.0/maxbin ;

p= calloc(maxbin,sizeof(int)) ;

FILE *myfile1 ;

myfile1 = fopen("gs.dat","w") ;

for (i=0; i<500000 ; ++i) {

    x=ran1(&idum) ;

    ibin=round(x/delta);

    if ( ibin >= 0 && ibin< 100){

        *(p+ibin)=*(p+ibin) + 1 ;
     }

  }

for (ibin=0 ; ibin < 100 ; ++ibin){

       fprintf(myfile1, "%lf \t %lf \n", ibin*delta , *(p+ibin)/(500000*delta));
 }


free(p);

fclose(myfile1) ;
}

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM