您的位置:首页 > 其它

利用/dev/random生成随机数

2008-02-25 15:42 447 查看

int random_number(int min, int max)




...{


    static int dev_random_fd = -1;


    char *next_random_byte;


    int bytes_to_read;


    unsigned random_value;




    assert( max > min );






    if( dev_random_fd == -1)...{


        dev_random_fd = open( "/dev/random", O_RDONLY);


        assert( dev_random_fd != -1);


    }




    next_random_byte = ( char * ) &random_value;


    bytes_to_read = sizeof( random_value );






    do ...{


        int bytes_read;


        bytes_read = read( dev_random_fd, next_random_byte, bytes_to_read );


        bytes_to_read -= bytes_read;


        next_random_byte += bytes_read;


    } while ( bytes_to_read > 0 );




    return min + ( random_value % ( max - min + 1 ));


}



 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  random byte