mirror of
https://git.code.sf.net/p/quake/quakeforge
synced 2025-03-21 01:41:10 +00:00
[util] Add mtwist float random number functions
mtwist_rand_0_1 produces numbers in the range [0, 1) and mtwist_rand_m1_1 produces numbers in the range (-1, 1). The numbers will not be denormal, so the distribution should be fairly uniform (as much as Mersenne Twister itself is), but this needs proper testing. 0 is included for the mtwist_rand_0_1 as it seems useful, but -1 is not included in mtwist_rand_m1_1 in order to keep the extremes of the distribution balanced around 0.
This commit is contained in:
parent
6411518603
commit
91a686d1f5
2 changed files with 41 additions and 0 deletions
|
@ -42,5 +42,45 @@ typedef struct {
|
|||
|
||||
void mtwist_seed (mtstate_t *state, uint32_t seed);
|
||||
uint32_t mtwist_rand (mtstate_t *state);
|
||||
GNU89INLINE inline float mtwist_rand_0_1 (mtstate_t *state);
|
||||
GNU89INLINE inline float mtwist_rand_m1_1 (mtstate_t *state);
|
||||
|
||||
#ifndef IMPLEMENT_MTWIST_Funcs
|
||||
GNU89INLINE inline
|
||||
#else
|
||||
VISIBLE
|
||||
#endif
|
||||
float
|
||||
mtwist_rand_0_1 (mtstate_t *state)
|
||||
{
|
||||
union {
|
||||
uint32_t u;
|
||||
float f;
|
||||
} uf;
|
||||
|
||||
uf.u = mtwist_rand (state) & 0x007fffff;
|
||||
uf.u |= 0x3f800000;
|
||||
return uf.f - 1.0;
|
||||
}
|
||||
|
||||
#ifndef IMPLEMENT_MTWIST_Funcs
|
||||
GNU89INLINE inline
|
||||
#else
|
||||
VISIBLE
|
||||
#endif
|
||||
float
|
||||
mtwist_rand_m1_1 (mtstate_t *state)
|
||||
{
|
||||
union {
|
||||
uint32_t u;
|
||||
float f;
|
||||
} uf;
|
||||
|
||||
do {
|
||||
uf.u = mtwist_rand (state) & 0x007fffff;
|
||||
} while (!uf.u);
|
||||
uf.u |= 0x40000000;
|
||||
return uf.f - 3.0;
|
||||
}
|
||||
|
||||
#endif//__QF_mersenne_h
|
||||
|
|
|
@ -48,6 +48,7 @@
|
|||
There were no differences.
|
||||
*/
|
||||
|
||||
#define IMPLEMENT_MTWIST_Funcs
|
||||
#include "QF/mersenne.h"
|
||||
|
||||
#define KNUTH_MULT 1812433253ul // 0x6c078965
|
||||
|
|
Loading…
Reference in a new issue