-
Notifications
You must be signed in to change notification settings - Fork 79
/
random.jl
60 lines (53 loc) · 1.57 KB
/
random.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
@testsuite "random" (AT, eltypes)->begin
rng = if AT <: AbstractGPUArray
GPUArrays.default_rng(AT)
else
Random.default_rng()
end
cpu_rng = Random.default_rng()
@testset "rand" begin # uniform
for T in eltypes, d in (10, (10,10))
A = AT{T}(undef, d)
B = copy(A)
rand!(rng, A)
rand!(rng, B)
@test Array(A) != Array(B)
Random.seed!(rng)
Random.seed!(rng, 1)
rand!(rng, A)
Random.seed!(rng, 1)
rand!(rng, B)
@test all(Array(A) .== Array(B))
if rng != cpu_rng
rand!(cpu_rng, A)
end
end
A = AT{Bool}(undef, 1024)
fill!(A, false)
rand!(rng, A)
@test true in Array(A)
fill!(A, true)
rand!(rng, A)
@test false in Array(A)
end
@testset "randn" begin # normally-distributed
# XXX: randn calls sqrt, and Base's sqrt(::Complex) performs
# checked type conversions that throw boxed numbers.
for T in filter(isrealfloattype, eltypes), d in (2, (2,2))
A = AT{T}(undef, d)
B = copy(A)
randn!(rng, A)
randn!(rng, B)
@test Array(A) != Array(B)
Random.seed!(rng)
Random.seed!(rng, 1)
randn!(rng, A)
Random.seed!(rng, 1)
randn!(rng, B)
@test Array(A) == Array(B)
if rng != cpu_rng
randn!(cpu_rng, A)
end
end
end
end