Skip to content

Commit 68578b1

Browse files
Sheppard, KevinSheppard, Kevin
authored andcommitted
ENH: Improve fallback seeding on Windows
Use QueryPerformanceCounter to improve fallback entropy when more than 1 value is needed Add random_entropy to module import
1 parent 94c5ce0 commit 68578b1

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

randomstate/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from __future__ import division, absolute_import, print_function
22

33
from randomstate.prng.mt19937 import *
4+
from randomstate.entropy import random_entropy
45
import randomstate.prng

randomstate/src/common/entropy.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,14 @@ uint32_t entropy_randombytes(void) {
142142
gettimeofday(&tv, NULL);
143143
return entropy_hash_32(getpid()) ^ entropy_hash_32(tv.tv_sec) ^ entropy_hash_32(tv.tv_usec) ^ entropy_hash_32(clock());
144144
#else
145+
uint32_t out = 0;
146+
int64_t counter;
145147
struct _timeb tv;
146148
_ftime(&tv);
147-
return entropy_hash_32(GetCurrentProcessId()) ^ entropy_hash_32((uint32_t)tv.time) ^ entropy_hash_32(tv.millitm) ^ entropy_hash_32(clock());
149+
out = entropy_hash_32(GetCurrentProcessId()) ^ entropy_hash_32((uint32_t)tv.time) ^ entropy_hash_32(tv.millitm) ^ entropy_hash_32(clock());
150+
if (QueryPerformanceCounter((LARGE_INTEGER *)&counter) != 0)
151+
out ^= entropy_hash_32((uint32_t)(counter & 0xffffffff));
152+
return out;
148153
#endif
149154
}
150155

0 commit comments

Comments
 (0)