I wish to add noise to the escapement (drive) of my clock pendulum simulation. The below works “sort of”.
Either it’s bad code or a rather bad random number generator. I verified the system is C++11, which is supposed to contain a better generator. However, the sum of the noise output is varyingly ~ -2561 (instead of zero) with 1e6 points for four runs.
[Later I found code for <random> (is next)]
==========================
#include <iostream>
#include <random>
int main()
{
std::random_device seed;
std::mt19937 gen(seed());
std::uniform_int_distribution<int> dist(0, 100);
for (int i = 0; i < 10; i++)
{
std::cout << dist(gen) << '\n';
}
}
============================
My use of above:
#include <iostream>
#include <random>
int main()
{
std::random_device seed;
std::mt19937 gen(seed());
std::uniform_int_distribution<int> dist(-100, 100);
for (int i = 0; i < 100; i++)
{
std::cout << dist(gen) << '\n';
}
}
,
which didn’t appear better than my first try. (which is next)
/* initialize random seed: */
//srand (time(NULL));
srand((unsigned)time(0));
I later replaced the second random (directly above) with: (May be rewritten w/o the else, I think.)
/////////////////////////
if ( N % 2 == 0 ) {
inoise10 = inoise10;
} else {
inoise10 = -inoise10;
}
////////////////////
With ~ same result.
=================================
noise = inoise10/2;
————————
dr = dr*(1+noise);
——————
/////////////////////////////////// [
//Euler-Richardson method with quadratic dissipation (drag) and "circular error":
dr = dr*(1+noise);
// acceleration at beginning of interval:
double A = - ((4*pi*pi)/(period*period)) * sin(theta) - r*thetadot*abs(thetadot) +dr; // was (g/l)*(1+g increment) * sin(theta), ff. note: r (gamma) includes the mass
etc.
———————— ——
bc assumes “white” noise; will later try 1/f, and random walk.
And is amazed at how much c++ “ tuff” is on the internet.