C随机范围内的数字

//改编自lua的math.random函数
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
            
#define RAND_MAX 2147483647
#define lua_Number double
            
int math_random(int down_limit, int up_limit)
{           
    lua_Number r = (lua_Number)(rand() % RAND_MAX) / (lua_Number)RAND_MAX;
            
    lua_Number l = down_limit;
    lua_Number u = up_limit;
            
    lua_Number ret = floor(r * (u - l + 1 ) + l); 
            
    return (int)ret;
}           
            
int main(int argc, char* argv[])
{           
    srand(time(NULL));
    printf("RAND_MAX %d\n", RAND_MAX);
            
            
    int i = 0;
    for (i = 0; i < 100; ++i) {
        int ret = math_random(-3, 3);
		printf("%d\n", ret);                            
    }       
            
    return 0;
} 

点赞