C++ 连续创建 0 到 1 之间的随机数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19091527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
creating a random number between 0 to 1 continuously
提问by lakesh
I am trying to model a stock price movement in C++. I need to create a random number between 0 to 1.
我正在尝试用 C++ 模拟股票价格变动。我需要创建一个介于 0 到 1 之间的随机数。
But it seems that the random number generator value keeps increasing and is not really random.
但似乎随机数生成器的值一直在增加,并不是真正的随机。
The code looks like this:
代码如下所示:
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<time.h>
using namespace std;
int main()
{
double stockPrice = 25;
int start = 0, end = 0;
start = clock();
srand (time(NULL));
cout << (double) rand() / (double) (RAND_MAX) << endl;
system("pause");
while(stockPrice > 18)
{
if(stockPrice == 20)
{
double probability = (rand()/(double)RAND_MAX);
if(probability <= (1/10))
{
stockPrice = stockPrice-1;
}
else
{
stockPrice = stockPrice +1;
}
}
else if (stockPrice < 20)
{
double probability = (rand()/(double)RAND_MAX);
if(probability <= (1/3))
{
stockPrice = stockPrice -1;
}
else
{
stockPrice = stockPrice +1;
}
}
else
{
double probability = (rand()/(double)RAND_MAX);
if(probability <= (2/3))
{
stockPrice = stockPrice -1;
}
else
{
stockPrice = stockPrice +1;
}
}
cout << stockPrice << endl;
}
end = clock();
double t = (double)(start-end)/CLOCKS_PER_SEC;
cout << t << endl;
system("pause");
}
Not sure how to solve this.. Need some guidance...
不知道如何解决这个问题......需要一些指导......
回答by 4pie0
Need some guidance...
需要一些指导...
guidance 1:
指导1:
correct comparisons, you should use
正确的比较,你应该使用
double probability = (rand()/(double)(RAND_MAX + 1));
^
for better scaling
because currently in line if(probability <= (1/10))
you are comparing with 0 because of conversion 1/10
1/3
and 2/3
to integer
因为目前if(probability <= (1/10))
您正在与 0 进行比较,因为转换1/10
1/3
和2/3
整数
guidance 2:
指导2:
after all you might use generator with better statistical properties
毕竟,您可能会使用具有更好统计属性的生成器
#include <random>
#include <iostream>
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> dis(0, 1);
double uniformOn01 = dis(gen);
回答by Pete Becker
if(probability <= (1/3))
{
stockPrice = stockPrice -1;
}
else
{
stockPrice = stockPrice +1;
}
Since probabililty
is never negative, this code will almost alwaysincrement the value of stockPrice
. The only time it won't is when probability
is 0. That's because 1/3
is integerdivision, and its value is 0. Change all of these fraction to something like 1.0/3
and things will be much better. And this has nothingto do with the quality of the random number generator. Some folks get so exercised when they see rand
that they don't see anything else.
由于probabililty
永远不会为负,因此此代码几乎总是会增加 的值stockPrice
。唯一不会的时候是 whenprobability
是 0。那是因为1/3
是整数除法,它的值是 0。将所有这些分数更改为类似的东西1.0/3
,事情会好得多。而这与随机数生成器的质量无关。有些人在看到rand
他们没有看到其他任何东西时会变得如此锻炼。
However, there isa flaw in the scaling in the code. Instead of
然而,就是在代码中的缩放一大败笔。代替
double probability = (rand()/(double)RAND_MAX);
use
用
double probability = (rand()/(double)(RAND_MAX + 1));
As originally written, the value of probability
will be 1 whenever rand()
produces the value RAND_MAX
, and it will produce other values much more often.
正如最初编写的那样,probability
每当rand()
产生 value时, 的 value将为 1 RAND_MAX
,并且它会更频繁地产生其他值。
回答by Michael
In all of your cases where you compare the probability you have integer division, which all result in 0... instead of (2/3)
you wanted (2/(double)3)
or 2/3.0
.
在您比较概率的所有情况下,您都有整数除法,结果都是 0... 而不是(2/3)
您想要的(2/(double)3)
or 2/3.0
。