Generate random numbers uniformly over an entire range

后端 未结 17 2345
野性不改
野性不改 2020-11-22 04:21

I need to generate random numbers within a specified interval, [max;min].

Also, the random numbers should be uniformly distributed over the interval, not located to

17条回答
  •  鱼传尺愫
    2020-11-22 04:53

    This is not the code, but this logic may help you.

    static double rnd(void)
    {
       return (1.0 / (RAND_MAX + 1.0) * ((double)(rand())) );
    }
    
    static void InitBetterRnd(unsigned int seed)
    {
        register int i;
        srand( seed );
        for( i = 0; i < POOLSIZE; i++){
            pool[i] = rnd();
        }
    }
    
     // This function returns a number between 0 and 1
     static double rnd0_1(void)
     {
        static int i = POOLSIZE-1;
        double r;
    
        i = (int)(POOLSIZE*pool[i]);
        r = pool[i];
        pool[i] = rnd();
        return (r);
    }
    

提交回复
热议问题