diff options
| author | Christoph Berg <myon@debian.org> | 2025-09-16 12:37:12 +0200 |
|---|---|---|
| committer | Christoph Berg <myon@debian.org> | 2025-09-16 12:57:29 +0200 |
| commit | 5a4967c577ad245a2117b7b2f71c13b1212fe285 (patch) | |
| tree | 4d09343cfe757239895a0dd9b399242d2a07d7c2 /lib/superfox/qpc/np_rnd.c | |
| parent | 23c903a09fb1d73fd20b50323cd80cfb3aba8799 (diff) | |
Merge 2.7 changes, I had uploaded -1 from the wrong branch.debian/3.0.0_rc1+repack-2
Diffstat (limited to 'lib/superfox/qpc/np_rnd.c')
| -rwxr-xr-x | lib/superfox/qpc/np_rnd.c | 270 |
1 files changed, 135 insertions, 135 deletions
diff --git a/lib/superfox/qpc/np_rnd.c b/lib/superfox/qpc/np_rnd.c index df2afacc..1d792c6b 100755 --- a/lib/superfox/qpc/np_rnd.c +++ b/lib/superfox/qpc/np_rnd.c @@ -1,135 +1,135 @@ -// ------------------------------------------------------------------------------ -// np_rnd.c -// Functions to generate random numbers with uniform/gaussian probability distributions -// -// (c) 2024 - Nico Palermo, IV3NWV - Microtelecom Srl, Italy -// ------------------------------------------------------------------------------ -// -// This source is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// This file is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this source distribution. -// If not, see <http://www.gnu.org/licenses/>. - -#include "np_rnd.h" - -#if _WIN32 // note the underscore: without it, it's not msdn official! - // Windows (x64 and x86) - #include <windows.h> // required only for GetTickCount(...) - #define K_RAND_MAX UINT_MAX -#elif _SVID_SOURCE || _XOPEN_SOURCE || __unix__ || (defined (__APPLE__) && defined(__MACH__)) /* POSIX or Unix or Apple */ - #include <stdlib.h> - #define rand_s(x) (*x)=(unsigned int)lrand48() // returns unsigned integers in the range 0..0x7FFFFFFF - #define K_RAND_MAX 0x7FFFFFFF // that's the max number - // generated by lrand48 -#else - #error "No good quality PRNG found" -#endif - - -void np_normrnd_real(float *dst, int nitems, float mean, float stdev) -{ - // for odd or even nitems - - unsigned int r; - float phi=0, u=0; - int set = 0; - float scalephi = (M_2PI / (1.0f + K_RAND_MAX)); - float scaleu = (1.0f / (1.0f + K_RAND_MAX)); - - - while (nitems--) - if (set==1) { - // generate a second normally distributed number - *dst++ = sinf(phi)*u*stdev+mean; - set = 0; - } - else { - // generate a uniform distributed phase phi in the range [0,2*PI) - rand_s((unsigned int*)&r); phi = scalephi * r; - - // generate a uniform distributed random number u in the range (0,1] - rand_s((unsigned int*)&r); u = scaleu * (1.0f + r); - - // generate normally distributed number - u = (float)sqrt(-2.0f * log(u)); - *dst++ = cosf(phi) * u * stdev + mean; - - set=1; - } -} - -void np_normrnd_cpx(float* dst, int nitems, float mean, float stdev) -{ - // as qpc_normrnd_real, but generates always nitems complex numbers (2*nitems reals) - - unsigned int r; - float phi = 0, u = 0; - // JHT int set = 0; - float scalephi = (M_2PI / (1.0f + K_RAND_MAX)); - float scaleu = (1.0f / (1.0f + K_RAND_MAX)); - - while (nitems--) { - // generate a uniform distributed phase phi in the range [0,2*PI) - rand_s((unsigned int*)&r); phi = scalephi * r; - - // generate a uniform distributed random number u in the range (0,1] - rand_s((unsigned int*)&r); u = scaleu * (1.0f + r); - - // generate normally distributed real and imaginary parts - u = (float)sqrt(-2.0f * log(u)); - *dst++ = cosf(phi) * u * stdev + mean; - *dst++ = sinf(phi) * u * stdev + mean; - } -} - -void np_unidrnd(unsigned int* dst, int nitems, unsigned int nsetsize) -{ - // generate uniform unsigned int random numbers in the range [0..nsetsize) - - unsigned int r; - float u; - float scaleu = (1.0f / (1.0f + K_RAND_MAX)); - - while (nitems--) { - rand_s((unsigned int*)&r); u = scaleu * nsetsize * r; - *dst++ = (unsigned int)floorf(u); - } - -} -void np_unidrnd_uc(unsigned char* dst, int nitems, unsigned char nsetsize) -{ - // generate uniform unsigned char random numbers in the range [0..nsetsize) - - unsigned int r; - float u; - float scaleu = (1.0f / (1.0f + K_RAND_MAX)); - - while (nitems--) { - rand_s((unsigned int*)&r); u = scaleu * nsetsize * r; - *dst++ = (unsigned char)floorf(u); - } - -} - -void np_unifrnd(float* dst, int nitems, float fmax) -{ - // generate uniform float random numbers in the range [0..fmax) - - unsigned int r; - float u; - float scaleu = (1.0f / (1.0f + K_RAND_MAX)); - - while (nitems--) { - rand_s((unsigned int*)&r); u = scaleu * fmax * r; - *dst++ = u; - } - -} +// ------------------------------------------------------------------------------
+// np_rnd.c
+// Functions to generate random numbers with uniform/gaussian probability distributions
+//
+// (c) 2024 - Nico Palermo, IV3NWV - Microtelecom Srl, Italy
+// ------------------------------------------------------------------------------
+//
+// This source is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+// This file is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this source distribution.
+// If not, see <http://www.gnu.org/licenses/>.
+
+#include "np_rnd.h"
+
+#if _WIN32 // note the underscore: without it, it's not msdn official!
+ // Windows (x64 and x86)
+ #include <windows.h> // required only for GetTickCount(...)
+ #define K_RAND_MAX UINT_MAX
+#elif _SVID_SOURCE || _XOPEN_SOURCE || __unix__ || (defined (__APPLE__) && defined(__MACH__)) /* POSIX or Unix or Apple */
+ #include <stdlib.h>
+ #define rand_s(x) (*x)=(unsigned int)lrand48() // returns unsigned integers in the range 0..0x7FFFFFFF
+ #define K_RAND_MAX 0x7FFFFFFF // that's the max number
+ // generated by lrand48
+#else
+ #error "No good quality PRNG found"
+#endif
+
+
+void np_normrnd_real(float *dst, int nitems, float mean, float stdev)
+{
+ // for odd or even nitems
+
+ unsigned int r;
+ float phi=0, u=0;
+ int set = 0;
+ float scalephi = (M_2PI / (1.0f + K_RAND_MAX));
+ float scaleu = (1.0f / (1.0f + K_RAND_MAX));
+
+
+ while (nitems--)
+ if (set==1) {
+ // generate a second normally distributed number
+ *dst++ = sinf(phi)*u*stdev+mean;
+ set = 0;
+ }
+ else {
+ // generate a uniform distributed phase phi in the range [0,2*PI)
+ rand_s((unsigned int*)&r); phi = scalephi * r;
+
+ // generate a uniform distributed random number u in the range (0,1]
+ rand_s((unsigned int*)&r); u = scaleu * (1.0f + r);
+
+ // generate normally distributed number
+ u = (float)sqrt(-2.0f * log(u));
+ *dst++ = cosf(phi) * u * stdev + mean;
+
+ set=1;
+ }
+}
+
+void np_normrnd_cpx(float* dst, int nitems, float mean, float stdev)
+{
+ // as qpc_normrnd_real, but generates always nitems complex numbers (2*nitems reals)
+
+ unsigned int r;
+ float phi = 0, u = 0;
+ // JHT int set = 0;
+ float scalephi = (M_2PI / (1.0f + K_RAND_MAX));
+ float scaleu = (1.0f / (1.0f + K_RAND_MAX));
+
+ while (nitems--) {
+ // generate a uniform distributed phase phi in the range [0,2*PI)
+ rand_s((unsigned int*)&r); phi = scalephi * r;
+
+ // generate a uniform distributed random number u in the range (0,1]
+ rand_s((unsigned int*)&r); u = scaleu * (1.0f + r);
+
+ // generate normally distributed real and imaginary parts
+ u = (float)sqrt(-2.0f * log(u));
+ *dst++ = cosf(phi) * u * stdev + mean;
+ *dst++ = sinf(phi) * u * stdev + mean;
+ }
+}
+
+void np_unidrnd(unsigned int* dst, int nitems, unsigned int nsetsize)
+{
+ // generate uniform unsigned int random numbers in the range [0..nsetsize)
+
+ unsigned int r;
+ float u;
+ float scaleu = (1.0f / (1.0f + K_RAND_MAX));
+
+ while (nitems--) {
+ rand_s((unsigned int*)&r); u = scaleu * nsetsize * r;
+ *dst++ = (unsigned int)floorf(u);
+ }
+
+}
+void np_unidrnd_uc(unsigned char* dst, int nitems, unsigned char nsetsize)
+{
+ // generate uniform unsigned char random numbers in the range [0..nsetsize)
+
+ unsigned int r;
+ float u;
+ float scaleu = (1.0f / (1.0f + K_RAND_MAX));
+
+ while (nitems--) {
+ rand_s((unsigned int*)&r); u = scaleu * nsetsize * r;
+ *dst++ = (unsigned char)floorf(u);
+ }
+
+}
+
+void np_unifrnd(float* dst, int nitems, float fmax)
+{
+ // generate uniform float random numbers in the range [0..fmax)
+
+ unsigned int r;
+ float u;
+ float scaleu = (1.0f / (1.0f + K_RAND_MAX));
+
+ while (nitems--) {
+ rand_s((unsigned int*)&r); u = scaleu * fmax * r;
+ *dst++ = u;
+ }
+
+}
|
