source: cpp/frams/util/rndutil.cpp @ 215

Last change on this file since 215 was 197, checked in by Maciej Komosinski, 11 years ago

GDK used by developers since 1999, distributed on the web since 2002

  • Property svn:eol-style set to native
File size: 1.4 KB
RevLine 
[121]1// This file is a part of the Framsticks GDK.
[197]2// Copyright (C) 1999-2014  Maciej Komosinski and Szymon Ulatowski.  See LICENSE.txt for details.
[121]3// Refer to http://www.framsticks.com/ for further information.
4
[109]5#include "rndutil.h"
6#include <common/nonstd_math.h>
7#include <stdlib.h>
8
9unsigned short pseudornd(short x)
10{
[154]11        static long seed = 0;
12        long y;
13        if (x <= 0) { seed = -x; return 0; }
14        seed = (y = (3677 * seed + 3680) & 0x7fffffff) - 1;
15        return (unsigned short)(((unsigned short)y) % (x)); //rzutowanie y->unsigned short to pewnie blad bo zmniejsza wartosc ktorej sie potem robi modulo, ale pseudornd sluzy chyba tylko do generowania randomowych world map? i modulo i tak jest tam bardzo male, lepiej niczego nie zmieniac bo po co maja pliki z ustawieniami zmienic swoje przypadkowe znaczenie
[109]16}
17
18double CustomRnd(double *tab)
19{
[154]20        double *range = tab + 1 + 2 * randomN((int)(0.5 + tab[0]));
21        return range[0] + rnd0N(range[1] - range[0]);
[112]22}
[109]23
24double RandomGener::Uni(double begin, double end)
25{
[154]26        return begin + rnd01*(end - begin);
[109]27}
28
29double RandomGener::GaussStd()
30{
[154]31        if (isNextGauss) { isNextGauss = 0; return nextGauss; }
32        double v1, v2, s;
33        do {
34                v1 = 2 * rnd01 - 1; //-1..1
35                v2 = 2 * rnd01 - 1; //-1..1
36                s = v1*v1 + v2*v2;
37        } while (s >= 1);
38        double mult = sqrt(-2 * log(s) / s);
39        nextGauss = v2*mult;
40        isNextGauss = 1;
41        return v1*mult;
[109]42}
43
[154]44double RandomGener::Gauss(double m, double s)
45{
46        return m + s*GaussStd();
47}
[109]48
49RandomGener RndGen;
Note: See TracBrowser for help on using the repository browser.