本文介绍了生成高斯分布的随机数用C - 如何将一个保0和1之间的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直工作在一个函数生成rel=\"nofollow\">这种零和1



  • I've been working on a function to generate gaussian distributed random randoms between zero and 1. This website here was a great help as I basically copied the algorithm for Polar Form to get an understanding of the procedure, but I am having trouble keeping the value between 0 and 1, including 0 but excluding 1. I believe the mathematical notation for this is [0, 1) if I'm correct. Any insight you could provide would be great. On Unix, this compiles with; gcc fileName.c -lm

    #include <limits.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <math.h>
    
    int main()
    {
        int i;
        float x, w;
        for (i=0; i<50; i++)
        {
            do {
                x = 2.0 * ( (float)rand() / (float)RAND_MAX ) - 1.0;
                w = x * x;
            }while (w >= 1.0);
    
            w = (float)sqrt( (-2.0 * log( w )) / w );
            printf("%f\n", x*w);
        }
        return 0;
    }
    
    解决方案

    I believe the questioner is asking for something like a truncated gaussian distribution. You can sample such a distribution simply by generating samples from a Gaussian distribution with mean 0.5 and suitable variance, and discarding any samples that lie outside of [0,1].

    However, you might also be interested in:

    这篇关于生成高斯分布的随机数用C - 如何将一个保0和1之间的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    10-16 16:32