Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test/random.jl: segregate deterministic tests at the end #8339

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions test/random.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
# Issue #6573
srand(0); rand(); x = rand(384);
@test find(x .== rand()) == []
## Non-deterministic tests
# deterministic tests (using srand) should go at the end of the file

using Base.Random.RANDOM_SEED

srand()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something like?

srand() #Reseed the RNG with entropy from the OS
seed = rand(Uint)
#seed = 
println("Running random tests with seed\nseed = 0x",hex(seed))
srand(seed)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why you would like seeding with rand(Uint)... is it so to be able to print only an Uint instead of RANDOM_SEED which is an array of four Uint32 ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was for convenience and shorter printing. If you think there are any benefit in using RANDOM_SEED directly, please do that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would then prefer to print RANDOM_SEED as an Uint128. So do you suggest I update this PR to print to the screen instead of to the file?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, for Travis and bug reports, it seems better to print to STDOUT. Uint128 seems like a good idea.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

# bugs (failed tests) are still reproducible, provided that each test calls
# `rand` a deterministic (once global RNG state is known) number of times
# (e.g. not based on `time()`)
let seed = rand(Uint128)
# change seed to the printed value for reproducing (failed) tests
srand(seed)
println("Running random tests with seed: 0x", hex(seed))
end

@test rand() != rand()
@test 0.0 <= rand() < 1.0
Expand Down Expand Up @@ -141,25 +152,25 @@ randmtzig_fill_ziggurat_tables()
@test all(we == Base.Random.we)
@test all(fe == Base.Random.fe)

#same random numbers on for small ranges on all systems

seed = rand(Uint) #leave state nondeterministic as above
srand(seed)
# same random numbers for small ranges on all systems

srand(RANDOM_SEED)
r = int64(rand(int32(97:122)))
srand(seed)
srand(RANDOM_SEED)
@test r == rand(int64(97:122))

srand(seed)
srand(RANDOM_SEED)
r = uint64(rand(uint32(97:122)))
srand(seed)
srand(RANDOM_SEED)
@test r == rand(uint64(97:122))

@test all([div(0x000100000000,k)*k - 1 == Base.Random.RandIntGen(uint64(1:k)).u for k in 13 .+ int64(2).^(1:30)])
@test all([div(0x000100000000,k)*k - 1 == Base.Random.RandIntGen(int64(1:k)).u for k in 13 .+ int64(2).^(1:30)])

import Base.Random: uuid4, UUID

# UUID
using Base.Random: uuid4, UUID

a = uuid4()
@test a == UUID(string(a)) == UUID(utf16(string(a))) == UUID(utf32(string(a)))
@test_throws ArgumentError UUID("550e8400e29b-41d4-a716-446655440000")
Expand All @@ -171,3 +182,9 @@ i8257 = 1:1/3:100
for i = 1:100
@test rand(i8257) in i8257
end

## Deterministic tests

# Issue #6573
srand(0); rand(); x = rand(384);
@test find(x .== rand()) == []