我正在尝试用 C++ 对股票价格变动进行建模。我需要创建一个介于 0 到 1 之间的随机数。
但似乎随机数生成器的值不断增加,并不是真正随机的。
代码如下所示:
#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");
}
不知道如何解决这个问题..需要一些指导...