Repository: JuliaLang/julia
File: stdlib/Random/src/RNGs.jl (Julia)

Code before:
    isequal(r1.vals, r2.vals) &&
    isequal(r1.ints, r2.ints) &&
    r1.idxF == r2.idxF && r1.idxI == r2.idxI

hash(r::MersenneTwister, h::UInt) =
    foldr(hash, (r.seed, r.state, r.vals, r.ints, r.idxF, r.idxI); init=h)

function show(io::IO, rng::MersenneTwister)
    # seed
    if rng.adv_jump == 0 && rng.adv == 0
        return print(io, MersenneTwister, "(", repr(rng.seed), ")")
    end
    print(io, MersenneTwister, "(", repr(rng.seed), ", (")
    # state
    sep = ", "
    show(io, rng.adv_jump)
    print(io, sep)
    show(io, rng.adv)
    if rng.adv_vals != -1 || rng.adv_ints != -1
        print(io, sep)
        if rng.adv_vals == -1
            @assert rng.idxF == MT_CACHE_F
            # "(0, 0)" is nicer on the eyes than (-1, 1002)
            print(io, '0')
            print(io, sep)
            print(io, '0')
        else
            show(io, rng.adv_vals)
            print(io, sep)
            show(io, rng.idxF)
        end
    end
    if rng.adv_ints != -1
        idxI = (length(rng.ints)*16 - rng.idxI) / 8 # 8 represents one Int64
        idxI = Int(idxI) # idxI should always be an integer when using public APIs
        print(io, sep)
        show(io, rng.adv_ints)
        print(io, sep)
        show(io, idxI)
    end
    print(io, "))")
end

### low level API

function reset_caches!(r::MersenneTwister)
    # zeroing the caches makes comparing two MersenneTwister RNGs easier
    fill!(r.vals, 0.0)
    fill!(r.ints, zero(UInt128))
    mt_setempty!(r)
    mt_setempty!(r, UInt128)

Review comment (suggestion):
Github won't let me highlight the whole `if`, but hopefully the idea is clear:
```suggestion
            # "(0, 0)" is nicer on the eyes than (-1, 1002)
            (a, b) = (rng.adv_vals != -1) ? (rng.adv_vals, rng.idxF) : (Int64(0), 0)
            print(io, a)
            print(io, sep)
            print(io, b)
```

Code after:
    isequal(r1.vals, r2.vals) &&
    isequal(r1.ints, r2.ints) &&
    r1.idxF == r2.idxF && r1.idxI == r2.idxI

hash(r::MersenneTwister, h::UInt) =
    foldr(hash, (r.seed, r.state, r.vals, r.ints, r.idxF, r.idxI); init=h)

function show(io::IO, rng::MersenneTwister)
    # seed
    if rng.adv_jump == 0 && rng.adv == 0
        return print(io, MersenneTwister, "(", repr(rng.seed), ")")
    end
    print(io, MersenneTwister, "(", repr(rng.seed), ", (")
    # state
    sep = ", "
    show(io, rng.adv_jump)
    print(io, sep)
    show(io, rng.adv)
    if rng.adv_vals != -1 || rng.adv_ints != -1
        # "(0, 0)" is nicer on the eyes than (-1, 1002)
        s = rng.adv_vals != -1
        print(io, sep)
        show(io, s ? rng.adv_vals : zero(rng.adv_vals))
        print(io, sep)
        show(io, s ? rng.idxF : zero(rng.idxF))
    end
    if rng.adv_ints != -1
        idxI = (length(rng.ints)*16 - rng.idxI) / 8 # 8 represents one Int64
        idxI = Int(idxI) # idxI should always be an integer when using public APIs
        print(io, sep)
        show(io, rng.adv_ints)
        print(io, sep)
        show(io, idxI)
    end
    print(io, "))")
end

### low level API

function reset_caches!(r::MersenneTwister)
    # zeroing the caches makes comparing two MersenneTwister RNGs easier
    fill!(r.vals, 0.0)
    fill!(r.ints, zero(UInt128))
    mt_setempty!(r)
    mt_setempty!(r, UInt128)
    r.adv_vals = -1
    r.adv_ints = -1
    r
end

#### floats