Random Number Generator 1.1

This bundle is marked as pending. It has not been reviewed by a staff member yet.
    This is a linear congruential generator in vJass. You can have different setups and seeds in tandem.
    This wiki article contains a good list of numbers to plug in:
    These kind of generators can generate psuedo random numbers without repeating until it reaches the period.
    The period is just a term used to denote the number where the generator will reset and patterns can emerge if
    this period is too small.
    The maximum a period can be is based off of the .mod parameter. The period can never exceed the .mod, but it can be less
    than the .mod if you have bad parameters - hence the warning below.
    Combining two generators can increase the period by an order of magnitude of 9, meaning you can have really, really long periods
    without patterns emerging. (Like 5,290,000,000,000,000,000 long, as seen in the example. This may seem impossible in 32 bit, but
    there is some seriously math cool going on here)
    This can be useful for a number of things, for example:
        1. Creating seed based foliage
        2. Proceedural generation
        3. Using it like a weird noise algorithm https://en.wikipedia.org/wiki/RANDU
        4. Async usage - just be careful of thread limiting locally
        5. Random number usage without affecting vanilla gameplay RNG
    WARNING: Plugging in random numbers will give terrible results! Refer to the numbers list in the wiki at the top for
    consistent RNG.
        1. Create (seed, multiplier, modulus, increment) - returns your new RNG table
        2. Random - returns a number between 0 and 1
        3. Real (low bound range, high bound range) - returns within the range provided
        4. Int - same as above
        1. RandomC (RNG 1, RNG 2) - combines two RNGs and returns a number between 0 and 1
        2. RandomRealC( RNG 1, RNG 2, low bound range, high bound range) - returns within the range provided
        3. RandomIntC - same as above
library RandomNumberGenerator
    struct RNG
        integer seed
        integer mult
        integer mod
        integer inc
        static method create takes integer newSeed, integer newMult, integer newMod, integer newInc returns RNG
            local RNG this = RNG.allocate()
            set .seed = newSeed
            set .mult = newMult
            set .mod = newMod
            set .inc = newInc
            return this
        method push takes nothing returns integer
            local integer d = .seed * .mult + .inc
            local integer m = d - (d / .mod) * .mod
            if m < 0 then
                set m = m + .mod
            set .seed = m
            return 0
        method Random takes nothing returns real
            return .push() + I2R(.seed) / .mod
        method Real takes real lowBound, real highBound returns real
            return .Random() * (highBound - lowBound) + lowBound
        method Int takes integer lowBound, integer highBound returns integer
            return MathRound(.Random() * (highBound - lowBound) + lowBound)
    //Combined RNG
    function RandomC takes RNG rng1, RNG rng2 returns real
        local integer d = rng1.push() + rng2.push() + rng1.seed - rng2.seed
        local integer m = IMaxBJ(rng1.mod,rng2.mod)
        local integer r = d - (d / m) * m
        if r > 0 then
            return I2R(r) / m
        elseif r < 0 then
            return I2R(r) / m + 1
            return I2R(m - 1) / m
    function RandomRealC takes RNG rng1, RNG rng2, real lowBound, real highBound returns real
        return RandomC(rng1,rng2) * (highBound - lowBound) + lowBound
    function RandomIntC takes RNG rng1, RNG rng2, integer lowBound, integer highBound returns integer
        return MathRound(RandomC(rng1,rng2) * (highBound - lowBound) + lowBound)

Just another Warcraft III map (Map)

Level 22
Jan 1, 2011
I fail to see why one wouldn’t just use the built-in GetRandomInt and GetRandomReal. If JASS didn’t already have those functions, then this resource would have a purpose.
Because it is difficult to maintain seed consistency. If you have combat during dungeon generation for example, the layout will be slightly different because the internal seed gets pushed. This allows complete control over the seed without effecting other areas of gameplay
Level 38
Sep 26, 2009
I'm well aware. But like I said, anything that pushes the seed forward will interrupt whatever sequence you are in. Setting the seed before every GetRandom call would be a hassle, not only that but it allows players to use different inputs to get variations in RNG
Thanks, these points are worth including in your original post. Plain language that people unfamiliar with why you created this resource can understand and see how it could be useful.
Level 22
Jan 1, 2011
Is this going to be immensely more useful than GetRandomInt for a triggered Crit/Evasion ability or All Random hero pick system? And do I have to create my own RNG table before calling the generation function?
1. No, it is probably just about as equally "random" as the default RNG the engine uses. It may even have a smaller period because the engine is likely using a 64 bit PRNG. Except if you are using the combined RNG functions provided, those will almost certainly have a larger period. But it doesn't really matter for most use cases. Once you get a period in excess of like a billion, anything more is just masturbatory.

2. Yes, refer to the example for how to do that.