Last change
on this file since 1299 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
|
Rev | Line | |
---|
[286] | 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. |
---|
[121] | 4 | |
---|
[109] | 5 | #include "rndutil.h" |
---|
| 6 | #include <common/nonstd_math.h> |
---|
[424] | 7 | #ifndef IPHONE |
---|
[247] | 8 | #include <cstdint> |
---|
[424] | 9 | #endif |
---|
[109] | 10 | #include <stdlib.h> |
---|
| 11 | |
---|
| 12 | unsigned short pseudornd(short x) |
---|
| 13 | { |
---|
[247] | 14 | static int32_t seed = 0; |
---|
| 15 | int32_t y; |
---|
[154] | 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 |
---|
[109] | 19 | } |
---|
| 20 | |
---|
| 21 | double CustomRnd(double *tab) |
---|
| 22 | { |
---|
[896] | 23 | double *range = tab + 1 + 2 * rndUint((int)(0.5 + tab[0])); |
---|
| 24 | return range[0] + rndDouble(range[1] - range[0]); |
---|
[112] | 25 | } |
---|
[109] | 26 | |
---|
| 27 | double RandomGener::Uni(double begin, double end) |
---|
| 28 | { |
---|
[896] | 29 | return begin + rndDouble(end - begin); |
---|
[109] | 30 | } |
---|
| 31 | |
---|
| 32 | double RandomGener::GaussStd() |
---|
| 33 | { |
---|
[154] | 34 | if (isNextGauss) { isNextGauss = 0; return nextGauss; } |
---|
| 35 | double v1, v2, s; |
---|
| 36 | do { |
---|
[896] | 37 | v1 = rndDouble(2) - 1; //-1..1 |
---|
| 38 | v2 = rndDouble(2) - 1; //-1..1 |
---|
[154] | 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; |
---|
[109] | 45 | } |
---|
| 46 | |
---|
[154] | 47 | double RandomGener::Gauss(double m, double s) |
---|
| 48 | { |
---|
| 49 | return m + s*GaussStd(); |
---|
| 50 | } |
---|
[109] | 51 | |
---|
| 52 | RandomGener RndGen; |
---|
Note: See
TracBrowser
for help on using the repository browser.