Skip to content

Commit bf8c452

Browse files
committed
srand() now seeds both RNGs
1 parent 7ef6733 commit bf8c452

File tree

5 files changed

+33
-15
lines changed

5 files changed

+33
-15
lines changed

base/client.jl

-1
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,6 @@ function _start()
368368
LinAlg.init()
369369
Sys.init()
370370
GMP.gmp_init()
371-
GMP.rand_init()
372371
global const CPU_CORES = Sys.CPU_CORES
373372
init_profiler()
374373

base/gmp.jl

+1-10
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ function randu(randstate::BigRNG, k::BigInt)
483483
(Ptr{BigInt}, Ptr{BigRNG}, Ptr{BigInt}), &z, &randstate, &k)
484484
z
485485
end
486-
randu(k::BigInt) = randu(DEFAULT_BIGRNG, k)
486+
randu(k::BigInt) = randu(Base.Random.DEFAULT_BIGRNG, k)
487487

488488
srand(r::BigRNG, seed) = srand(r, convert(BigInt, seed))
489489
function srand(randstate::BigRNG, seed::Vector{Uint32})
@@ -504,13 +504,4 @@ function srand(randstate::BigRNG, seed::BigInt)
504504
return
505505
end
506506

507-
# Initialize the default BigRNG
508-
function rand_init()
509-
s = big(0)
510-
for i in Base.Random.RANDOM_SEED
511-
s = s << 32 + i
512-
end
513-
global DEFAULT_BIGRNG = BigRNG(s)
514-
end
515-
516507
end # module

base/mpfr.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -747,15 +747,15 @@ function rand(::Type{BigFloat}, randstate::BigRNG)
747747
&z, &randstate, ROUNDING_MODE[end])
748748
z
749749
end
750-
rand(::Type{BigFloat}) = rand(BigFloat, Base.GMP.DEFAULT_BIGRNG)
750+
rand(::Type{BigFloat}) = rand(BigFloat, Base.Random.DEFAULT_BIGRNG)
751751

752752
function rand!(r::BigRNG, A::Array{BigFloat})
753753
for i = 1:length(A)
754754
A[i] = rand(BigFloat, r)
755755
end
756756
A
757757
end
758-
rand!(A::Array{BigFloat}) = rand!(Base.GMP.DEFAULT_BIGRNG, A)
758+
rand!(A::Array{BigFloat}) = rand!(Base.Random.DEFAULT_BIGRNG, A)
759759

760760
function randn(::Type{BigFloat}, randstate::BigRNG)
761761
z = BigFloat()
@@ -764,7 +764,7 @@ function randn(::Type{BigFloat}, randstate::BigRNG)
764764
&z, C_NULL, &randstate, ROUNDING_MODE[end])
765765
z
766766
end
767-
randn(::Type{BigFloat}) = randn(BigFloat, Base.GMP.DEFAULT_BIGRNG)
767+
randn(::Type{BigFloat}) = randn(BigFloat, Base.Random.DEFAULT_BIGRNG)
768768
randn(::Type{BigFloat}, dims::Dims) = randn!(Array(BigFloat, dims))
769769
randn(::Type{BigFloat}, dims::Int...) = randn!(Array(BigFloat, dims...))
770770

@@ -774,6 +774,6 @@ function randn!(r::BigRNG, A::Array{BigFloat})
774774
end
775775
A
776776
end
777-
randn!(A::Array{BigFloat}) = randn!(Base.GMP.DEFAULT_BIGRNG, A)
777+
randn!(A::Array{BigFloat}) = randn!(Base.Random.DEFAULT_BIGRNG, A)
778778

779779
end #module

base/random.jl

+6
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ end
7575
function srand(seed::Vector{Uint32})
7676
global RANDOM_SEED = seed
7777
dsfmt_gv_init_by_array(seed)
78+
s = big(0)
79+
for i in Base.Random.RANDOM_SEED
80+
s = s << 32 + i
81+
end
82+
global DEFAULT_BIGRNG = BigRNG(s)
83+
return
7884
end
7985
srand(n::Integer) = srand(make_seed(n))
8086

test/random.jl

+22
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,25 @@ if sizeof(Int32) < sizeof(Int)
1717
@test typeof(r) == Int32
1818
@test -1 <= r <= typemax(Int32)
1919
end
20+
21+
range = big(typemax(Int128)):(big(typemax(Int128)) + 10000)
22+
@test rand(range) != rand(range)
23+
@test big(typemax(Int128)) <= rand(range) <= big(typemax(Int128)) + 10000
24+
r = rand(range, 1, 2)
25+
@test size(r) == (1, 2)
26+
@test typeof(r) == Matrix{BigInt}
27+
28+
srand(0)
29+
r = rand(range)
30+
f = rand(BigFloat)
31+
srand(0)
32+
@test rand(range) == r
33+
@test rand(BigFloat) == f
34+
35+
@test rand(BigFloat) != rand(BigFloat)
36+
@test 0.0 <= rand(BigFloat) < 1.0
37+
@test typeof(rand(BigFloat)) == BigFloat
38+
39+
r = rand(BigFloat, 1, 3)
40+
@test size(r) == (1, 3)
41+
@test typeof(r) == Matrix{BigFloat}

0 commit comments

Comments
 (0)