@@ -17,7 +17,6 @@ from libc.stdint cimport (uint8_t, uint16_t, uint32_t, uint64_t,
17
17
from ._bounded_integers cimport (_rand_bool , _rand_int32 , _rand_int64 ,
18
18
_rand_int16 , _rand_int8 , _rand_uint64 , _rand_uint32 , _rand_uint16 ,
19
19
_rand_uint8 , _gen_mask )
20
- from ._bounded_integers import _integers_types
21
20
from ._pcg64 import PCG64
22
21
from numpy .random cimport bitgen_t
23
22
from ._common cimport (POISSON_LAM_MAX , CONS_POSITIVE , CONS_NONE ,
@@ -262,7 +261,7 @@ cdef class Generator:
262
261
263
262
def random (self , size = None , dtype = np .float64 , out = None ):
264
263
"""
265
- random(size=None, dtype='d' , out=None)
264
+ random(size=None, dtype=np.float64 , out=None)
266
265
267
266
Return random floats in the half-open interval [0.0, 1.0).
268
267
@@ -278,10 +277,9 @@ cdef class Generator:
278
277
Output shape. If the given shape is, e.g., ``(m, n, k)``, then
279
278
``m * n * k`` samples are drawn. Default is None, in which case a
280
279
single value is returned.
281
- dtype : {str, dtype}, optional
282
- Desired dtype of the result, either 'd' (or 'float64') or 'f'
283
- (or 'float32'). All dtypes are determined by their name. The
284
- default value is 'd'.
280
+ dtype : dtype, optional
281
+ Desired dtype of the result, only `float64` and `float32` are supported.
282
+ Byteorder must be native. The default value is np.float64.
285
283
out : ndarray, optional
286
284
Alternative output array in which to place the result. If size is not None,
287
285
it must have the same shape as the provided size and must match the type of
@@ -312,13 +310,13 @@ cdef class Generator:
312
310
313
311
"""
314
312
cdef double temp
315
- key = np .dtype (dtype ). name
316
- if key == ' float64' :
313
+ _dtype = np .dtype (dtype )
314
+ if _dtype == np . float64 :
317
315
return double_fill (& random_standard_uniform_fill , & self ._bitgen , size , self .lock , out )
318
- elif key == ' float32' :
316
+ elif _dtype == np . float32 :
319
317
return float_fill (& random_standard_uniform_fill_f , & self ._bitgen , size , self .lock , out )
320
318
else :
321
- raise TypeError ('Unsupported dtype "%s" for random' % key )
319
+ raise TypeError ('Unsupported dtype %r for random' % _dtype )
322
320
323
321
def beta (self , a , b , size = None ):
324
322
"""
@@ -417,7 +415,7 @@ cdef class Generator:
417
415
418
416
def standard_exponential (self , size = None , dtype = np .float64 , method = u'zig' , out = None ):
419
417
"""
420
- standard_exponential(size=None, dtype='d' , method='zig', out=None)
418
+ standard_exponential(size=None, dtype=np.float64 , method='zig', out=None)
421
419
422
420
Draw samples from the standard exponential distribution.
423
421
@@ -431,9 +429,8 @@ cdef class Generator:
431
429
``m * n * k`` samples are drawn. Default is None, in which case a
432
430
single value is returned.
433
431
dtype : dtype, optional
434
- Desired dtype of the result, either 'd' (or 'float64') or 'f'
435
- (or 'float32'). All dtypes are determined by their name. The
436
- default value is 'd'.
432
+ Desired dtype of the result, only `float64` and `float32` are supported.
433
+ Byteorder must be native. The default value is np.float64.
437
434
method : str, optional
438
435
Either 'inv' or 'zig'. 'inv' uses the default inverse CDF method.
439
436
'zig' uses the much faster Ziggurat method of Marsaglia and Tsang.
@@ -454,24 +451,24 @@ cdef class Generator:
454
451
>>> n = np.random.default_rng().standard_exponential((3, 8000))
455
452
456
453
"""
457
- key = np .dtype (dtype ). name
458
- if key == ' float64' :
454
+ _dtype = np .dtype (dtype )
455
+ if _dtype == np . float64 :
459
456
if method == u'zig' :
460
457
return double_fill (& random_standard_exponential_fill , & self ._bitgen , size , self .lock , out )
461
458
else :
462
459
return double_fill (& random_standard_exponential_inv_fill , & self ._bitgen , size , self .lock , out )
463
- elif key == ' float32' :
460
+ elif _dtype == np . float32 :
464
461
if method == u'zig' :
465
462
return float_fill (& random_standard_exponential_fill_f , & self ._bitgen , size , self .lock , out )
466
463
else :
467
464
return float_fill (& random_standard_exponential_inv_fill_f , & self ._bitgen , size , self .lock , out )
468
465
else :
469
- raise TypeError ('Unsupported dtype "%s" for standard_exponential'
470
- % key )
466
+ raise TypeError ('Unsupported dtype %r for standard_exponential'
467
+ % _dtype )
471
468
472
469
def integers (self , low , high = None , size = None , dtype = np .int64 , endpoint = False ):
473
470
"""
474
- integers(low, high=None, size=None, dtype=' int64' , endpoint=False)
471
+ integers(low, high=None, size=None, dtype=np. int64, endpoint=False)
475
472
476
473
Return random integers from `low` (inclusive) to `high` (exclusive), or
477
474
if endpoint=True, `low` (inclusive) to `high` (inclusive). Replaces
@@ -496,11 +493,9 @@ cdef class Generator:
496
493
Output shape. If the given shape is, e.g., ``(m, n, k)``, then
497
494
``m * n * k`` samples are drawn. Default is None, in which case a
498
495
single value is returned.
499
- dtype : {str, dtype}, optional
500
- Desired dtype of the result. All dtypes are determined by their
501
- name, i.e., 'int64', 'int', etc, so byteorder is not available
502
- and a specific precision may have different C types depending
503
- on the platform. The default value is `np.int_`.
496
+ dtype : dtype, optional
497
+ Desired dtype of the result. Byteorder must be native.
498
+ The default value is np.int64.
504
499
endpoint : bool, optional
505
500
If true, sample from the interval [low, high] instead of the
506
501
default [low, high)
@@ -559,39 +554,39 @@ cdef class Generator:
559
554
high = low
560
555
low = 0
561
556
562
- dt = np .dtype (dtype )
563
- key = dt .name
564
- if key not in _integers_types :
565
- raise TypeError ('Unsupported dtype "%s" for integers' % key )
566
- if not dt .isnative :
567
- raise ValueError ('Providing a dtype with a non-native byteorder '
568
- 'is not supported. If you require '
569
- 'platform-independent byteorder, call byteswap '
570
- 'when required.' )
557
+ _dtype = np .dtype (dtype )
571
558
572
559
# Implementation detail: the old API used a masked method to generate
573
560
# bounded uniform integers. Lemire's method is preferable since it is
574
561
# faster. randomgen allows a choice, we will always use the faster one.
575
562
cdef bint _masked = False
576
563
577
- if key == ' int32' :
564
+ if _dtype == np . int32 :
578
565
ret = _rand_int32 (low , high , size , _masked , endpoint , & self ._bitgen , self .lock )
579
- elif key == ' int64' :
566
+ elif _dtype == np . int64 :
580
567
ret = _rand_int64 (low , high , size , _masked , endpoint , & self ._bitgen , self .lock )
581
- elif key == ' int16' :
568
+ elif _dtype == np . int16 :
582
569
ret = _rand_int16 (low , high , size , _masked , endpoint , & self ._bitgen , self .lock )
583
- elif key == ' int8' :
570
+ elif _dtype == np . int8 :
584
571
ret = _rand_int8 (low , high , size , _masked , endpoint , & self ._bitgen , self .lock )
585
- elif key == ' uint64' :
572
+ elif _dtype == np . uint64 :
586
573
ret = _rand_uint64 (low , high , size , _masked , endpoint , & self ._bitgen , self .lock )
587
- elif key == ' uint32' :
574
+ elif _dtype == np . uint32 :
588
575
ret = _rand_uint32 (low , high , size , _masked , endpoint , & self ._bitgen , self .lock )
589
- elif key == ' uint16' :
576
+ elif _dtype == np . uint16 :
590
577
ret = _rand_uint16 (low , high , size , _masked , endpoint , & self ._bitgen , self .lock )
591
- elif key == ' uint8' :
578
+ elif _dtype == np . uint8 :
592
579
ret = _rand_uint8 (low , high , size , _masked , endpoint , & self ._bitgen , self .lock )
593
- elif key == 'bool' :
580
+ elif _dtype == np . bool_ :
594
581
ret = _rand_bool (low , high , size , _masked , endpoint , & self ._bitgen , self .lock )
582
+ elif not _dtype .isnative :
583
+ raise ValueError ('Providing a dtype with a non-native byteorder '
584
+ 'is not supported. If you require '
585
+ 'platform-independent byteorder, call byteswap '
586
+ 'when required.' )
587
+ else :
588
+ raise TypeError ('Unsupported dtype %r for integers' % _dtype )
589
+
595
590
596
591
if size is None and dtype in (bool , int , np .compat .long ):
597
592
if np .array (ret ).shape == ():
@@ -977,7 +972,7 @@ cdef class Generator:
977
972
# Complicated, continuous distributions:
978
973
def standard_normal (self , size = None , dtype = np .float64 , out = None ):
979
974
"""
980
- standard_normal(size=None, dtype='d' , out=None)
975
+ standard_normal(size=None, dtype=np.float64 , out=None)
981
976
982
977
Draw samples from a standard Normal distribution (mean=0, stdev=1).
983
978
@@ -987,10 +982,9 @@ cdef class Generator:
987
982
Output shape. If the given shape is, e.g., ``(m, n, k)``, then
988
983
``m * n * k`` samples are drawn. Default is None, in which case a
989
984
single value is returned.
990
- dtype : {str, dtype}, optional
991
- Desired dtype of the result, either 'd' (or 'float64') or 'f'
992
- (or 'float32'). All dtypes are determined by their name. The
993
- default value is 'd'.
985
+ dtype : dtype, optional
986
+ Desired dtype of the result, only `float64` and `float32` are supported.
987
+ Byteorder must be native. The default value is np.float64.
994
988
out : ndarray, optional
995
989
Alternative output array in which to place the result. If size is not None,
996
990
it must have the same shape as the provided size and must match the type of
@@ -1038,14 +1032,13 @@ cdef class Generator:
1038
1032
[ 0.39924804, 4.68456316, 4.99394529, 4.84057254]]) # random
1039
1033
1040
1034
"""
1041
- key = np .dtype (dtype ). name
1042
- if key == ' float64' :
1035
+ _dtype = np .dtype (dtype )
1036
+ if _dtype == np . float64 :
1043
1037
return double_fill (& random_standard_normal_fill , & self ._bitgen , size , self .lock , out )
1044
- elif key == ' float32' :
1038
+ elif _dtype == np . float32 :
1045
1039
return float_fill (& random_standard_normal_fill_f , & self ._bitgen , size , self .lock , out )
1046
-
1047
1040
else :
1048
- raise TypeError ('Unsupported dtype "%s" for standard_normal' % key )
1041
+ raise TypeError ('Unsupported dtype %r for standard_normal' % _dtype )
1049
1042
1050
1043
def normal (self , loc = 0.0 , scale = 1.0 , size = None ):
1051
1044
"""
@@ -1151,7 +1144,7 @@ cdef class Generator:
1151
1144
1152
1145
def standard_gamma (self , shape , size = None , dtype = np .float64 , out = None ):
1153
1146
"""
1154
- standard_gamma(shape, size=None, dtype='d' , out=None)
1147
+ standard_gamma(shape, size=None, dtype=np.float64 , out=None)
1155
1148
1156
1149
Draw samples from a standard Gamma distribution.
1157
1150
@@ -1167,10 +1160,9 @@ cdef class Generator:
1167
1160
``m * n * k`` samples are drawn. If size is ``None`` (default),
1168
1161
a single value is returned if ``shape`` is a scalar. Otherwise,
1169
1162
``np.array(shape).size`` samples are drawn.
1170
- dtype : {str, dtype}, optional
1171
- Desired dtype of the result, either 'd' (or 'float64') or 'f'
1172
- (or 'float32'). All dtypes are determined by their name. The
1173
- default value is 'd'.
1163
+ dtype : dtype, optional
1164
+ Desired dtype of the result, only `float64` and `float32` are supported.
1165
+ Byteorder must be native. The default value is np.float64.
1174
1166
out : ndarray, optional
1175
1167
Alternative output array in which to place the result. If size is
1176
1168
not None, it must have the same shape as the provided size and
@@ -1227,19 +1219,19 @@ cdef class Generator:
1227
1219
1228
1220
"""
1229
1221
cdef void * func
1230
- key = np .dtype (dtype ). name
1231
- if key == ' float64' :
1222
+ _dtype = np .dtype (dtype )
1223
+ if _dtype == np . float64 :
1232
1224
return cont (& random_standard_gamma , & self ._bitgen , size , self .lock , 1 ,
1233
1225
shape , 'shape' , CONS_NON_NEGATIVE ,
1234
1226
0.0 , '' , CONS_NONE ,
1235
1227
0.0 , '' , CONS_NONE ,
1236
1228
out )
1237
- if key == ' float32' :
1229
+ if _dtype == np . float32 :
1238
1230
return cont_f (& random_standard_gamma_f , & self ._bitgen , size , self .lock ,
1239
1231
shape , 'shape' , CONS_NON_NEGATIVE ,
1240
1232
out )
1241
1233
else :
1242
- raise TypeError ('Unsupported dtype "%s" for standard_gamma' % key )
1234
+ raise TypeError ('Unsupported dtype %r for standard_gamma' % _dtype )
1243
1235
1244
1236
def gamma (self , shape , scale = 1.0 , size = None ):
1245
1237
"""
0 commit comments