@@ -607,8 +607,8 @@ cdef class RandomState:
607
607
def __init__ (self , seed = None ):
608
608
self .internal_state = < rk_state* > PyMem_Malloc(sizeof(rk_state))
609
609
610
- self .seed(seed)
611
610
self .lock = Lock()
611
+ self .seed(seed)
612
612
613
613
def __dealloc__ (self ):
614
614
if self .internal_state != NULL :
@@ -639,19 +639,22 @@ cdef class RandomState:
639
639
cdef ndarray obj " arrayObject_obj"
640
640
try :
641
641
if seed is None :
642
- errcode = rk_randomseed(self .internal_state)
642
+ with self .lock:
643
+ errcode = rk_randomseed(self .internal_state)
643
644
else :
644
645
idx = operator.index(seed)
645
646
if idx > int (2 ** 32 - 1 ) or idx < 0 :
646
647
raise ValueError (" Seed must be between 0 and 4294967295" )
647
- rk_seed(idx, self .internal_state)
648
+ with self .lock:
649
+ rk_seed(idx, self .internal_state)
648
650
except TypeError :
649
651
obj = np.asarray(seed).astype(np.int64, casting = ' safe' )
650
652
if ((obj > int (2 ** 32 - 1 )) | (obj < 0 )).any():
651
653
raise ValueError (" Seed must be between 0 and 4294967295" )
652
654
obj = obj.astype(' L' , casting = ' unsafe' )
653
- init_by_array(self .internal_state, < unsigned long * > PyArray_DATA(obj),
654
- PyArray_DIM(obj, 0 ))
655
+ with self .lock:
656
+ init_by_array(self .internal_state, < unsigned long * > PyArray_DATA(obj),
657
+ PyArray_DIM(obj, 0 ))
655
658
656
659
def get_state (self ):
657
660
"""
@@ -936,7 +939,8 @@ cdef class RandomState:
936
939
937
940
diff = < unsigned long > hi - < unsigned long > lo - 1 UL
938
941
if size is None :
939
- rv = lo + < long > rk_interval(diff, self . internal_state)
942
+ with self .lock:
943
+ rv = lo + < long > rk_interval(diff, self . internal_state)
940
944
return rv
941
945
else :
942
946
array = < ndarray> np.empty(size, int )
@@ -4581,20 +4585,22 @@ cdef class RandomState:
4581
4585
# each row. So we can't just use ordinary assignment to swap the
4582
4586
# rows; we need a bounce buffer.
4583
4587
buf = np.empty_like(x[0 ])
4584
- while i > 0 :
4585
- j = rk_interval(i, self .internal_state)
4586
- buf[...] = x[j]
4587
- x[j] = x[i]
4588
- x[i] = buf
4589
- i = i - 1
4588
+ with self .lock:
4589
+ while i > 0 :
4590
+ j = rk_interval(i, self .internal_state)
4591
+ buf[...] = x[j]
4592
+ x[j] = x[i]
4593
+ x[i] = buf
4594
+ i = i - 1
4590
4595
else :
4591
4596
# For single-dimensional arrays, lists, and any other Python
4592
4597
# sequence types, indexing returns a real object that's
4593
4598
# independent of the array contents, so we can just swap directly.
4594
- while i > 0 :
4595
- j = rk_interval(i, self .internal_state)
4596
- x[i], x[j] = x[j], x[i]
4597
- i = i - 1
4599
+ with self .lock:
4600
+ while i > 0 :
4601
+ j = rk_interval(i, self .internal_state)
4602
+ x[i], x[j] = x[j], x[i]
4603
+ i = i - 1
4598
4604
4599
4605
def permutation (self , object x ):
4600
4606
"""
0 commit comments