Skip to content

Commit 90c91b2

Browse files
committed
Merge pull request #53 from bashtage/alternative-double
ENH: Improve double generation for 64-bit generators
2 parents f872636 + 6249825 commit 90c91b2

File tree

6 files changed

+6
-28
lines changed

6 files changed

+6
-28
lines changed

randomstate/interface/mlfg-1279-861/mlfg-1279-861-shim.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,8 @@ inline uint64_t random_raw_values(aug_state* state)
4040
inline double random_double(aug_state* state)
4141
{
4242
uint64_t rn;
43-
int32_t a, b;
4443
rn = mlfg_next(state->rng);
45-
a = rn >> 37;
46-
b = (rn & 0xFFFFFFFFULL) >> 6;
47-
return (a * 67108864.0 + b) / 9007199254740992.0;
44+
return (rn >> 11) * (1.0 / 9007199254740992.0);
4845
}
4946

5047
extern void set_seed(aug_state* state, uint64_t seed);

randomstate/interface/pcg-64/pcg-64-shim.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@ inline void entropy_init(aug_state* state)
6363
inline double random_double(aug_state* state)
6464
{
6565
uint64_t rn;
66-
int32_t a, b;
6766
rn = random_uint64(state);
68-
a = rn >> 37;
69-
b = (rn & 0xFFFFFFFFLL) >> 6;
70-
return (a * 67108864.0 + b) / 9007199254740992.0;
67+
return (rn >> 11) * (1.0 / 9007199254740992.0);
7168
}

randomstate/interface/xoroshiro128plus/xoroshiro128plus-shim.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,8 @@ inline uint64_t random_raw_values(aug_state* state)
4747
inline double random_double(aug_state* state)
4848
{
4949
uint64_t rn;
50-
int32_t a, b;
5150
rn = random_uint64(state);
52-
a = rn >> 37;
53-
b = (rn & 0xFFFFFFFFLL) >> 6;
54-
return (a * 67108864.0 + b) / 9007199254740992.0;
51+
return (rn >> 11) * (1.0 / 9007199254740992.0);
5552
}
5653

5754
extern void set_seed(aug_state* state, uint64_t seed);

randomstate/interface/xorshift1024/xorshift1024-shim.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,8 @@ inline uint64_t random_raw_values(aug_state* state)
4747
inline double random_double(aug_state* state)
4848
{
4949
uint64_t rn;
50-
int32_t a, b;
5150
rn = random_uint64(state);
52-
a = rn >> 37;
53-
b = (rn & 0xFFFFFFFFLL) >> 6;
54-
return (a * 67108864.0 + b) / 9007199254740992.0;
51+
return (rn >> 11) * (1.0 / 9007199254740992.0);
5552
}
5653

5754
extern void set_seed(aug_state* state, uint64_t seed);

randomstate/interface/xorshift128/xorshift128-shim.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,8 @@ inline uint64_t random_raw_values(aug_state* state)
4747
inline double random_double(aug_state* state)
4848
{
4949
uint64_t rn;
50-
int32_t a, b;
5150
rn = random_uint64(state);
52-
a = rn >> 37;
53-
b = (rn & 0xFFFFFFFFLL) >> 6;
54-
return (a * 67108864.0 + b) / 9007199254740992.0;
51+
return (rn >> 11) * (1.0 / 9007199254740992.0);
5552
}
5653

5754
extern void set_seed(aug_state* state, uint64_t seed);

randomstate/tests/test_direct.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,9 @@ def uniform_from_uint(x, bits):
2626
return uniform_from_uint64(x)
2727
elif bits == 32:
2828
return uniform_from_uint32(x)
29-
elif bits == 63:
30-
return uniform_from_uint64(x)
31-
3229

3330
def uniform_from_uint64(x):
34-
a = x >> 37
35-
b = (x & 0xFFFFFFFF) >> 6
36-
return (a * 67108864.0 + b) / 9007199254740992.0
37-
31+
return (x >> np.uint64(11)) * (1.0 / 9007199254740992.0)
3832

3933
def uniform_from_uint32(x):
4034
out = np.empty(len(x) // 2)
@@ -44,7 +38,6 @@ def uniform_from_uint32(x):
4438
out[i // 2] = (a * 67108864.0 + b) / 9007199254740992.0
4539
return out
4640

47-
4841
def uint64_from_uint63(x):
4942
out = np.empty(len(x) // 2, dtype=np.uint64)
5043
for i in range(0, len(x), 2):

0 commit comments

Comments
 (0)