Funktion für Normal- und Exponentialverteilung
-
Hallo,
ich suche eine C++ Funktion die Normal- und Exponentialverteilung ausgerechnet, ich brauche dies für eine Simulation, wo gewisse Ereignisse normalverteilt oder exponentialverteilt eintreten.
Wer kann mir da helfen?cash21
-
wenn du einen zufallsgenerator für so etwas meinst:
#include <cmath> class Randint { // gleichverteilt im Intervall [0,max] unsigned long randx; public: Randint (long s=0) { randx=s; } void seed (long s) { randx=s; } long abs (long x) { return x & 0x7fffffff; } static double max() { return 2147483648.0; } long draw() { return randx = randx * 1103515245 + 12345; } double fdraw() { return abs(draw())/max(); } long operator()() { return abs(draw()); } }; class Urand : public Randint { // gleichverteilt in [0,n[ long n; public: Urand (long nn) { n = nn; } long operator()() { long r = n*fdraw(); return (r==n) ? n-1 : r; } }; class Erand : public Randint { // exponentiell veteilte Zufallszahl long average; public: Erand (long a) { average=a; } long operator()() { return -average * std::log((max()-draw())/max()+.5); } };
ist btw, aus "Die Programmiersprache C++"
-
Das sieht dich schon mal ganz gut aus. Gibt es da auch eine kleine Doku dazu? Und wird da einmal die Klammern überladen bei long operator()()?
-
Hi, hast du dafür auch noch ein kurzes Main-Programm. Damit ich mal die Aufrufe sehen. Sonst sieht das sehr gut. Danke schonmal.
-
int main () { Randint rand; int zahl = rand(); }
oder einfach draw (fdraw) aufrufen.
-
cash21 schrieb:
Und wird da einmal die Klammern überladen bei long operator()()?
Nicht die Klammern. Der Funktionsaufrufoperator (der allerdings durch ein paar Klammern dargestellt wird.)
-
Bei boost gibt es alles, z.B. Normalverteilung siehe hier:
http://www.c-plusplus.net/forum/viewtopic.php?t=84597Wer es komplett selbst basteln und verstehen will:
http://www.c-plusplus.net/forum/viewtopic.php?t=84017&start=30