File: brownian_motion_binary.cpp

package info (click to toggle)
labplot 2.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 70,348 kB
  • sloc: cpp: 145,806; ansic: 4,534; python: 881; yacc: 540; xml: 357; sh: 185; awk: 35; makefile: 7
file content (38 lines) | stat: -rw-r--r-- 858 bytes parent folder | download
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
37
38
#include <fstream>
#include <iostream>
#include <math.h>

extern "C" {
#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));
		}
	}
}