Inzynierka_Gwiazdy/machine_learning/Lib/site-packages/sklearn/utils/_random.pxd

40 lines
1.4 KiB
Cython
Raw Normal View History

2023-09-20 19:46:58 +02:00
# Authors: Arnaud Joly
#
# License: BSD 3 clause
cimport numpy as cnp
ctypedef cnp.npy_uint32 UINT32_t
cdef inline UINT32_t DEFAULT_SEED = 1
cdef enum:
# Max value for our rand_r replacement (near the bottom).
# We don't use RAND_MAX because it's different across platforms and
# particularly tiny on Windows/MSVC.
# It corresponds to the maximum representable value for
# 32-bit signed integers (i.e. 2^31 - 1).
RAND_R_MAX = 0x7FFFFFFF
cpdef sample_without_replacement(cnp.int_t n_population,
cnp.int_t n_samples,
method=*,
random_state=*)
# rand_r replacement using a 32bit XorShift generator
# See http://www.jstatsoft.org/v08/i14/paper for details
cdef inline UINT32_t our_rand_r(UINT32_t* seed) nogil:
"""Generate a pseudo-random np.uint32 from a np.uint32 seed"""
# seed shouldn't ever be 0.
if (seed[0] == 0): seed[0] = DEFAULT_SEED
seed[0] ^= <UINT32_t>(seed[0] << 13)
seed[0] ^= <UINT32_t>(seed[0] >> 17)
seed[0] ^= <UINT32_t>(seed[0] << 5)
# Use the modulo to make sure that we don't return a values greater than the
# maximum representable value for signed 32bit integers (i.e. 2^31 - 1).
# Note that the parenthesis are needed to avoid overflow: here
# RAND_R_MAX is cast to UINT32_t before 1 is added.
return seed[0] % ((<UINT32_t>RAND_R_MAX) + 1)