Skip to content

Commit

Permalink
faster rand(::MersenneTwister, ::Union{[U]Int{8,16,32},Bool}) (#25277)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rfourquet authored Dec 29, 2017
1 parent d0ed2f2 commit ea5b0a3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
7 changes: 5 additions & 2 deletions base/random/RNGs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,11 @@ rand(r::MersenneTwister, sp::SamplerTrivial{Close1Open2_64}) =

#### integers

rand(r::MersenneTwister, T::SamplerUnion(BitInteger)) = mt_pop!(r, T[])
rand(r::MersenneTwister, ::SamplerType{Bool}) = rand(r, UInt8) % Bool
rand(r::MersenneTwister, T::SamplerUnion(Union{Int64,UInt64,Int128,UInt128})) =
mt_pop!(r, T[])

rand(r::MersenneTwister, T::SamplerUnion(Union{Bool,Int8,UInt8,Int16,UInt16,Int32,UInt32})) =
rand(r, UInt52Raw()) % T[]

#### arrays of floats

Expand Down
8 changes: 4 additions & 4 deletions test/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,10 @@ let mt = MersenneTwister(0)
B = Vector{T}(uninitialized, 31)
rand!(mt, A)
rand!(mt, B)
@test A[end] == Any[21, 0x4e, -3158, 0x0ded, 2132370312, 0x5e76d222, 1701112237820550475, 0x552ac662d46426bf,
-48946429529471164341432530784191877404, Float16(0.99805), 0.845204f0][i]
@test B[end] == Any[94, 0x14, 22684, 0x0278, -862240437, 0xc425026c, -7329373301769527751, 0x8a40806d8107ce23,
37650272825719887492093881479739648820, Float16(0.14648), 0.5025569f0][i]
@test A[end] == Any[21, 0x7b, 17385, 0x3086, -1574090021, 0xadcb4460, 6797283068698303107, 0xc8e6453e139271f3,
69855512850528774484795047199183096941, Float16(0.16895), 0.21086597f0][i]
@test B[end] == Any[49, 0x65, -3725, 0x719d, 814246081, 0xdf61843a, 2120308604158549401, 0xcb28c236e9c0f608,
61881313582466480231846019869039259750, Float16(0.38672), 0.20027375f0][i]
end

srand(mt, 0)
Expand Down

0 comments on commit ea5b0a3

Please sign in to comment.