|
1 | 1 | /** \file |
2 | | - * \brief IMPLEMENTATION FOR PSEUDORANDOM NUMBER GENERATOR "randg". |
3 | | - * @date 2026-04-30 (last modification) |
4 | | - * @note When using the full RTM library there is no need to add this file in your project! |
5 | | - * ****************************************************************************************************************** */ |
| 2 | + * \brief IMPLEMENTATION FOR PSEUDORANDOM NUMBER GENERATOR "randg". */ |
| 3 | +/* ========================================================== */ |
| 4 | +/** @date 2026-05-11 (last modification) */ |
6 | 5 |
|
7 | 6 | #include <stdlib.h> |
8 | 7 | #include <stdio.h> |
@@ -44,48 +43,51 @@ void srandg(short int srandg_init_val) |
44 | 43 | float randg() |
45 | 44 | { |
46 | 45 | //TODO - MUTEXOWE ZABEZPIECZENIE ::rand() na wielowątkowość!!! |
47 | | - static short int inext, inextp; |
48 | | - static int32 ma[55]; |
49 | | - int32 mj, mk, mbig = 1000000000; |
50 | | - short int i, k; |
51 | | - |
52 | | - if (di < 0) |
53 | | - { |
54 | | - mj = 161803398 - abs(di); |
55 | | - mj %= mbig; |
56 | | - ma[54] = mj; |
57 | | - mk = 1; |
58 | | - for (i=1; i <= 54; i++) |
| 46 | + static int16_t inext, inextp; |
| 47 | + static int32_t ma[55]; |
| 48 | + int32_t mj, mk, mbig = 1000000000; |
| 49 | + int16_t i, k; |
| 50 | + |
| 51 | + if (di < 0) |
59 | 52 | { |
60 | | - k = (21 * i) % 55; |
61 | | - ma[k - 1] = mk; |
62 | | - mk = mj - mk; |
63 | | - if (mk < 0) |
64 | | - mk += mbig; |
65 | | - mj = ma[k - 1]; |
| 53 | + mj = 161803398 - abs(di); |
| 54 | + mj %= mbig; |
| 55 | + ma[54] = mj; |
| 56 | + mk = 1; |
| 57 | + for (i=1; i <= 54; i++) |
| 58 | + { |
| 59 | + k = (21 * i) % 55; |
| 60 | + ma[k - 1] = mk; |
| 61 | + mk = mj - mk; |
| 62 | + if (mk < 0) |
| 63 | + mk += mbig; |
| 64 | + mj = ma[k - 1]; |
| 65 | + }; |
| 66 | + for (k = 1; k <= 4; k++) |
| 67 | + for (i = 1; i <= 55; i++) |
| 68 | + { |
| 69 | + ma[i - 1] -= ma[(i + 30) % 55]; |
| 70 | + if (ma[i - 1] < 0) |
| 71 | + ma[i - 1] += mbig; |
| 72 | + }; |
| 73 | + inext = 0; |
| 74 | + inextp = 31; |
| 75 | + di= 1; |
66 | 76 | }; |
67 | | - for (k = 1; k <= 4; k++) |
68 | | - for (i = 1; i <= 55; i++) |
69 | | - { |
70 | | - ma[i - 1] -= ma[(i + 30) % 55]; |
71 | | - if (ma[i - 1] < 0) |
72 | | - ma[i - 1] += mbig; |
73 | | - }; |
74 | | - inext = 0; |
75 | | - inextp = 31; |
76 | | - di= 1; |
77 | | - }; |
78 | | - inext++; |
79 | | - if (inext == 56) |
80 | | - inext = 1; |
81 | | - inextp++; |
82 | | - if (inextp == 56) |
83 | | - inextp = 1; |
84 | | - mj=ma[inext - 1] - ma[inextp - 1]; |
85 | | - if (mj < 0) |
86 | | - mj += mbig; |
87 | | - ma[inext - 1] = mj; |
88 | | - return (mj * 1.e-9f);/* CZY MOŻE BYĆ 1.e-9f czy powinno BYĆ MAX. DLA DOUBLE? */ |
| 77 | + inext++; |
| 78 | + if (inext == 56) |
| 79 | + inext = 1; |
| 80 | + inextp++; |
| 81 | + if (inextp == 56) |
| 82 | + inextp = 1; |
| 83 | + mj=ma[inext - 1] - ma[inextp - 1]; |
| 84 | + if (mj < 0) |
| 85 | + mj += mbig; |
| 86 | + ma[inext - 1] = mj; |
| 87 | + float tmp=(mj * 1.e-9f); //assert(tmp<1.0f); |
| 88 | + if(tmp>=1) /* Czasem się tak jednak zdarza... */ |
| 89 | + tmp=0.999999940395355224609375f; |
| 90 | + return tmp;/* CZY MOŻE BYC 1.e-9f czy winno BYC DOUBLE */ |
89 | 91 | } |
90 | 92 |
|
91 | 93 | float randnorm() |
@@ -127,6 +129,6 @@ float randexp() |
127 | 129 | return (float)( -log(dum) ); |
128 | 130 | } |
129 | 131 |
|
130 | | -/* *************************** */ |
131 | | -/* FROM OLD Numerical Recipies */ |
132 | | -/* *************************** */ |
| 132 | +/* ********************************************* */ |
| 133 | +/* For WBRTM (light) FROM OLD Numerical Recipies */ |
| 134 | +/* ********************************************* */ |
0 commit comments