My new random number generator

Please note I’m not a professional rng designer so this might not be up to the standard, but feel free to check it out. It passes the smallcrush test.

#=
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <https://unlicense.org>
=#
using Random

const Multiplier = UInt32(1664525)
const Adder = UInt32(1013904223)


mutable struct myRNG<:Random.AbstractRNG
    mainstate::UInt32 
    a::UInt32
    b::UInt32
    c::UInt32
    d::UInt32

    function myRNG(seed::Integer, a::Integer = 0xa496d730, b::Integer = 0x03a67336, c::Integer = 0x2b43593c, d::Integer = 0xbded99a9)
        return new(seed,a,b,c,d)
    end
end


function Base.copy(rng::myRNG)
    return myRNG(rng.mainstate,rng.a,rng.b,rng.c,rng.d)
end




function Base.rand(rng::myRNG,::Type{UInt32})
    rng.mainstate = rng.mainstate*Multiplier + Adder
    x = rng.a + ((rng.b & rng.c) | (~rng.b & rng.d)) + rng.mainstate
    
    rng.a = rng.d
    rng.d = rng.c
    rng.c = bitrotate(rng.b,15)
    rng.b = bitrotate(x,7)+rng.b

    return rng.d
end

function Base.rand(rng::myRNG,::Random.SamplerType{UInt32})
    return rand(rng,UInt32)
end

Update: It passes bigcrush too.

You should reorder

    rng.mainstate = rng.mainstate*Multiplier + Adder
    x = rng.a + ((rng.b & rng.c) | (~rng.b & rng.d)) + rng.mainstate

into

    x = rng.a + ((rng.b & rng.c) | (~rng.b & rng.d)) + rng.mainstate
    rng.mainstate = rng.mainstate*Multiplier + Adder

The evolution of rng.mainstate is a LFSR that drives the remaining circuit of 4 state variables; switching these lines simply phase-shifts the LFSR by one, so this doesn’t meaningfully affect output. But it hides the latency of the multiply from the remaining operations.

That being said, you should explain what distinguishes your construction from standard ones: What’s the potential upside of this? Why is it designed the way it is designed?

3 Likes

Maybe that slightly speeds up the rng?

This design is quite statistically strong while being quite fast. It’s almost as fast as the base random number generator (1.4 ms vs 1.0 ms for 1000000 numbers on my machine.)