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

Last change on this file since 336 was 286, checked in by Maciej Komosinski, 10 years ago

Updated headers

  • Property svn:eol-style set to native
File size: 1.4 KB
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
4
5#include "rndutil.h"
6#include <common/nonstd_math.h>
7#include <cstdint>
8#include <stdlib.h>
9
10unsigned short pseudornd(short x)
11{
12        static int32_t seed = 0;
13        int32_t y;
14        if (x <= 0) { seed = -x; return 0; }
15        seed = (y = (3677 * seed + 3680) & 0x7fffffff) - 1;
16        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
17}
18
19double CustomRnd(double *tab)
20{
21        double *range = tab + 1 + 2 * randomN((int)(0.5 + tab[0]));
22        return range[0] + rnd0N(range[1] - range[0]);
23}
24
25double RandomGener::Uni(double begin, double end)
26{
27        return begin + rnd01*(end - begin);
28}
29
30double RandomGener::GaussStd()
31{
32        if (isNextGauss) { isNextGauss = 0; return nextGauss; }
33        double v1, v2, s;
34        do {
35                v1 = 2 * rnd01 - 1; //-1..1
36                v2 = 2 * rnd01 - 1; //-1..1
37                s = v1*v1 + v2*v2;
38        } while (s >= 1);
39        double mult = sqrt(-2 * log(s) / s);
40        nextGauss = v2*mult;
41        isNextGauss = 1;
42        return v1*mult;
43}
44
45double RandomGener::Gauss(double m, double s)
46{
47        return m + s*GaussStd();
48}
49
50RandomGener RndGen;
Note: See TracBrowser for help on using the repository browser.