File: brownian_motion_binary.cpp

package info (click to toggle)
labplot 2.12.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 78,528 kB
  • sloc: cpp: 241,047; ansic: 6,324; python: 915; xml: 400; yacc: 237; sh: 221; awk: 35; makefile: 11
file content (36 lines) | stat: -rw-r--r-- 843 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <fstream>
#include <iostream>
#include <math.h>

#include <gsl/gsl_randist.h>
#include <gsl/gsl_rng.h>

int main() {
	std::string fileName = "out_1M_5pathes.bin";
	int count = 1000000;
	int pathes = 5;

	double delta = 0.25;
	int dt = 1;
	double sigma = std::pow(delta, 2) * dt;
	double path[pathes] = {0.0};

	std::ofstream ostrm(fileName, std::ios::binary);

	// create a generator chosen by the environment variable GSL_RNG_TYPE
	gsl_rng_env_setup();
	const gsl_rng_type* T = gsl_rng_default;
	gsl_rng* r = gsl_rng_alloc(T);
	gsl_rng_set(r, 12345);

	// data
	for (int i = 0; i < count; ++i) {
		double x = i * dt;
		ostrm.write(reinterpret_cast<char*>(&x), sizeof(double));

		for (int p = 0; p < pathes; ++p) {
			path[p] += gsl_ran_gaussian(r, sigma);
			ostrm.write(reinterpret_cast<char*>(&path[p]), sizeof(double));
		}
	}
}