function randtest(n::Int)
r = rand(n)
for i=1:n
for j=(i+1):n
if r[i] == r[j]
println("Random match found at ", i, ",", j)
return(true)
end
end
end
return false
end
Version 0.3.0-prerelease+2519 (2014-04-07 02:50 UTC) Commit 11972c1 (11 days old master) (linux)
Version 0.3.0-prerelease+1561 (2014-02-13 16:38 UTC) Commit d15ce97* (63 days old master) (macos)
Cheers,
---david
julia> srand(0)
julia> [randtest(383) for _=1:10]' #'
Random match found at 2,383
Random match found at 3,383
Random match found at 4,383
Random match found at 5,383
Random match found at 6,383
Random match found at 7,383
Random match found at 8,383
Random match found at 9,383
Random match found at 10,383
1x10 Array{Bool,2}:
false true true true true true true true true true
julia> srand(1)
julia> [randtest(383) for _=1:10]' #'
Random match found at 2,383
Random match found at 3,383
Random match found at 4,383
Random match found at 5,383
Random match found at 6,383
Random match found at 7,383
Random match found at 8,383
Random match found at 9,383
Random match found at 10,383
1x10 Array{Bool,2}:
false true true true true true true true true true
julia> srand(rand(Uint64,1)[1])
julia> [randtest(383) for _=1:10]' #'
Random match found at 2,383
Random match found at 3,383
Random match found at 4,383
Random match found at 5,383
Random match found at 6,383
Random match found at 7,383
Random match found at 8,383
Random match found at 9,383
Random match found at 10,383
1x10 Array{Bool,2}:
false true true true true true true true true true
# John's test behaves similarly:
julia> srand(0)
julia> x = rand(383); length(unique(x)) == length(x) # First invocation after seeding is oktrue
julia> x = rand(383); length(unique(x)) == length(x) # Subsequent ones have duplicate numbersfalseJulia Version 0.3.0-prerelease+2658
Commit 752dc20 (2014-04-18 11:35 UTC)
Platform Info:
System: Darwin (x86_64-apple-darwin13.1.0)
CPU: Intel(R) Core(TM) i5 CPU M 520 @ 2.40GHz
WORD_SIZE: 64
BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY)
LAPACK: libopenblas
LIBM: libopenlibmjulia> f(from,to) = [(srand(0); rand(n); length(unique(rand(n))) != n) for n in from:to]f (generic function with 1 method)
julia> find(f(1,400))9-element Array{Int64,1}: 383 385 387 389 391 393 395 397 399
julia> find(f(1_000_001,1_000_010))5-element Array{Int64,1}: 1 3 5 7 9julia> srand(0); rand(383);
julia> x = rand(383);
julia> find(x .== rand())1-element Array{Int64,1}: 3
julia> find(x .== rand())1-element Array{Int64,1}: 4
julia> find(x .== rand())1-element Array{Int64,1}: 5Well, 383 makes sense. for N <= 382 (which is Base.LibRandom.dsfmt_min_array_size), the array is iteratively filled by single rand() invocations. And odd-length-only makes sense, as for N > 382, the array is filled two Float64s at a time, with the last element getting set by a single rand() call. I that last rand() call is assuming that the state of the RNG is unchanged by the preceding call… and as such is *overwriting* the state instead of incrementing it. Or there's a similar kind of race condition. That explains why the seed is irrelevant. librandom.jl:120