Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
duk_util_tinyrandom.c File Reference
#include "duk_internal.h"

Go to the source code of this file.

Macros

#define DUK__UPDATE_RND(rnd)
 
#define DUK__RND_BIT(rnd)   ((rnd) >> 31) /* only use the highest bit */
 

Functions

DUK_INTERNAL duk_uint32_t duk_util_tinyrandom_get_bits (duk_hthread *thr, duk_small_int_t n)
 
DUK_INTERNAL duk_double_t duk_util_tinyrandom_get_double (duk_hthread *thr)
 

Macro Definition Documentation

◆ DUK__RND_BIT

#define DUK__RND_BIT ( rnd)    ((rnd) >> 31) /* only use the highest bit */

◆ DUK__UPDATE_RND

#define DUK__UPDATE_RND ( rnd)
Value:
do { \
(rnd) += ((rnd) * (rnd)) | 0x05; \
(rnd) = ((rnd) & 0xffffffffU); /* if duk_uint32_t is exactly 32 bits, this is a NOP */ \
} while (0)

Definition at line 11 of file duktape-1.5.2/src-separate/duk_util_tinyrandom.c.

11#define DUK__UPDATE_RND(rnd) do { \
12 (rnd) += ((rnd) * (rnd)) | 0x05; \
13 (rnd) = ((rnd) & 0xffffffffU); /* if duk_uint32_t is exactly 32 bits, this is a NOP */ \
14 } while (0)

Referenced by duk_util_tinyrandom_get_bits(), and duk_util_tinyrandom_get_double().

Function Documentation

◆ duk_util_tinyrandom_get_bits()

DUK_INTERNAL duk_uint32_t duk_util_tinyrandom_get_bits ( duk_hthread * thr,
duk_small_int_t n )

Definition at line 18 of file duktape-1.5.2/src-separate/duk_util_tinyrandom.c.

18 {
20 duk_uint32_t res = 0;
21 duk_uint32_t rnd;
22
23 rnd = thr->heap->rnd_state;
24
25 for (i = 0; i < n; i++) {
26 DUK__UPDATE_RND(rnd);
27 res <<= 1;
28 res += DUK__RND_BIT(rnd);
29 }
30
31 thr->heap->rnd_state = rnd;
32
33 return res;
34}

References DUK__RND_BIT, DUK__UPDATE_RND, duk_hthread::heap, and duk_heap::rnd_state.

◆ duk_util_tinyrandom_get_double()

DUK_INTERNAL duk_double_t duk_util_tinyrandom_get_double ( duk_hthread * thr)

Definition at line 36 of file duktape-1.5.2/src-separate/duk_util_tinyrandom.c.

36 {
39 duk_uint32_t rnd;
40
41 /*
42 * XXX: could make this a lot faster if we create the double memory
43 * representation directly. Feasible easily (must be uniform random).
44 */
45
46 rnd = thr->heap->rnd_state;
47
48 n = 53; /* enough to cover the whole mantissa */
49 t = 0.0;
50
51 do {
52 DUK__UPDATE_RND(rnd);
53 t += DUK__RND_BIT(rnd);
54 t /= 2.0;
55 } while (--n);
56
57 thr->heap->rnd_state = rnd;
58
59 DUK_ASSERT(t >= (duk_double_t) 0.0);
60 DUK_ASSERT(t < (duk_double_t) 1.0);
61
62 return t;
63}

References DUK__RND_BIT, DUK__UPDATE_RND, DUK_ASSERT, duk_hthread::heap, and duk_heap::rnd_state.