蓝森林首页 | 返回主页 | 本站地图 | 站内搜索 | 联系信箱 |
 您目前的位置:首页 > 自由软件 > 技术交流 > 应用编程


    

蓝森林 http://www.lslnet.com 2006年6月6日 10:18


[0,1]--》[0,255]的方法?

把[0,1]之间的随机实数,转换成[0,255]的整数.有没有好的方法,不用直接*255的。
我试了下,用直接*,系统要消耗很多时间,实数的相乘,要太多的时钟周期了。

兄弟们有没有这样的想法,share一下。

[code]#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>

int main(int argc, char *argv[])
{
        double d;
        unsigned char u;

        srand(time(NULL));
        d = ((double) rand()) / UINT_MAX;
        u = *(char *)&d;
        printf("%d\n", u);
        return 0;
}
[/code]

-->

这也行吗?

你可以试试呀。

我去试试



Copyright © 1999-2000 LSLNET.COM. All rights reserved. 蓝森林网站 版权所有。 E-mail : webmaster@lslnet.com