Skip to content

Commit

Permalink
Deprecate randbool (#9105).
Browse files Browse the repository at this point in the history
For a random Bool, instead of randbool(), use rand(Bool)
Instead of randbool(dims), use bitrand(dims)
  • Loading branch information
ViralBShah committed Jan 3, 2015
1 parent 72ff0d9 commit 0796c87
Show file tree
Hide file tree
Showing 13 changed files with 167 additions and 176 deletions.
10 changes: 0 additions & 10 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -329,16 +329,6 @@ reinterpret{N}(B::BitArray, dims::NTuple{N,Int}) = reshape(B, dims)
bitunpack{N}(B::BitArray{N}) = convert(Array{Bool,N}, B)
bitpack{T,N}(A::AbstractArray{T,N}) = convert(BitArray{N}, A)

## Random ##

function bitarray_rand_fill!(rng, B::BitArray) # rng is an AbstractRNG
length(B) == 0 && return B
Bc = B.chunks
rand!(rng, Bc)
Bc[end] &= @_msk_end length(B)
return B
end

## Indexing: getindex ##

function unsafe_bitgetindex(Bc::Vector{UInt64}, i::Int)
Expand Down
2 changes: 2 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,5 @@ const base64 = base64encode

@deprecate sizehint(A, n) sizehint!(A, n)

@deprecate randbool() rand(Bool)
@deprecate randbool(x...) bitrand(x)
2 changes: 1 addition & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -916,12 +916,12 @@ export
RandomDevice,
rand!,
rand,
randbool,
randn!,
randn,
randexp!,
randexp,
srand,
bitrand,

# bigfloat & precision
precision,
Expand Down
21 changes: 12 additions & 9 deletions base/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export srand,
rand, rand!,
randn, randn!,
randexp, randexp!,
randbool,
bitrand,
AbstractRNG, RNG, MersenneTwister, RandomDevice


Expand Down Expand Up @@ -552,16 +552,19 @@ rand(rng::AbstractRNG, r::AbstractArray, dims::Int...) = rand(rng, r, dims)

## random BitArrays (AbstractRNG)

rand!(r::AbstractRNG, B::BitArray) = Base.bitarray_rand_fill!(r, B)

randbool(r::AbstractRNG, dims::Dims) = rand!(r, BitArray(dims))
randbool(r::AbstractRNG, dims::Int...) = rand!(r, BitArray(dims))

randbool(dims::Dims) = rand!(BitArray(dims))
randbool(dims::Int...) = rand!(BitArray(dims))
function rand!(rng::AbstractRNG, B::BitArray)
length(B) == 0 && return B
Bc = B.chunks
rand!(rng, Bc)
Bc[end] &= Base.@_msk_end length(B)
return B
end

randbool(r::AbstractRNG=GLOBAL_RNG) = rand(r, Bool)
bitrand(r::AbstractRNG, dims::Dims) = rand!(r, BitArray(dims))
bitrand(r::AbstractRNG, dims::Int...) = rand!(r, BitArray(dims))

bitrand(dims::Dims) = rand!(BitArray(dims))
bitrand(dims::Int...) = rand!(BitArray(dims))

## randn() - Normally distributed random numbers using Ziggurat algorithm

Expand Down
4 changes: 2 additions & 2 deletions doc/manual/parallel-computing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ following function in ``count_heads.jl``::
function count_heads(n)
c::Int = 0
for i=1:n
c += randbool()
c += rand(Bool)
end
c
end
Expand Down Expand Up @@ -303,7 +303,7 @@ we can use a *parallel for loop*, which can be written in Julia like
this::

nheads = @parallel (+) for i=1:200000000
int(randbool())
int(rand(Bool))
end

This construct implements the pattern of assigning iterations to
Expand Down
4 changes: 2 additions & 2 deletions doc/manual/performance-tips.rst
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ For example, the following contrived function returns an array of a
randomly-chosen type::

function strange_twos(n)
a = Array(randbool() ? Int64 : Float64, n)
a = Array(rand(Bool) ? Int64 : Float64, n)
for i = 1:n
a[i] = 2
end
Expand All @@ -305,7 +305,7 @@ This should be written as::
end

function strange_twos(n)
a = Array(randbool() ? Int64 : Float64, n)
a = Array(rand(Bool) ? Int64 : Float64, n)
fill_twos!(a)
return a
end
Expand Down
5 changes: 2 additions & 3 deletions doc/stdlib/numbers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -535,9 +535,9 @@ As ``BigInt`` represents unbounded integers, the interval must be specified (e.g

Populate the array A with random values. If the indexable collection ``coll`` is specified, the values are picked randomly from ``coll``. This is equivalent to ``copy!(A, rand(rng, coll, size(A)))`` or ``copy!(A, rand(rng, eltype(A), size(A)))`` but without allocating a new array.

.. function:: randbool([rng], [dims...])
.. function:: bitrand([rng], [dims...])

Generate a random boolean value. Optionally, generate a ``BitArray`` of random boolean values.
Generate a ``BitArray`` of random boolean values.

.. function:: randn([rng], [dims...])

Expand All @@ -554,4 +554,3 @@ As ``BigInt`` represents unbounded integers, the interval must be specified (e.g
.. function:: randexp!([rng], A::Array{Float64,N})

Fill the array A with random numbers following the exponential distribution (with scale 1).

Loading

0 comments on commit 0796c87

Please sign in to comment.