Skip to content

Commit

Permalink
Merge pull request #9569 from JuliaLang/vs/randbool
Browse files Browse the repository at this point in the history
Replace the use of randbool()
  • Loading branch information
ViralBShah committed Jan 3, 2015
2 parents b0d94dd + ff69e6f commit b365c36
Show file tree
Hide file tree
Showing 15 changed files with 188 additions and 191 deletions.
9 changes: 7 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,14 @@ Deprecated or removed
`trunc{T<:Integer}(T,x)`, `floor{T<:Integer}(T,x)`, etc.. `trunc` is now
always bound-checked;`Base.unsafe_trunc` provides the old unchecked `itrunc`
behaviour ([#9133]).

* `squeeze` now requires that passed dimension(s) are an `Int` or tuple of `Int`s;
calling `squeeze` with an arbitrary iterator is deprecated ([#9271]).
Additionally, passed dimensions must be unique and correspond to extant
dimensions of the input array.


* `randbool` is deprecated. Use `rand(Bool)` to produce a random boolean value, and
`bitrand` to produce a random BitArray ([#9105], [#9569]).

Julia v0.3.0 Release Notes
==========================
Expand Down Expand Up @@ -1113,6 +1115,7 @@ Too numerous to mention.
[#8660]: https://github.com/JuliaLang/julia/issues/8660
[#8712]: https://github.com/JuliaLang/julia/issues/8712
[#8734]: https://github.com/JuliaLang/julia/issues/8734
[#8750]: https://github.com/JuliaLang/julia/issues/8750
[#8776]: https://github.com/JuliaLang/julia/issues/8776
[#8791]: https://github.com/JuliaLang/julia/issues/8791
[#8792]: https://github.com/JuliaLang/julia/issues/8792
Expand All @@ -1131,6 +1134,7 @@ Too numerous to mention.
[#9049]: https://github.com/JuliaLang/julia/issues/9049
[#9065]: https://github.com/JuliaLang/julia/issues/9065
[#9083]: https://github.com/JuliaLang/julia/issues/9083
[#9105]: https://github.com/JuliaLang/julia/issues/9105
[#9122]: https://github.com/JuliaLang/julia/issues/9122
[#9126]: https://github.com/JuliaLang/julia/issues/9126
[#9132]: https://github.com/JuliaLang/julia/issues/9132
Expand All @@ -1140,3 +1144,4 @@ Too numerous to mention.
[#9261]: https://github.com/JuliaLang/julia/issues/9261
[#9271]: https://github.com/JuliaLang/julia/issues/9271
[#9294]: https://github.com/JuliaLang/julia/issues/9294
[#9569]: https://github.com/JuliaLang/julia/issues/9569
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
9 changes: 7 additions & 2 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,6 @@ const IpAddr = IPAddr
@deprecate isblank(c::Char) c == ' ' || c == '\t'
@deprecate isblank(s::AbstractString) all(c -> c == ' ' || c == '\t', s)

@deprecate randbool! rand!

export Nothing
const Nothing = Void

Expand Down Expand Up @@ -259,3 +257,10 @@ const base64 = base64encode

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

@deprecate randbool! rand!
@deprecate randbool() rand(Bool)
@deprecate randbool(r::AbstractRNG) rand(r, Bool)
@deprecate randbool(dims::Dims) bitrand(dims)
@deprecate randbool(dims::Int...) bitrand(dims)
@deprecate randbool(r::AbstractRNG, dims::Dims) bitrand(r, dims)
@deprecate randbool(r::AbstractRNG, dims::Int...) bitrand(r, dims)
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
20 changes: 9 additions & 11 deletions doc/manual/arrays.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Function Description
=================================================== =====================================================================
:func:`Array(type, dims...) <Array>` an uninitialized dense array
:func:`cell(dims...) <cell>` an uninitialized cell array (heterogeneous array)
:func:`zeros(type, dims...) <zeros>` an array of all zeros of specified type, defaults to ``Float64`` if
:func:`zeros(type, dims...) <zeros>` an array of all zeros of specified type, defaults to ``Float64`` if
``type`` not specified
:func:`zeros(A) <zeros>` an array of all zeros of same element type and shape of ``A``
:func:`ones(type, dims...) <ones>` an array of all ones of specified type, defaults to ``Float64`` if
Expand Down Expand Up @@ -191,7 +191,7 @@ and its left and right neighbor along a 1-d grid. :
0.699456
0.977653
0.994953
0.41084
0.41084
0.809411

julia> [ 0.25*x[i-1] + 0.5*x[i] + 0.25*x[i+1] for i=2:length(x)-1 ]
Expand All @@ -200,7 +200,7 @@ and its left and right neighbor along a 1-d grid. :
0.57468
0.685417
0.912429
0.8446
0.8446
0.656511

.. note:: In the above example, ``x`` is declared as constant because type
Expand Down Expand Up @@ -259,7 +259,7 @@ Example:
7 11

Empty ranges of the form ``n:n-1`` are sometimes used to indicate the inter-index
location between ``n-1`` and ``n``. For example, the :func:`searchsorted` function uses
location between ``n-1`` and ``n``. For example, the :func:`searchsorted` function uses
this convention to indicate the insertion point of a value not found in a sorted
array:

Expand Down Expand Up @@ -564,9 +564,9 @@ beyond the point of insertion have to be moved one place over.
All operations on sparse matrices are carefully implemented to exploit
the CSC data structure for performance, and to avoid expensive operations.

If you have data in CSC format from a different application or library,
If you have data in CSC format from a different application or library,
and wish to import it in Julia, make sure that you use 1-based indexing.
The row indices in every column need to be sorted. If your `SparseMatrixCSC`
The row indices in every column need to be sorted. If your `SparseMatrixCSC`
object contains unsorted row indices, one quick way to sort them is by
doing a double transpose.

Expand Down Expand Up @@ -640,7 +640,7 @@ into a sparse matrix using the :func:`sparse` function:
[4, 4] = 1.0
[5, 5] = 1.0

You can go in the other direction using the :func:`full` function. The
You can go in the other direction using the :func:`full` function. The
:func:`issparse` function can be used to query if a matrix is sparse.

.. doctest::
Expand Down Expand Up @@ -705,10 +705,8 @@ reference.
| | | distribution. (Requires the |
| | | ``Distributions`` package.) |
+----------------------------------------+----------------------------------+--------------------------------------------+
| :func:`sprandbool(m,n,d) <sprandbool>` | :func:`randbool(m,n) <randbool>` | Creates a *m*-by-*n* random matrix (of |
| :func:`sprandbool(m,n,d) <sprandbool>` | :func:`rand(Bool,m,n) <rand>` | Creates a *m*-by-*n* random matrix (of |
| | | density *d*) with non-zero ``Bool`` |
| | | elements with probability *d* (*d* =0.5 |
| | | for :func:`randbool`.) |
| | | for :func:`rand(Bool) <rand>`.) |
+----------------------------------------+----------------------------------+--------------------------------------------+


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 b365c36

Please sign in to comment.