# Random Number Generator 1.1

This bundle is marked as pending. It has not been reviewed by a staff member yet.
JASS:
``````/*
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:
https://en.wikipedia.org/wiki/Linear_congruential_generator#cite_note-Steele20-2

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.

Methods:
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

Combined
Functions:
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
endmethod

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
endif
set .seed = m
return 0
endmethod

method Random takes nothing returns real
return .push() + I2R(.seed) / .mod
endmethod

method Real takes real lowBound, real highBound returns real
return .Random() * (highBound - lowBound) + lowBound
endmethod

method Int takes integer lowBound, integer highBound returns integer
return MathRound(.Random() * (highBound - lowBound) + lowBound)
endmethod
endstruct

//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
else
return I2R(m - 1) / m
endif
endfunction

function RandomRealC takes RNG rng1, RNG rng2, real lowBound, real highBound returns real
return RandomC(rng1,rng2) * (highBound - lowBound) + lowBound
endfunction

function RandomIntC takes RNG rng1, RNG rng2, integer lowBound, integer highBound returns integer
return MathRound(RandomC(rng1,rng2) * (highBound - lowBound) + lowBound)
endfunction
endlibrary``````
Contents

# Just another Warcraft III map (Map)

#### Bribe

Level 38
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.

Level 22
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

#### Bribe

Level 38
`native SetRandomSeed takes integer seed returns nothing` ?

Level 22
`native SetRandomSeed takes integer seed returns nothing` ?
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

#### Bribe

Level 38
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
Update:
• Added two more example RNG parameters
• Added combined RNG as a feature
• Minor tweaks

#### Spacebuns

Level 16
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?

Level 22
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.

Replies
13
Views
9K
Replies
21
Views
12K
Replies
3
Views
172
Replies
3
Views
2K
Replies
22
Views
23K