Skip to content

Commit ea5b0a3

Browse files
authored
faster rand(::MersenneTwister, ::Union{[U]Int{8,16,32},Bool}) (#25277)
Now that generating scalar Float64 values (i.e. 52 random bits) is even faster, it becomes faster to just generate such values and make small integers out of them, rather than saving entropy bits with using the integer cache.
1 parent d0ed2f2 commit ea5b0a3

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

base/random/RNGs.jl

+5-2
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,11 @@ rand(r::MersenneTwister, sp::SamplerTrivial{Close1Open2_64}) =
313313

314314
#### integers
315315

316-
rand(r::MersenneTwister, T::SamplerUnion(BitInteger)) = mt_pop!(r, T[])
317-
rand(r::MersenneTwister, ::SamplerType{Bool}) = rand(r, UInt8) % Bool
316+
rand(r::MersenneTwister, T::SamplerUnion(Union{Int64,UInt64,Int128,UInt128})) =
317+
mt_pop!(r, T[])
318+
319+
rand(r::MersenneTwister, T::SamplerUnion(Union{Bool,Int8,UInt8,Int16,UInt16,Int32,UInt32})) =
320+
rand(r, UInt52Raw()) % T[]
318321

319322
#### arrays of floats
320323

test/random.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,10 @@ let mt = MersenneTwister(0)
278278
B = Vector{T}(uninitialized, 31)
279279
rand!(mt, A)
280280
rand!(mt, B)
281-
@test A[end] == Any[21, 0x4e, -3158, 0x0ded, 2132370312, 0x5e76d222, 1701112237820550475, 0x552ac662d46426bf,
282-
-48946429529471164341432530784191877404, Float16(0.99805), 0.845204f0][i]
283-
@test B[end] == Any[94, 0x14, 22684, 0x0278, -862240437, 0xc425026c, -7329373301769527751, 0x8a40806d8107ce23,
284-
37650272825719887492093881479739648820, Float16(0.14648), 0.5025569f0][i]
281+
@test A[end] == Any[21, 0x7b, 17385, 0x3086, -1574090021, 0xadcb4460, 6797283068698303107, 0xc8e6453e139271f3,
282+
69855512850528774484795047199183096941, Float16(0.16895), 0.21086597f0][i]
283+
@test B[end] == Any[49, 0x65, -3725, 0x719d, 814246081, 0xdf61843a, 2120308604158549401, 0xcb28c236e9c0f608,
284+
61881313582466480231846019869039259750, Float16(0.38672), 0.20027375f0][i]
285285
end
286286

287287
srand(mt, 0)

0 commit comments

Comments
 (0)