diff --git a/base/bitarray.jl b/base/bitarray.jl index 6900d54318320..c27646a7fcec0 100644 --- a/base/bitarray.jl +++ b/base/bitarray.jl @@ -239,10 +239,10 @@ function bitarray_rand_fill!(B::BitArray) end Bc = B.chunks for i = 1 : length(Bc) - 1 - Bc[i] = randi(Uint64) + Bc[i] = rand(Uint64) end msk = @_msk_end length(B) - Bc[end] = msk & randi(Uint64) + Bc[end] = msk & rand(Uint64) return B end diff --git a/base/combinatorics.jl b/base/combinatorics.jl index b08c3536449b4..13c7c6d9d3335 100644 --- a/base/combinatorics.jl +++ b/base/combinatorics.jl @@ -64,7 +64,7 @@ pascal(n) = [binomial(i+j-2,i-1) for i=1:n,j=1:n] function shuffle!(a::AbstractVector) for i = length(a):-1:2 - j = randi(i) + j = rand(1:i) a[i], a[j] = a[j], a[i] end return a @@ -76,7 +76,7 @@ function randperm(n::Integer) a = Array(typeof(n), n) a[1] = 1 for i = 2:n - j = randi(i) + j = rand(1:i) a[i] = a[j] a[j] = i end @@ -87,7 +87,7 @@ function randcycle(n::Integer) a = Array(typeof(n), n) a[1] = 1 for i = 2:n - j = randi(i-1) + j = rand(1:i-1) a[i] = a[j] a[j] = i end diff --git a/base/deprecated.jl b/base/deprecated.jl index a27f66173740f..3782d92510766 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -23,9 +23,6 @@ end @deprecate idump xdump @deprecate cwd pwd @deprecate strlen length -@deprecate chi2rnd randchi2 -@deprecate betarnd randbeta -@deprecate exprnd randexp @deprecate islogical isbool @deprecate csvread readcsv @deprecate dlmread readdlm @@ -35,3 +32,23 @@ end @deprecate uc uppercase @deprecate nCr binomial @deprecate julia_pkgdir Pkg.dir +@deprecate chi2rnd randchi2 +@deprecate betarnd randbeta +@deprecate exprnd randexp + +randi(x...) = error("randi is deprecated. Instead use: rand(r::Range)") + +randival(x...) = error("randi is deprecated. Instead use: rand(r::Range)") + +randexp(x...) = error("randexp is deprecated. Instead use the Distributions package: + using Distributions; rand(Exponential())") + +randg(x...) = error("randg is deprecated. Instead use the Distributions package: + using Distributions; rand(Gamma())") + +randbeta(x...) = error("randbeta is deprecated. Instead use the Distributions package: + using Distributions; rand(Beta)") + +randchi2(x...) = error("randbeta is deprecated. Instead use the Distributions package: + using Distributions; rand(Chisq())") + diff --git a/base/exports.jl b/base/exports.jl index bc190d63d899b..2046b96612243 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -7,7 +7,7 @@ export LAPACK, BLAS, LibRandom, - RNG, + Random, Math, Sort, Test, @@ -935,7 +935,25 @@ export info, warn, -# statistics and random numbers +# random numbers + AbstractRNG, + MersenneTwister, + rand!, + rand, + randbool!, + randbool, + randn!, + randn, + # deprecated stuff + randbeta, + randchi2, + randexp, + randg, + randg2, + randi, + randival, + +# statistics autocor, cor, cor_pearson, @@ -957,28 +975,7 @@ export quantile, quartile, quintile, - rand!, - rand, - randbeta!, - randbeta, - randbool!, - randbool, - randchi2!, - randchi2, - randexp!, - randexp, - randg!, - randg, - randg2, - randi!, - randi, - randival!, - randival, - randn!, - randn, rle, - Rng, - Rng_MT, skewness, srand, std, diff --git a/base/file.jl b/base/file.jl index c4f0c36b92558..8d8c4233cad7a 100644 --- a/base/file.jl +++ b/base/file.jl @@ -142,7 +142,7 @@ end end @windows_only function mktempdir() - seed = randi(Uint32) + seed = rand(Uint32) while true filename = GetTempFileName(seed) ret = ccall(:_mkdir, Int32, (Ptr{Uint8},), filename) diff --git a/base/lapack.jl b/base/lapack.jl index 9df44d1997989..cf63b3eea42f9 100644 --- a/base/lapack.jl +++ b/base/lapack.jl @@ -143,11 +143,11 @@ end # gegp3 - pivoted QR decomposition # gerqf - unpivoted RQ decomposition # getrf - LU decomposition -for (gebrd, gelqf, geqlf, geqrf, geqp3, gerqf, getrf, elty) in - ((:dgebrd_,:dgelqf_,:dgeqlf_,:dgeqrf_,:dgeqp3_,:dgerqf_,:dgetrf_,:Float64), - (:sgebrd_,:sgelqf_,:sgeqlf_,:sgeqrf_,:sgeqp3_,:sgerqf_,:sgetrf_,:Float32), - (:zgebrd_,:zgelqf_,:zgeqlf_,:zgeqrf_,:zgeqp3_,:zgerqf_,:zgetrf_,:Complex128), - (:cgebrd_,:cgelqf_,:cgeqlf_,:cgeqrf_,:cgeqp3_,:cgerqf_,:cgetrf_,:Complex64)) +for (gebrd, gelqf, geqlf, geqrf, geqp3, gerqf, getrf, elty, relty) in + ((:dgebrd_,:dgelqf_,:dgeqlf_,:dgeqrf_,:dgeqp3_,:dgerqf_,:dgetrf_,:Float64,:Float64), + (:sgebrd_,:sgelqf_,:sgeqlf_,:sgeqrf_,:sgeqp3_,:sgerqf_,:sgetrf_,:Float32,:Float32), + (:zgebrd_,:zgelqf_,:zgeqlf_,:zgeqrf_,:zgeqp3_,:zgerqf_,:zgetrf_,:Complex128,:Float64), + (:cgebrd_,:cgelqf_,:cgeqlf_,:cgeqrf_,:cgeqp3_,:cgerqf_,:cgetrf_,:Complex64,:Float32)) @eval begin # SUBROUTINE DGEBRD( M, N, A, LDA, D, E, TAUQ, TAUP, WORK, LWORK, # INFO ) @@ -248,15 +248,14 @@ for (gebrd, gelqf, geqlf, geqrf, geqp3, gerqf, getrf, elty) in work = Array($elty, 1) lwork = blas_int(-1) info = Array(BlasInt, 1) - Rtyp = typeof(real(A[1])) cmplx = iscomplex(A) - if cmplx rwork = Array(Rtyp, 2n) end + if cmplx; rwork = Array($relty, 2n); end for i in 1:2 if cmplx ccall(($(string(geqp3)),liblapack), Void, (Ptr{BlasInt}, Ptr{BlasInt}, Ptr{$elty}, Ptr{BlasInt}, Ptr{BlasInt}, Ptr{$elty}, Ptr{$elty}, Ptr{BlasInt}, - Ptr{Rtyp}, Ptr{BlasInt}), + Ptr{$relty}, Ptr{BlasInt}), &m, &n, A, &stride(A,2), jpvt, tau, work, &lwork, rwork, info) else ccall(($(string(geqp3)),liblapack), Void, @@ -455,8 +454,8 @@ for (gels, gesv, getrs, getri, elty) in end end end -for (gelsd, elty) in ((:dgelsd_, Float64), - (:sgelsd_, Float32)) +for (gelsd, elty) in ((:dgelsd_, :Float64), + (:sgelsd_, :Float32)) @eval begin # SUBROUTINE DGELSD( M, N, NRHS, A, LDA, B, LDB, S, RCOND, RANK, # $ WORK, LWORK, IWORK, INFO ) @@ -503,8 +502,8 @@ for (gelsd, elty) in ((:dgelsd_, Float64), gelsd!(A::StridedMatrix{$elty}, B::StridedVecOrMat{$elty}) = gelsd!(A, B, -1.) end end -for (gelsd, elty, relty) in ((:zgelsd_, Complex128, Float64), - (:cgelsd_, Complex64, Float32)) +for (gelsd, elty, relty) in ((:zgelsd_, :Complex128, :Float64), + (:cgelsd_, :Complex64, :Float32)) @eval begin # SUBROUTINE ZGELSD( M, N, NRHS, A, LDA, B, LDB, S, RCOND, RANK, # $ WORK, LWORK, RWORK, IWORK, INFO ) @@ -556,11 +555,11 @@ for (gelsd, elty, relty) in ((:zgelsd_, Complex128, Float64), end # (GE) general matrices eigenvalue-eigenvector and singular value decompositions -for (geev, gesvd, gesdd, elty) in - ((:dgeev_,:dgesvd_,:dgesdd_,:Float64), - (:sgeev_,:sgesvd_,:sgesdd_,:Float32), - (:zgeev_,:zgesvd_,:zgesdd_,:Complex128), - (:cgeev_,:cgesvd_,:cgesdd_,:Complex64)) +for (geev, gesvd, gesdd, elty, relty) in + ((:dgeev_,:dgesvd_,:dgesdd_,:Float64,Float64), + (:sgeev_,:sgesvd_,:sgesdd_,:Float32,:Float32), + (:zgeev_,:zgesvd_,:zgesdd_,:Complex128,:Float64), + (:cgeev_,:cgesvd_,:cgesdd_,:Complex64,:Float32)) @eval begin # SUBROUTINE DGEEV( JOBVL, JOBVR, N, A, LDA, WR, WI, VL, LDVL, VR, # $ LDVR, WORK, LWORK, INFO ) @@ -578,11 +577,10 @@ for (geev, gesvd, gesdd, elty) in rvecs = jobvr == 'V' VL = Array($elty, (n, lvecs ? n : 0)) VR = Array($elty, (n, rvecs ? n : 0)) - Rtyp = typeof(real(A[1])) cmplx = iscomplex(A) if cmplx W = Array($elty, n) - rwork = Array(Rtyp, 2n) + rwork = Array($relty, 2n) else WR = Array($elty, n) WI = Array($elty, n) @@ -596,7 +594,7 @@ for (geev, gesvd, gesdd, elty) in (Ptr{Uint8}, Ptr{Uint8}, Ptr{BlasInt}, Ptr{$elty}, Ptr{BlasInt}, Ptr{$elty}, Ptr{$elty}, Ptr{BlasInt}, Ptr{$elty}, Ptr{BlasInt}, Ptr{$elty}, Ptr{BlasInt}, - Ptr{Rtyp}, Ptr{BlasInt}), + Ptr{$relty}, Ptr{BlasInt}), &jobvl, &jobvr, &n, A, &stride(A,2), W, VL, &n, VR, &n, work, &lwork, rwork, info) else @@ -645,11 +643,10 @@ for (geev, gesvd, gesdd, elty) in end work = Array($elty, 1) lwork = blas_int(-1) - Rtyp = typeof(real(A[1])) - S = Array(Rtyp, minmn) + S = Array($relty, minmn) cmplx = iscomplex(A) if cmplx - rwork = Array(Rtyp, job == 'N' ? 7*minmn : 5*minmn*minmn + 5*minmn) + rwork = Array($relty, job == 'N' ? 7*minmn : 5*minmn*minmn + 5*minmn) end iwork = Array(BlasInt, 8*minmn) info = Array(BlasInt, 1) @@ -659,7 +656,7 @@ for (geev, gesvd, gesdd, elty) in (Ptr{Uint8}, Ptr{BlasInt}, Ptr{BlasInt}, Ptr{$elty}, Ptr{BlasInt}, Ptr{$elty}, Ptr{$elty}, Ptr{BlasInt}, Ptr{$elty}, Ptr{BlasInt}, Ptr{$elty}, Ptr{BlasInt}, - Ptr{Rtyp}, Ptr{BlasInt}, Ptr{BlasInt}), + Ptr{$relty}, Ptr{BlasInt}, Ptr{BlasInt}), &job, &m, &n, A, &stride(A,2), S, U, &max(1,stride(U,2)), VT, &max(1,stride(VT,2)), work, &lwork, rwork, iwork, info) else @@ -693,15 +690,14 @@ for (geev, gesvd, gesdd, elty) in # $ VT( LDVT, * ), WORK( * ) function gesvd!(jobu::BlasChar, jobvt::BlasChar, A::StridedMatrix{$elty}) chkstride1(A) - Rtyp = typeof(real(A[1])) m, n = size(A) minmn = min(m, n) - S = Array(Rtyp, minmn) + S = Array($relty, minmn) U = Array($elty, jobu == 'A'? (m, m):(jobu == 'S'? (m, minmn) : (m, 0))) VT = Array($elty, jobvt == 'A'? (n, n):(jobvt == 'S'? (minmn, n) : (n, 0))) work = Array($elty, 1) cmplx = iscomplex(A) - if cmplx rwork = Array(Rtyp, 5minmn) end + if cmplx; rwork = Array($relty, 5minmn); end lwork = blas_int(-1) info = Array(BlasInt, 1) for i in 1:2 @@ -710,7 +706,7 @@ for (geev, gesvd, gesdd, elty) in (Ptr{Uint8}, Ptr{Uint8}, Ptr{BlasInt}, Ptr{BlasInt}, Ptr{$elty}, Ptr{BlasInt}, Ptr{$elty}, Ptr{$elty}, Ptr{BlasInt}, Ptr{$elty}, Ptr{BlasInt}, Ptr{$elty}, - Ptr{BlasInt}, Ptr{Rtyp}, Ptr{BlasInt}), + Ptr{BlasInt}, Ptr{$relty}, Ptr{BlasInt}), &jobu, &jobvt, &m, &n, A, &stride(A,2), S, U, &max(1,stride(U,2)), VT, &max(1,stride(VT,2)), work, &lwork, rwork, info) else @@ -1130,7 +1126,7 @@ end ## (TR) triangular matrices: solver and inverse for (trtri, trtrs, elty) in ((:dtrtri_,:dtrtrs_,:Float64), - (:strtri_,:strtrs_,Float32), + (:strtri_,:strtrs_,:Float32), (:ztrtri_,:ztrtrs_,:Complex128), (:ctrtri_,:ctrtrs_,:Complex64)) @eval begin @@ -1203,11 +1199,11 @@ end ## (SY) symmetric matrices - eigendecomposition, Bunch-Kaufman decomposition, ## solvers (direct and factored) and inverse. -for (syconv, syev, sysv, sytrf, sytri, sytrs, elty) in - ((:dsyconv_,:dsyev_,:dsysv_,:dsytrf_,:dsytri_,:dsytrs_,:Float64), - (:ssyconv_,:ssyev_,:ssysv_,:ssytrf_,:ssytri_,:ssytrs_,:Float32), - (:zheconv_,:zheev_,:zhesv_,:zhetrf_,:zhetri_,:zhetrs_,:Complex128), - (:checonv_,:cheev_,:chesv_,:chetrf_,:chetri_,:chetrs_,:Complex64)) +for (syconv, syev, sysv, sytrf, sytri, sytrs, elty, relty) in + ((:dsyconv_,:dsyev_,:dsysv_,:dsytrf_,:dsytri_,:dsytrs_,:Float64, :Float64), + (:ssyconv_,:ssyev_,:ssysv_,:ssytrf_,:ssytri_,:ssytrs_,:Float32, :Float32), + (:zheconv_,:zheev_,:zhesv_,:zhetrf_,:zhetri_,:zhetrs_,:Complex128, :Float64), + (:checonv_,:cheev_,:chesv_,:chetrf_,:chetri_,:chetrs_,:Complex64, :Float32)) @eval begin # SUBROUTINE DSYCONV( UPLO, WAY, N, A, LDA, IPIV, WORK, INFO ) # * .. Scalar Arguments .. @@ -1239,25 +1235,24 @@ for (syconv, syev, sysv, sytrf, sytri, sytrs, elty) in chkstride1(A) chksquare(A) cmplx = iscomplex(A) - Rtyp = typeof(real(A[1])) n = size(A, 1) - W = Array(Rtyp, n) + W = Array($relty, n) work = Array($elty, 1) lwork = blas_int(-1) if cmplx - rwork = Array(Rtyp, max(1, 3n-2)) + rwork = Array($relty, max(1, 3n-2)) end info = Array(BlasInt, 1) for i in 1:2 if cmplx ccall(($(string(syev)),liblapack), Void, (Ptr{Uint8}, Ptr{Uint8}, Ptr{BlasInt}, Ptr{$elty}, Ptr{BlasInt}, - Ptr{Rtyp}, Ptr{$elty}, Ptr{BlasInt}, Ptr{Rtyp}, Ptr{BlasInt}), + Ptr{$relty}, Ptr{$elty}, Ptr{BlasInt}, Ptr{$relty}, Ptr{BlasInt}), &jobz, &uplo, &n, A, &stride(A,2), W, work, &lwork, rwork, info) else ccall(($(string(syev)),liblapack), Void, (Ptr{Uint8}, Ptr{Uint8}, Ptr{BlasInt}, Ptr{$elty}, Ptr{BlasInt}, - Ptr{Rtyp}, Ptr{$elty}, Ptr{BlasInt}, Ptr{BlasInt}), + Ptr{$relty}, Ptr{$elty}, Ptr{BlasInt}, Ptr{BlasInt}), &jobz, &uplo, &n, A, &stride(A,2), W, work, &lwork, info) end if info[1] != 0 throw(LapackException(info[1])) end @@ -1777,4 +1772,4 @@ for (gees, elty, relty) in end end end -end # module \ No newline at end of file +end # module diff --git a/base/random.jl b/base/random.jl new file mode 100644 index 0000000000000..0f08e99eb826e --- /dev/null +++ b/base/random.jl @@ -0,0 +1,183 @@ +module Random + +using Base.LibRandom + +export librandom_init, srand, + rand, rand!, + randn, randn!, + randbool, randbool!, + AbstractRNG, RNG, MersenneTwister + +abstract AbstractRNG + +type MersenneTwister <: AbstractRNG + state::DSFMT_state + seed::Union(Uint32,Vector{Uint32}) + len::Int # Use for iteration. Set to -1 otherwise. + + function MersenneTwister() + seed = uint32(0) + state = DSFMT_state() + dsfmt_init_gen_rand(state, seed) + return new(state, seed, -1) + end + + MersenneTwister(seed) = MersenneTwister(seed, -1) + + function MersenneTwister(seed::Uint32, len::Int) + state = DSFMT_state() + dsfmt_init_gen_rand(state, seed) + return new(state, seed, len) + end + + function MersenneTwister(seed::Vector{Uint32}, len::Int) + state = DSFMT_state() + dsfmt_init_by_array(state, seed) + return new(state, seed, len) + end +end + +function srand(r::MersenneTwister, seed) + r.seed = seed + dsfmt_init_gen_rand(r.state, seed) + return r +end + +## initialization + +function librandom_init() + +@unix_only begin + try + srand("/dev/urandom") + catch + println(STDERR, "Entropy pool not available to seed RNG, using ad-hoc entropy sources.") + seed = reinterpret(Uint64, time()) + seed = bitmix(seed, uint64(getpid())) + try + seed = bitmix(seed, parse_int(Uint64, readall(`ifconfig`|`sha1sum`)[1:40], 16)) + catch + # ignore + end + srand(seed) + end +end + +@windows_only begin + a = zeros(Uint32, 2) + win32_SystemFunction036!(a) + srand(a) +end + randmtzig_create_ziggurat_tables() +end + +## srand() + +function srand(seed::Vector{Uint32}) + global RANDOM_SEED = seed + dsfmt_gv_init_by_array(seed) +end +srand(n::Integer) = srand(make_seed(n)) + +function make_seed(n::Integer) + n < 0 && throw(DomainError()) + seed = Uint32[] + while true + push!(seed, n & 0xffffffff) + n >>= 32 + if n == 0 + return seed + end + end +end + +function srand(filename::String, n::Integer) + open(filename) do io + a = Array(Uint32, int(n)) + read(io, a) + srand(a) + end +end +srand(filename::String) = srand(filename, 4) + +## rand() + +rand() = dsfmt_gv_genrand_close_open() +rand(r::MersenneTwister) = dsfmt_genrand_close_open(r.state) + +rand!(A::Array{Float64}) = dsfmt_gv_fill_array_close_open!(A) +rand(dims::Dims) = rand!(Array(Float64, dims)) +rand(dims::Int...) = rand(dims) + +rand!(r::MersenneTwister, A::Array{Float64}) = dsfmt_fill_array_close_open!(r.state, A) +rand(r::AbstractRNG, dims::Dims) = rand!(r, Array(Float64, dims)) +rand(r::AbstractRNG, dims::Int...) = rand(r, dims) + +## random integers + +dsfmt_randui32() = dsfmt_gv_genrand_uint32() +dsfmt_randui64() = uint64(dsfmt_randui32()) | (uint64(dsfmt_randui32())<<32) + +rand(::Type{Uint32}) = dsfmt_randui32() +rand(::Type{Uint64}) = dsfmt_randui64() +rand(::Type{Uint128}) = uint128(rand(Uint64))<<64 | rand(Uint64) + +rand(::Type{Int32}) = int32(rand(Uint32)) & typemax(Int32) +rand(::Type{Int64}) = int64(rand(Uint64)) & typemax(Int64) +rand(::Type{Int128}) = int128(rand(Uint128)) & typemax(Int128) + +# random integer from lo to hi inclusive +function rand{T<:Integer}(r::Range1{T}) + lo = r[1] + hi = r[end] + + m = typemax(T) + s = rand(T) + if (hi-lo == m) + return s + lo + end + r = hi-lo+1 + if (r&(r-1))==0 + # power of 2 range + return s&(r-1) + lo + end + # note: m>=0 && r>=0 + lim = m - rem(rem(m,r)+1, r) + while s > lim + s = rand(T) + end + return rem(s,r) + lo +end + +function rand!{T<:Integer}(r::Range1{T}, A::Array{T}) + for i=1:length(A) + A[i] = rand(r) + end + return A +end + +rand{T<:Integer}(r::Range1{T}, dims::Dims) = rand!(r, Array(T, dims)) +rand{T<:Integer}(r::Range1{T}, dims::Int...) = rand(r, dims) + +## random Bools + +rand!(B::BitArray) = Base.bitarray_rand_fill!(B) + +randbool(dims::Dims) = rand!(BitArray(dims)) +randbool(dims::Int...) = rand!(BitArray(dims)) + +randbool() = ((dsfmt_randui32() & 1) == 1) +randbool!(B::BitArray) = rand!(B) +randbool!(A::Array) = [ randbool() for i in 1:length(A) ] + +## randn() - Normally distributed random numbers using Ziggurat algorithm + +# The Ziggurat Method for generating random variables - Marsaglia and Tsang +# Paper and reference code: http://www.jstatsoft.org/v05/i08/ + +randn() = randmtzig_randn() +randn!(A::Array{Float64}) = randmtzig_fill_randn!(A) +randn(dims::Dims) = randn!(Array(Float64, dims)) +randn(dims::Int...) = randn(dims) + +end # module diff --git a/base/rng.jl b/base/rng.jl deleted file mode 100644 index 62e270dd086b1..0000000000000 --- a/base/rng.jl +++ /dev/null @@ -1,288 +0,0 @@ -module RNG - -using Base.LibRandom - -export librandom_init, srand, - rand, rand!, - randn, randn!, - randi, randi!, randival, randival!, - randg, randg!, - randexp, randexp!, - randchi2, randchi2!, - randbeta, randbeta!, - randbool, randbool!, - Rng, Rng_MT - -abstract Rng - -start(r::Rng) = 0 -done(r::Rng, count) = r.len == count ? true : false -next(r::Rng, count) = (rand(r), count + 1) - -type Rng_MT <: Rng - state::DSFMT_state - seed::Union(Uint32,Vector{Uint32}) - len::Int # Use for iteration. Set to -1 otherwise. - - function Rng_MT() - seed = uint32(0) - state = DSFMT_state() - dsfmt_init_gen_rand(state, seed) - return new(state, seed, -1) - end - - Rng_MT(seed) = Rng_MT(seed, -1) - - function Rng_MT(seed::Uint32, len::Int) - state = DSFMT_state() - dsfmt_init_gen_rand(state, seed) - return new(state, seed, len) - end - - function Rng_MT(seed::Vector{Uint32}, len::Int) - state = DSFMT_state() - dsfmt_init_by_array(state, seed) - return new(state, seed, len) - end -end - -function srand(r::Rng_MT, seed) - r.seed = seed - dsfmt_init_gen_rand(r.state, seed) - return r -end - -## initialization - -function librandom_init() - -@unix_only begin - try - srand("/dev/urandom") - catch - println(STDERR, "Entropy pool not available to seed RNG, using ad-hoc entropy sources.") - seed = reinterpret(Uint64, time()) - seed = bitmix(seed, uint64(getpid())) - try - seed = bitmix(seed, parse_int(Uint64, readall(`ifconfig`|`sha1sum`)[1:40], 16)) - catch - # ignore - end - srand(seed) - end -end - -@windows_only begin - a = zeros(Uint32, 2) - win32_SystemFunction036!(a) - srand(a) -end - randmtzig_create_ziggurat_tables() -end - -## srand() - -function srand(seed::Vector{Uint32}) - global RANDOM_SEED = seed - dsfmt_gv_init_by_array(seed) -end -srand(n::Integer) = srand(make_seed(n)) - -function make_seed(n::Integer) - n < 0 && throw(DomainError()) - seed = Uint32[] - while true - push!(seed, n & 0xffffffff) - n >>= 32 - if n == 0 - return seed - end - end -end - -function srand(filename::String, n::Integer) - open(filename) do io - a = Array(Uint32, int(n)) - read(io, a) - srand(a) - end -end -srand(filename::String) = srand(filename, 4) - -## rand() - -rand() = dsfmt_gv_genrand_close_open() -rand(r::Rng_MT) = dsfmt_genrand_close_open(r.state) - -rand!(A::Array{Float64}) = dsfmt_gv_fill_array_close_open!(A) -rand(dims::Dims) = rand!(Array(Float64, dims)) -rand(dims::Int...) = rand(dims) - -rand!(r::Rng_MT, A::Array{Float64}) = dsfmt_fill_array_close_open!(r.state, A) -rand(r::Rng, dims::Dims) = rand!(r, Array(Float64, dims)) -rand(r::Rng, dims::Int...) = rand(r, dims) - -## random integers - -dsfmt_randui32() = dsfmt_gv_genrand_uint32() -dsfmt_randui64() = uint64(dsfmt_randui32()) | (uint64(dsfmt_randui32())<<32) - -randi(::Type{Uint32}) = dsfmt_randui32() -randi(::Type{Uint64}) = dsfmt_randui64() -randi(::Type{Uint128}) = uint128(randi(Uint64))<<64 | randi(Uint64) - -randi(::Type{Int32}) = int32(randi(Uint32)) & typemax(Int32) -randi(::Type{Int64}) = int64(randi(Uint64)) & typemax(Int64) -randi(::Type{Int128}) = int128(randi(Uint128)) & typemax(Int128) - -randi() = randi(Int) - -# random integer from lo to hi inclusive -function randival{T<:Integer}(lo::T, hi::T) - if lo > hi - error("randi: invalid range") - end - m = typemax(T) - s = randi(T) - if (hi-lo == m) - return s + lo - end - r = hi-lo+1 - if (r&(r-1))==0 - # power of 2 range - return s&(r-1) + lo - end - # note: m>=0 && r>=0 - lim = m - rem(rem(m,r)+1, r) - while s > lim - s = randi(T) - end - return rem(s,r) + lo -end - -function randival!{T<:Integer}(lo, hi, A::Array{T}) - lo = convert(T,lo) - hi = convert(T,hi) - for i = 1:length(A) - A[i] = randival(lo, hi) - end - return A -end -randival{T<:Integer}(lo::T, hi::T, dims::Dims) = randival!(lo, hi, Array(T, dims)) -randival(lo, hi, dims::Dims) = randival(promote(lo, hi)..., dims) -randival(lo, hi, dims::Int...) = randival(lo, hi, dims) - -randi!(max::Integer, A::Array) = randival!(one(max), max, A) -randi!(r::(Integer,Integer), A::Array) = randival!(r[1], r[2], A) - -randi(max::Integer) = randival(one(max), max) -randi(max::Integer, dims::Dims) = randival(one(max), max, dims) -randi(max::Integer, dims::Int...) = randival(one(max), max, dims) -randi(r::(Integer,Integer)) = randival(r[1], r[2]) -randi(r::(Integer,Integer), dims::Dims) = randival(r[1], r[2], dims) -randi(r::(Integer,Integer), dims::Int...) = randival(r[1], r[2], dims) - -## random Bools - -rand!(B::BitArray) = Base.bitarray_rand_fill!(B) - -randbool(dims::Dims) = rand!(BitArray(dims)) -randbool(dims::Int...) = rand!(BitArray(dims)) - -randbool() = ((dsfmt_randui32() & 1) == 1) - -randbool!(B::BitArray) = rand!(B) - -function randbool!(A::Array) - for i = 1:length(A) - A[i] = randbool() - end - return A -end - -## randn() - Normally distributed random numbers using Ziggurat algorithm - -# The Ziggurat Method for generating random variables - Marsaglia and Tsang -# Paper and reference code: http://www.jstatsoft.org/v05/i08/ - -randn() = randmtzig_randn() -randn!(A::Array{Float64}) = randmtzig_fill_randn!(A) -randn(dims::Dims) = randn!(Array(Float64, dims)) -randn(dims::Int...) = randn(dims) - -## randexp() - Exponentially distributed random numbers using Ziggurat algorithm - -randexp() = randmtzig_exprnd() -randexp!(A::Array{Float64}) = randmtzig_fill_exprnd!(A) -randexp(dims::Dims) = randexp!(Array(Float64, dims)) -randexp(dims::Int...) = randexp(dims) - -## randg() - -# A simple method for generating gamma variables - Marsaglia and Tsang (2000) -# http://www.cparity.com/projects/AcmClassification/samples/358414.pdf -# Page 369 - -# basic simulation loop for pre-computed d and c -function randg2(d::Float64, c::Float64) - while true - x = v = 0.0 - while v <= 0.0 - x = randn() - v = 1.0 + c*x - end - v = v^3 - U = rand() - x2 = x^2 - if U < 1.0-0.331*x2^2 || log(U) < 0.5*x2+d*(1.0-v+log(v)) - return d*v - end - end -end - -function randg!(a::Real, A::Array{Float64}) - if a <= 0. error("shape parameter a must be > 0") end - d = (a <= 1. ? a + 1 : a) - 1.0/3.0 - c = 1.0/sqrt(9.0d) - for i in 1:length(A) A[i] = randg2(d, c) end - if a <= 1. - ainv = 1./a - for i in 1:length(A) A[i] *= rand()^ainv end - end - A -end - -function randg(a::Real) - if a <= 0. error("shape parameter a must be > 0") end - d = (a <= 1. ? a + 1 : a) - 1.0/3.0 - randg2(d, 1.0/sqrt(9.0d)) * (a > 1. ? 1. : rand()^(1./a)) -end - -randg(a::Real, dims::Dims) = randg!(a, Array(Float64, dims)) -randg(a::Real, dims::Int...) = randg(a, dims) - -## randchi2 - the distribution chi^2(df) is 2*gamma(df/2) -## for integer n, a chi^2(n) is the sum of n squared standard normals - -function randchi2!(df::Real, A::Array{Float64}) - if df == 1 - for i in 1:length(A) - A[i] = randn()^2 - end - return A - end - d = df >= 2 ? df/2. - 1.0/3.0 : error("require degrees of freedom df >= 2") - c = 1.0/sqrt(9.0d) - for i in 1:length(A) A[i] = 2.randg2(d,c) end - A -end - -randchi2(df::Real) = df == 1 ? randn()^2 : 2.randg(df/2.) -randchi2(df::Real, dims::Dims) = randchi2!(df, Array(Float64, dims)) -randchi2(df::Real, dims::Int...) = randchi2(df, dims) - -randbeta(alpha::Real, beta::Real) = (u = randg(alpha); u / (u + randg(beta))) -randbeta(alpha::Real, beta::Real, dims::Dims) = (u = randg(alpha, dims); u ./ (u + randg(beta, dims))) -randbeta(alpha::Real, beta::Real, dims::Int...) = randbeta(alpha, beta, dims) - -end # module diff --git a/base/sparse.jl b/base/sparse.jl index 854248c921032..3d2930fee9e14 100644 --- a/base/sparse.jl +++ b/base/sparse.jl @@ -404,8 +404,8 @@ end function sprand(m::Integer, n::Integer, density::FloatingPoint, rng::Function, v) numnz = int(m*n*density) - I = randival!(1, m, Array(Int, numnz)) - J = randival!(1, n, Array(Int, numnz)) + I = rand!(1:m, Array(Int, numnz)) + J = rand!(1:n, Array(Int, numnz)) S = sparse(I, J, v, m, n) if !isbool(v) S.nzval = rng(nnz(S)) diff --git a/base/sysimg.jl b/base/sysimg.jl index 245c6f966285b..4e0940bf6b40f 100644 --- a/base/sysimg.jl +++ b/base/sysimg.jl @@ -124,8 +124,8 @@ importall Math # random number generation and statistics include("statistics.jl") include("librandom.jl") -include("rng.jl") -importall RNG +include("random.jl") +importall Random # Combinatorics include("sort.jl") @@ -200,10 +200,10 @@ compile_hint(cmp, (Int32, Int32)) compile_hint(min, (Int32, Int32)) compile_hint(==, (ASCIIString, ASCIIString)) compile_hint(arg_gen, (ASCIIString,)) -compile_hint(RNG.librandom_init, ()) -compile_hint(RNG.srand, (ASCIIString, Int)) +compile_hint(Random.librandom_init, ()) +compile_hint(Random.srand, (ASCIIString, Int)) +compile_hint(Random.srand, (Uint64,)) compile_hint(open, (ASCIIString, Bool, Bool, Bool, Bool)) -compile_hint(RNG.srand, (Uint64,)) compile_hint(done, (IntSet, Int64)) compile_hint(next, (IntSet, Int64)) compile_hint(ht_keyindex, (Dict{Any,Any}, Int32)) diff --git a/doc/stdlib/base.rst b/doc/stdlib/base.rst index 490a7c66c3dc5..7f376e484c1c7 100644 --- a/doc/stdlib/base.rst +++ b/doc/stdlib/base.rst @@ -1346,59 +1346,51 @@ Numbers Random Numbers -------------- -Random numbers are generated in Julia by calling functions from the `Mersenne Twister library `_ +Random number generateion in Julia uses the `Mersenne Twister library `_. Julia has a global RNG, which is used by default. Multiple RNGs can be plugged in using the ``AbstractRNG`` object, which can then be used to have multiple streams of random numbers. Currently, only ``MersenneTwister`` is supported. -.. function:: rand +.. function:: srand([rng], seed) - Generate a ``Float64`` random number in (0,1) - -.. function:: randf - - Generate a ``Float32`` random number in (0,1) - -.. function:: randi(Int32|Uint32|Int64|Uint64) + Seed the RNG with a ``seed``, which may be an unsigned integer or a vector of unsigned integers. ``seed`` can even be a filename, in which case the seed is read from a file. If the argument ``rng`` is not provided, the default global RNG is seeded. - Generate a random integer of the given type - -.. function:: randi(n) +.. function:: MersenneTwister([seed]) - Generate a random integer from 1 to ``n`` inclusive + Create a ``MersenneTwister`` RNG object. Different RNG objects can have their own seeds, which may be useful for generating different streams of random numbers. -.. function:: randi(n, dims...) +.. function:: rand() - Generate an array of random integers from 1 to ``n`` inclusive + Generate a ``Float64`` random number in (0,1) -.. function:: randi((a,b)) +.. function:: rand!([rng], A) - Generate a random integer in the interval from ``a`` to ``b`` inclusive. The argument is a tuple. + Populate the array A with random number generated from the specified RNG. -.. function:: randi((a,b), dims...) +.. function:: rand(rng::AbstractRNG, [dims...]) - Generate an array of random integers in the interval from ``a`` to ``b`` inclusive. The first argument is a tuple. + Generate a random ``Float64`` number or array of the size specified by dims, using the specified RNG object. Currently, ``MersenneTwister`` is the only available Random Number Generator (RNG), which may be seeded using srand. -.. function:: randbool +.. function:: rand(dims...) - Generate a random boolean value + Generate a random ``Float64`` array of the size specified by dims -.. function:: randn +.. function:: rand(Int32|Uint32|Int64|Uint64) - Generate a normally-distributed random number with mean 0 and standard deviation 1 + Generate a random integer of the given type -.. function:: randg(a) +.. function:: rand(r, [dims...]) - Generate a sample from the gamma distribution with shape parameter ``a`` + Generate a random integer from ``1``:``n`` inclusive. Optionally, generate a random integer array. -.. function:: randchi2(n) +.. function:: randbool([dims...]) - Generate a sample from the chi-squared distribution with ``n`` degrees of freedom (also available as ``chi2rnd``) + Generate a random boolean value. Optionally, generate an array of random boolean values. -.. function:: randexp +.. function:: randbool!(A) - Generate samples from the exponential distribution + Fill an array with random boolean values. A may be an ``Array`` or a ``BitArray``. -.. function:: srand +.. function:: randn([dims...]) - Seed the RNG + Generate a normally-distributed random number with mean 0 and standard deviation 1. Optionally generate an array of normally-distributed random numbers. Arrays ------ diff --git a/test/arrayperf.jl b/test/arrayperf.jl index a6ecdc348b2fa..10d1a849a5f7d 100644 --- a/test/arrayperf.jl +++ b/test/arrayperf.jl @@ -38,12 +38,12 @@ if run_ref for n_dims in 1:10 sz = ntuple(n_dims,i->lensmall) A = randn(sz) - ind = ntuple(n_dims,i -> ((i <= ceil(n_dims/2)) ? (1:sz[i]) : (randi(sz[i])))) + ind = ntuple(n_dims,i -> ((i <= ceil(n_dims/2)) ? (1:sz[i]) : (rand(1:sz[i])))) n_el = prod(map(length,ind)) n_r = iceil(n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") @time for i = 1:n_r - # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (randi(sz[i])))) + # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) B = A[ind...] end end @@ -51,12 +51,12 @@ if run_ref for n_dims in 1:length(lenbig) sz = ntuple(n_dims,i->lenbig[n_dims]) A = randn(sz) - ind = ntuple(n_dims,i -> ((i <= ceil(n_dims/2)) ? (1:sz[i]) : (randi(sz[i])))) + ind = ntuple(n_dims,i -> ((i <= ceil(n_dims/2)) ? (1:sz[i]) : (rand(1:sz[i])))) n_el = prod(map(length,ind)) n_r = iceil(n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") @time for i = 1:n_r - # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (randi(sz[i])))) + # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) B = A[ind...] end end @@ -67,12 +67,12 @@ if run_ref for n_dims in 1:10 sz = ntuple(n_dims,i->lensmall) A = randn(sz) - ind = ntuple(n_dims,i -> ((i > n_dims/2) ? (1:sz[i]) : (randi(sz[i])))) + ind = ntuple(n_dims,i -> ((i > n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) n_el = prod(map(length,ind)) n_r = iceil(n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") @time for i = 1:n_r - # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (randi(sz[i])))) + # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) B = A[ind...] end end @@ -80,12 +80,12 @@ if run_ref for n_dims in 1:length(lenbig) sz = ntuple(n_dims,i->lenbig[n_dims]) A = randn(sz) - ind = ntuple(n_dims,i -> ((i > n_dims/2) ? (1:sz[i]) : (randi(sz[i])))) + ind = ntuple(n_dims,i -> ((i > n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) n_el = prod(map(length,ind)) n_r = iceil(n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") @time for i = 1:n_r - # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (randi(sz[i])))) + # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) B = A[ind...] end end @@ -95,7 +95,7 @@ if run_ref println("Random operations:") println("Small arrays:") function randind(len) - i = randi(6) + i = rand(1:6) indchoices = {1:len,1:iceil(len/2),1:iceil(3*len/4),2:2:len,1:iceil(len/2):len,len:-1:1} return indchoices[i] end @@ -159,13 +159,13 @@ if run_assign for n_dims in 1:10 sz = ntuple(n_dims,i->lensmall) B = zeros(sz) - ind = ntuple(n_dims,i -> ((i <= ceil(n_dims/2)) ? (1:sz[i]) : (randi(sz[i])))) + ind = ntuple(n_dims,i -> ((i <= ceil(n_dims/2)) ? (1:sz[i]) : (rand(1:sz[i])))) A = randn(map(length,ind)) n_el = prod(map(length,ind)) n_r = iceil(n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") @time for i = 1:n_r - # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (randi(sz[i])))) + # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) # A = randn(map(length,ind)) B[ind...] = A end @@ -174,13 +174,13 @@ if run_assign for n_dims in 1:length(lenbig) sz = ntuple(n_dims,i->lenbig[n_dims]) B = zeros(sz) - ind = ntuple(n_dims,i -> ((i <= ceil(n_dims/2)) ? (1:sz[i]) : (randi(sz[i])))) + ind = ntuple(n_dims,i -> ((i <= ceil(n_dims/2)) ? (1:sz[i]) : (rand(1:sz[i])))) A = randn(map(length,ind)) n_el = prod(map(length,ind)) n_r = iceil(n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") @time for i = 1:n_r -# ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (randi(sz[i])))) +# ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) # A = randn(map(length,ind)) B[ind...] = A end @@ -192,13 +192,13 @@ if run_assign for n_dims in 1:10 sz = ntuple(n_dims,i->lensmall) B = zeros(sz) - ind = ntuple(n_dims,i -> ((i > n_dims/2) ? (1:sz[i]) : (randi(sz[i])))) + ind = ntuple(n_dims,i -> ((i > n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) A = randn(map(length,ind)) n_el = prod(map(length,ind)) n_r = iceil(n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") @time for i = 1:n_r - # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (randi(sz[i])))) + # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) # A = randn(map(length,ind)) B[ind...] = A end @@ -207,13 +207,13 @@ if run_assign for n_dims in 1:length(lenbig) sz = ntuple(n_dims,i->lenbig[n_dims]) B = zeros(sz) - ind = ntuple(n_dims,i -> ((i > n_dims/2) ? (1:sz[i]) : (randi(sz[i])))) + ind = ntuple(n_dims,i -> ((i > n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) A = randn(map(length,ind)) n_el = prod(map(length,ind)) n_r = iceil(n_evals/n_el) print(n_dims, " dimensions (", n_r, " repeats, ", n_r*n_el, " operations): ") @time for i = 1:n_r - # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (randi(sz[i])))) + # ind = ntuple(n_dims,i -> ((i <= n_dims/2) ? (1:sz[i]) : (rand(1:sz[i])))) # A = randn(map(length,ind)) B[ind...] = A end @@ -224,7 +224,7 @@ if run_assign println("Random operations:") println("Small arrays:") function randind(len) - i = randi(6) + i = rand(1:6) indchoices = {1:len,1:iceil(len/2),1:iceil(3*len/4),2:2:len,1:iceil(len/2):len,len:-1:1} return indchoices[i] end diff --git a/test/bitarray.jl b/test/bitarray.jl index c7349c3b4ea95..875869caa0e5c 100644 --- a/test/bitarray.jl +++ b/test/bitarray.jl @@ -34,7 +34,7 @@ s4 = 4 b1 = randbool(n1, n2) @test isequal(bitpack(bitunpack(b1)), b1) -i1 = randi(2, n1, n2) - 1 +i1 = rand(1:2, n1, n2) - 1 @test isequal(bitunpack(bitpack(i1)), i1) timesofar("conversions") @@ -56,8 +56,8 @@ timesofar("utils") ## Indexing ## b1 = randbool(n1, n2) -m1 = randi(n1) -m2 = randi(n2) +m1 = rand(1:n1) +m2 = rand(1:n2) b2 = randbool(m1, m2) @check_bit_operation copy_to BitMatrix (b1, b2) @check_bit_operation ref BitMatrix (b1, 1:m1, m2:n2) @@ -68,8 +68,8 @@ b2 = randbool(m1, m2) b2 = randbool(m1) @check_bit_operation assign BitMatrix (b1, b2, 1:m1, m2) -for p1 = [randi(v1) 1 63 64 65 191 192 193] - for p2 = [randi(v1) 1 63 64 65 191 192 193] +for p1 = [rand(1:v1) 1 63 64 65 191 192 193] + for p2 = [rand(1:v1) 1 63 64 65 191 192 193] for n = 0 : min(v1 - p1 + 1, v1 - p2 + 1) b1 = randbool(v1) b2 = randbool(v1) @@ -145,7 +145,7 @@ end b1 = BitArray() i1 = bitunpack(b1) for m = 1 : v1 - j = randi(m) + j = rand(1:m) x = randbool() insert!(b1, j, x) insert!(i1, j, x) @@ -155,7 +155,7 @@ end b1 = randbool(v1) i1 = bitunpack(b1) for j in [63, 64, 65, 127, 128, 129, 191, 192, 193] - x = randi(2) - 1 + x = rand(1:2) - 1 insert!(b1, j, x) insert!(i1, j, x) @test isequal(bitunpack(b1), i1) @@ -164,7 +164,7 @@ end b1 = randbool(v1) i1 = bitunpack(b1) for m = v1 : -1 : 1 - j = randi(m) + j = rand(1:m) delete!(b1, j) delete!(i1, j) @test isequal(bitunpack(b1), i1) @@ -248,7 +248,7 @@ b2 = randbool(n1, n1) @check_bit_operation (\) Matrix{Float64} (b1, b1) b1 = randbool(n1, n2) -b2 = randi(10, n1, n2) +b2 = rand(1:10, n1, n2) @check_bit_operation (&) Matrix{Int} (b1, b2) @check_bit_operation (|) Matrix{Int} (b1, b2) @check_bit_operation ($) Matrix{Int} (b1, b2) @@ -291,7 +291,7 @@ timesofar("binary comparison") b1 = randbool(s1, s2, s3, s4) for d = 1 : 4 - j = randi(size(b1, d)) + j = rand(1:size(b1, d)) #for j = 1 : size(b1, d) @check_bit_operation slicedim BitArray{4} (b1, d, j) #end @@ -309,7 +309,7 @@ for m = 0 : v1 end b1 = randbool(v1) -for m = [randi(v1)-1 0 1 63 64 65 191 192 193 v1-1] +for m = [rand(1:v1)-1 0 1 63 64 65 191 192 193 v1-1] @test isequal(b1 << m, [ b1[m+1:end]; falses(m) ]) @test isequal(b1 >>> m, [ falses(m); b1[1:end-m] ]) @test isequal(rotl(b1, m), [ b1[m+1:end]; b1[1:m] ]) diff --git a/test/combinatorics.jl b/test/combinatorics.jl index 57b167d27ec1d..a047e973b638c 100644 --- a/test/combinatorics.jl +++ b/test/combinatorics.jl @@ -44,7 +44,7 @@ for i = -5:.5:4 @test search_sorted_last_r(rg_r, i) == search_sorted_last_r(rgv_r, i) end -a = randi(10000, 1000) +a = rand(1:10000, 1000) # insertion_sort for _sort in [insertionsort, mergesort, timsort] diff --git a/test/corelib.jl b/test/corelib.jl index 61f163035fd68..3627f6e6785e1 100644 --- a/test/corelib.jl +++ b/test/corelib.jl @@ -148,7 +148,7 @@ end @test !isequal({1 => 1}, {2 => 1}) # Generate some data to populate dicts to be compared -data_in = [ (randi(1000), randstring(2)) for _ in 1:1001 ] +data_in = [ (rand(1:1000), randstring(2)) for _ in 1:1001 ] # Populate the first dict d1 = Dict{Int, String}(length(data_in)) @@ -158,7 +158,7 @@ end data_in = pairs(d1) # shuffle the data for i in 1:length(data_in) - j = randi(length(data_in)) + j = rand(1:length(data_in)) data_in[i], data_in[j] = data_in[j], data_in[i] end # Inserting data in different (shuffled) order should result in @@ -172,10 +172,10 @@ end d3 = copy(d2) d4 = copy(d2) # Removing an item gives different dict -delete!(d1, data_in[randi(length(data_in))][1]) +delete!(d1, data_in[rand(1:length(data_in))][1]) @test !isequal(d1, d2) # Changing a value gives different dict -d3[data_in[randi(length(data_in))][1]] = randstring(3) +d3[data_in[rand(1:length(data_in))][1]] = randstring(3) !isequal(d1, d3) # Adding a pair gives different dict d4[1001] = randstring(3) diff --git a/test/gzip.jl b/test/gzip.jl index a7ed2d36712fa..28e4f181df28e 100644 --- a/test/gzip.jl +++ b/test/gzip.jl @@ -176,7 +176,7 @@ let BUFSIZE = 65536 elseif isa(T, Complex128) r = Int64[rand(BUFSIZE)...] + Int64[rand(BUFSIZE)...] * im else - r = b[randi((1,BUFSIZE), BUFSIZE)]; + r = b[rand(1:BUFSIZE, BUFSIZE)]; end # Array file diff --git a/test/linalg.jl b/test/linalg.jl index d707ee8c85f53..ed9a001796ddf 100644 --- a/test/linalg.jl +++ b/test/linalg.jl @@ -180,8 +180,8 @@ B = [2 -2; 3 -5; -4 7] A = ones(Int, 2, 100) B = ones(Int, 100, 3) @test A*B == [100 100 100; 100 100 100] -A = randi(20, 5, 5) - 10 -B = randi(20, 5, 5) - 10 +A = rand(1:20, 5, 5) - 10 +B = rand(1:20, 5, 5) - 10 @test At_mul_B(A, B) == A'*B @test A_mul_Bt(A, B) == A*B' # Preallocated diff --git a/test/perf/perf.jl b/test/perf/perf.jl index 1b2ad28b7de32..aae5097669e5d 100644 --- a/test/perf/perf.jl +++ b/test/perf/perf.jl @@ -23,7 +23,7 @@ fib(n) = n < 2 ? n : fib(n-1) + fib(n-2) function parseintperf(t) local n, m for i=1:t - n = randi(Uint32) + n = rand(Uint32) s = hex(n) m = uint32(parse_hex(s)) end diff --git a/test/zlib.jl b/test/zlib.jl index 12a7e1c6ca97e..8a86a032fc057 100644 --- a/test/zlib.jl +++ b/test/zlib.jl @@ -17,7 +17,7 @@ for i = 1:length(b) end # Random array -r = b[randi((1,256), BUFSIZE)] +r = b[rand(1:256, BUFSIZE)] ######################## # type size tests