Skip to content

Commit 8d0620c

Browse files
committed
switch to randint and adjust types
1 parent cfc50de commit 8d0620c

File tree

1 file changed

+15
-30
lines changed

1 file changed

+15
-30
lines changed

frameworks/Python/aiohttp/app/random_utils.pyx

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,47 @@
11
# cython: boundscheck=False
22
# cython: wraparound=False
33
# cython: initializedcheck=False
4+
# cython: cdivision=True
45

56
from libc.stdlib cimport calloc, free
67

7-
import cython
8-
import os
8+
import random
99

1010
# Constants
11-
cdef unsigned int MAX_VALUE = 10000
11+
cdef const unsigned short MAX_VALUE = 10000
1212
# https://statmath.wu.ac.at/software/src/prng-3.0.2/doc/prng.html/Table_LCG.html
13-
cdef unsigned int MODULOS = 134217689
14-
cdef unsigned int RANDOM_THRESHOLD = MODULOS - (MODULOS % MAX_VALUE)
15-
cdef unsigned int LCG_MULTIPLIER = 3162696
16-
cdef bint* seen = <bint*>calloc(MAX_VALUE + 1, sizeof(bint)) # Bit array simulation
17-
18-
cdef unsigned long seed = 0
13+
cdef const unsigned int MODULOS = 134217689
14+
cdef const unsigned long LCG_MULTIPLIER = 3162696
1915

20-
cdef void _init_seed():
21-
global seed
22-
cdef bytes random_bytes = os.urandom(4)
23-
seed = int.from_bytes(random_bytes, byteorder='little') % MODULOS
24-
25-
if seed == 0:
26-
seed = 1 # Ensure seed is never zero to avoid low number cycle
16+
cdef unsigned int RANDOM_THRESHOLD = MODULOS - (MODULOS % MAX_VALUE)
17+
cdef char* seen = <char*>calloc(MAX_VALUE + 1, sizeof(char)) # Bit array simulation
2718

28-
_init_seed()
19+
cdef unsigned int seed = random.randint(1, MODULOS-1)
2920

30-
@cython.boundscheck(False)
31-
@cython.wraparound(False)
3221
cdef inline unsigned int _next_random() noexcept nogil:
3322
"""Generate a pseudo-random number based on a linear congruential generator"""
3423
global seed
3524

36-
cdef unsigned long next_val = (<unsigned long>LCG_MULTIPLIER * seed) % MODULOS
25+
cdef unsigned int next_val = (LCG_MULTIPLIER * seed) % MODULOS
3726

3827
while next_val >= RANDOM_THRESHOLD:
39-
next_val = (next_val * LCG_MULTIPLIER) % MODULOS
28+
next_val = (LCG_MULTIPLIER * next_val) % MODULOS
4029

4130
seed = next_val
4231
return seed
4332

44-
@cython.boundscheck(False)
45-
@cython.wraparound(False)
46-
cpdef unsigned int random_id() noexcept nogil:
33+
cpdef unsigned short random_id() noexcept nogil:
4734
"""Generate a pseudo-random number in range [1, MAX_VALUE]"""
4835
return 1 + (_next_random() % MAX_VALUE)
4936

50-
@cython.boundscheck(False)
51-
@cython.wraparound(False)
52-
cpdef list[unsigned int] random_unique_ids(int n):
37+
cpdef list[unsigned short] random_unique_ids(unsigned short n):
5338
"""Generate n unique random IDs in range[1, 10001]"""
54-
cdef list[int] result = [0] * n
55-
cdef int candidate, count = 0
39+
cdef list[unsigned short] result = [0] * n
40+
cdef unsigned short candidate, count = 0
5641

5742
try:
5843
while count < n:
59-
candidate = random_id()
44+
candidate = 1 + (_next_random() % MAX_VALUE)
6045

6146
if seen[candidate] == 0: # Not seen before
6247
seen[candidate] = 1

0 commit comments

Comments
 (0)