您的位置:首页 > 编程语言 > C语言/C++

c++中怎么产生0-1之间的随机数

2015-03-03 12:58 309 查看
http://zhidao.baidu.com/question/688719686574659564.html

#include <iostream>
#include <time.h>

using namespace std;

double GetRand();

int main()
{
cout << GetRand() << endl;
return 0;
}

double GetRand()
{
srand(time(NULL));

int iRand = rand();

iRand %= 10000; // 取0 ~ 9999

return iRand / 10000.0; // 注意加".0"

// PS:保留几位小数就合适求舍,求商
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: