Skip to content

Commit

Permalink
Fix rand() and replace the use of randi.
Browse files Browse the repository at this point in the history
  • Loading branch information
ViralBShah committed Jan 22, 2013
1 parent 335c9bd commit 73f5da9
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 62 deletions.
4 changes: 2 additions & 2 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions base/combinatorics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
6 changes: 0 additions & 6 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -946,18 +946,12 @@ export
randn,
RNG,
# deprecated stuff
randbeta!,
randbeta,
randchi2!,
randchi2,
randexp!,
randexp,
randg!,
randg,
randg2,
randi!,
randi,
randival!,
randival,

# statistics
Expand Down
2 changes: 1 addition & 1 deletion base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 2 additions & 9 deletions base/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ 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!,
AbstractRNG, RNG, MersenneTwister

Expand Down Expand Up @@ -134,15 +129,13 @@ rand(::Type{Int32}) = int32(rand(Uint32)) & typemax(Int32)
rand(::Type{Int64}) = int64(rand(Uint64)) & typemax(Int64)
rand(::Type{Int128}) = int128(rand(Uint128)) & typemax(Int128)

rand() = rand(Int)

# random integer from lo to hi inclusive
function rand{T<:Integer}(r::Range1{T})
lo = r[1]
hi = r[end]

m = typemax(T)
s = randi(T)
s = rand(T)
if (hi-lo == m)
return s + lo
end
Expand All @@ -154,7 +147,7 @@ function rand{T<:Integer}(r::Range1{T})
# note: m>=0 && r>=0
lim = m - rem(rem(m,r)+1, r)
while s > lim
s = randi(T)
s = rand(T)
end
return rem(s,r) + lo
end
Expand Down
4 changes: 2 additions & 2 deletions base/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
36 changes: 18 additions & 18 deletions test/arrayperf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,25 @@ 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
println("Big arrays:")
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
Expand All @@ -67,25 +67,25 @@ 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
println("Big arrays:")
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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
22 changes: 11 additions & 11 deletions test/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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] ])
Expand Down
2 changes: 1 addition & 1 deletion test/combinatorics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 4 additions & 4 deletions test/corelib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/gzip.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 73f5da9

Please sign in to comment.