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

Last change on this file since 980 was 896, checked in by Maciej Komosinski, 5 years ago

Replaced #defined macros for popular random-related operations with functions

  • 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#ifndef IPHONE
8#include <cstdint>
9#endif
10#include <stdlib.h>
11
12unsigned short pseudornd(short x)
13{
14        static int32_t seed = 0;
15        int32_t y;
16        if (x <= 0) { seed = -x; return 0; }
17        seed = (y = (3677 * seed + 3680) & 0x7fffffff) - 1;
18        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
19}
20
21double CustomRnd(double *tab)
22{
23        double *range = tab + 1 + 2 * rndUint((int)(0.5 + tab[0]));
24        return range[0] + rndDouble(range[1] - range[0]);
25}
26
27double RandomGener::Uni(double begin, double end)
28{
29        return begin + rndDouble(end - begin);
30}
31
32double RandomGener::GaussStd()
33{
34        if (isNextGauss) { isNextGauss = 0; return nextGauss; }
35        double v1, v2, s;
36        do {
37                v1 = rndDouble(2) - 1; //-1..1
38                v2 = rndDouble(2) - 1; //-1..1
39                s = v1*v1 + v2*v2;
40        } while (s >= 1);
41        double mult = sqrt(-2 * log(s) / s);
42        nextGauss = v2*mult;
43        isNextGauss = 1;
44        return v1*mult;
45}
46
47double RandomGener::Gauss(double m, double s)
48{
49        return m + s*GaussStd();
50}
51
52RandomGener RndGen;
Note: See TracBrowser for help on using the repository browser.