所以,我在这里有一个小问题,有一个相当大的for循环.这个程序是为了模拟每个步骤100步 - 我想要做的是让程序执行100万次这些随机游走模拟(随机游走是通过嵌套for循环完成的,主要是通过模拟硬币的翻转,如您可以在下面的代码中看到)并从每个随机游走中获取结果位移,并将它们打印到控制台窗口和指定的输出文件.
但是,我的代码似乎是从嵌套的for循环中添加x的每个最终值,然后将它们打印到控制台窗口(并保存到输出文件) - 我不想要这个,我只想要每个的独立net结果对于j的每个值(其范围从1到1E6,由外部for循环指定)输出随机游走.
因此,任何帮助将不胜感激.另外,我非常感谢你不仅仅引用一些代码供我使用,而是向我解释我的程序逻辑出错的地方以及原因.
在此先感谢,我的代码如下!
#include <iostream>
#include <ctime>
#include <fstream>
using namespace std;
int main(void) {
const unsigned IM = 1664525;
const unsigned IC = 1013904223;
const double zscale = 1.0/0xFFFFFFFF; //Scaling factor for random double between 0 and 1
unsigned iran = time(0); //Seeds the random-number generator from the system time
const int nsteps(100); //Number of steps
const int nwalks(1E6);
int x(0); // Variable to count step forward/back
ofstream randout;
randout.open("randomwalkdata2.txt");
// Table headers for …Run Code Online (Sandbox Code Playgroud)