As some of you may already know, the random number generator is not a true random number generator. ALL random numbers are generated via an algorithm. One popular algorithm if called the Linear Congruential Generation algorithm (known as LCG). The LCG is a fast generator, and it has been used for a long time to generate random numbers (one of the oldest generators, in fact). The code below will basically outline the random number generator using the LCG.
However, the script CAN BE CONFUSING! Before you continue, you must know and understand the following concepts:
- Recursion
- Modulus
- Basic mathematical processes (flooring, multiplication, division, etc)
The script may seem huge at first, but once you break it down into its individual parts, it's not hard to understand AT ALL! In fact, almost HALF the script is sending results based on whether the two parameters are actually numbers or not!
Beware, there MAY be a possibility of a c-stack overflow error. I'm not sure how many recursive calls we can get away with using Lua.
Now, to the easy part: explaining the different 'constants' in the script.
'm' is the MODULUS. We will be calling (a*X + c) % m.
- Qualifications: 0 < m
'a' is the MULTIPLIER. We will be multiplying this by each X value in the series.
- Qualifications: 0 < a < m
'b' is the INCREMENT, or STEP. We will add this to our product of a*X.
- Qualifications: 0 <= b < m
X0 is the SEED. The seed is the INITIAL VALUE of the series. Each consecutive value in the series is ultimately based on this seed value.
For more information, you can look up Linear_congruential_generator on Wikipedia.
Series? What's a series?
A series is just a LIST OF NUMBERS. Since we're dealing with a "recursive" series, each and every element of the series is based on the value of the first ELEMENT in the series. We can denote the value of each element of the series as follows:
X(n+1) = (a*Xn + b) % m
Where n = 0, 1, 2, ... k-1, k where k is the index of the individual terms of the series.
If you don't quite understand all that, don't worry. This is high level mathematics, after all.
In the example, we use 'Math.f' as our recursive function to find the value of X(n+1).
Now, enough with the jabbering. Time to post the script!
Math = {
start = os and os.time() or tick();
seed = 0;
numTimes = 0;
m = 1501;
a = 176;
b = 204;
f = function(number)
if number < 1 then return (Math.a*Math.seed + Math.b)%Math.m end
local a, m, b = Math.a, Math.m, Math.b
return (a*Math.f(number-1) + b) % m
end;
random = function( minOrMax, max )
local newNum = Math.f(Math.numTimes)
Math.numTimes = Math.numTimes + 1
if type(max)~="number" then
if type(minOrMax)~="number" then
return newNum/Math.m
else
return math.floor(newNum/Math.m*math.floor(minOrMax))
end
else
if type(minOrMax)=="number" then
return math.floor((newNum/Math.m*(max-minOrMax))+minOrMax)
end
end
return newNum/Math.m
end;
randomseed = function(num)
if type(num) == "number" then
Math.seed = math.floor(num) % Math.m
else
Math.seed = (os and os.time() or os.tick()) % Math.m
end
Math.numTimes = 0;
end;
}
print "------------ NO ARGUMENTS -------------"
for i = 1, 100 do
print( Math.random() )
end
print "----------- ONE ARGUMENT: 10 ------------"
for i = 1, 100 do
print( Math.random(10) )
end
print "----------- TWO ARGUMENTS: -5, 10 ------------"
for i = 1, 100 do
print( Math.random(-5, 10) )
end
print "----------- SETTING NEW SEED -------------"
math.randomseed(100)
print "NEW SEED: 100"
print "------------ NO ARGUMENTS -------------"
for i = 1, 100 do
print( Math.random() )
end
print "----------- ONE ARGUMENT: 10 ------------"
for i = 1, 100 do
print( Math.random(10) )
end
print "----------- TWO ARGUMENTS: -5, 10 ------------"
for i = 1, 100 do
print( Math.random(-5, 10) )
end
OUTPUT FOR A SAMPLE RUN BELOW:
------------ NO ARGUMENTS -------------
0.13590939373751
0.055962691538974
0.98534310459694
0.55629580279813
0.043970686209194
0.87475016655563
0.091938707528314
0.31712191872085
0.94936708860759
0.22451698867422
0.65089940039973
0.69420386409061
0.31578947368421
0.71485676215856
0.95069953364424
0.45902731512325
0.92471685542971
0.88607594936709
0.085276482345103
0.14457028647568
0.58027981345769
0.26515656229181
0.80346435709527
0.545636242505
0.16788807461692
0.68421052631579
0.55696202531646
0.16122584943371
0.51165889407062
0.18787475016656
0.2018654230513
0.66422385076616
0.039307128580946
0.053964023984011
0.63357761492338
0.64556962025316
0.75616255829447
0.22051965356429
0.94736842105263
0.87275149900067
0.74017321785476
0.40639573617588
0.66155896069287
0.57028647568288
0.50632911392405
0.24983344437042
0.10659560293138
0.89673550966023
0.96135909393738
0.33510992671552
0.11525649566955
0.42105263157895
0.24117255163225
0.58227848101266
0.61692205196536
0.71419053964024
0.83344437041972
0.82211858760826
0.82878081279147
0.0013324450366422
0.37041972018654
0.32978014656895
0.17721518987342
0.32578281145903
0.47368421052632
0.50433044636909
0.89806795469687
0.19586942038641
0.6089273817455
0.30712858094604
0.19053964023984
0.67088607594937
0.21185876082612
0.42305129913391
0.5929380413058
0.49300466355763
0.90473017988008
0.36842105263158
0.9780146568954
0.26648900732845
0.037974683544304
0.81945369753498
0.3597601598934
0.45369753497668
0.98667554963358
0.79080612924717
0.31778814123917
0.066622251832112
0.86142571618921
0.74683544303797
0.57894736842105
0.030646235842771
0.52964690206529
0.35376415722851
0.39840106595603
0.25449700199867
0.927381745503
0.35509660226516
0.63291139240506
0.52831445702865
----------- ONE ARGUMENT: 10 ------------
1
1
0
1
9
0
7
2
4
9
6
9
1
0
5
4
0
1
0
9
5
0
8
0
3
9
2
6
6
3
7
9
4
9
8
0
1
5
2
8
5
1
6
5
1
5
1
2
6
0
0
6
6
7
2
9
8
7
4
6
5
5
2
1
8
9
3
1
4
2
5
6
7
8
8
8
0
3
3
1
3
4
5
8
1
6
3
1
6
2
4
5
4
9
3
9
2
0
8
3
----------- TWO ARGUMENTS: -5, 10 ------------
1
9
6
-1
-5
7
6
3
-5
2
0
0
-2
8
0
4
2
-4
-4
-5
-3
8
-5
5
-2
1
9
4
9
-4
-5
3
1
-5
-3
-5
9
3
-5
8
-4
-1
9
-2
4
5
-1
5
9
1
8
8
-4
-3
3
-2
7
3
-3
5
3
-3
2
-3
-2
4
-5
-5
4
4
6
-2
9
8
6
1
4
3
2
-2
-4
8
9
0
-4
1
-2
3
4
5
7
7
7
-5
0
-1
-3
-1
2
2
----------- SETTING NEW SEED -------------
NEW SEED: 100
------------ NO ARGUMENTS -------------
0.89806795469687
0.19586942038641
0.6089273817455
0.30712858094604
0.19053964023984
0.67088607594937
0.21185876082612
0.42305129913391
0.5929380413058
0.49300466355763
0.90473017988008
0.36842105263158
0.9780146568954
0.26648900732845
0.037974683544304
0.81945369753498
0.3597601598934
0.45369753497668
0.98667554963358
0.79080612924717
0.31778814123917
0.066622251832112
0.86142571618921
0.74683544303797
0.57894736842105
0.030646235842771
0.52964690206529
0.35376415722851
0.39840106595603
0.25449700199867
0.927381745503
0.35509660226516
0.63291139240506
0.52831445702865
0.11925383077948
0.12458361092605
0.062624916722185
0.15789473684211
0.92538307794803
0.0033311125916056
0.72218520986009
0.24050632911392
0.46502331778814
0.98001332445037
0.618254497002
0.94870086608927
0.1072618254497
0.013990672884744
0.59826782145237
0.43104596935376
0
0.13590939373751
0.055962691538974
0.98534310459694
0.55629580279813
0.043970686209194
0.87475016655563
0.091938707528314
0.31712191872085
0.94936708860759
0.22451698867422
0.65089940039973
0.69420386409061
0.31578947368421
0.71485676215856
0.95069953364424
0.45902731512325
0.92471685542971
0.88607594936709
0.085276482345103
0.14457028647568
0.58027981345769
0.26515656229181
0.80346435709527
0.545636242505
0.16788807461692
0.68421052631579
0.55696202531646
0.16122584943371
0.51165889407062
0.18787475016656
0.2018654230513
0.66422385076616
0.039307128580946
0.053964023984011
0.63357761492338
0.64556962025316
0.75616255829447
0.22051965356429
0.94736842105263
0.87275149900067
0.74017321785476
0.40639573617588
0.66155896069287
0.57028647568288
0.50632911392405
0.24983344437042
0.10659560293138
0.89673550966023
0.96135909393738
----------- ONE ARGUMENT: 10 ------------
3
1
4
2
5
6
7
8
8
8
0
3
3
1
3
4
5
8
1
6
3
1
6
2
4
5
4
9
3
9
2
0
8
3
4
9
7
3
0
8
7
5
0
5
3
3
2
9
3
6
5
1
1
0
1
9
0
7
2
4
9
6
9
1
0
5
4
0
1
0
9
5
0
8
0
3
9
2
6
6
3
7
9
4
9
8
0
1
5
2
8
5
1
6
5
1
5
1
2
6
----------- TWO ARGUMENTS: -5, 10 ------------
-5
-5
4
4
6
-2
9
8
6
1
4
3
2
-2
-4
8
9
0
-4
1
-2
3
4
5
7
7
7
-5
0
-1
-3
-1
2
2
8
-3
4
-1
-3
5
-2
1
3
2
8
0
9
-2
-5
7
0
1
9
6
-1
-5
7
6
3
-5
2
0
0
-2
8
0
4
2
-4
-4
-5
-3
8
-5
5
-2
1
9
4
9
-4
-5
3
1
-5
-3
-5
9
3
-5
8
-4
-1
9
-2
4
5
-1
5
9
|