//Pseudo Random Number Generators, for Normal, Uniform and Weibull 
//Distribution.
//copyright: Daan G Uitenbroek PhD

function modeldata()  
{  
this.method="MINSTD"  
this.multiplier=16807  
this.adding=0  
this.divider=2147483647   // or Math.pow(2,31)-1  
}  
  
function alldata(length)    
{  
this.length=length  
      for (var j=1;j<=length;j++)  
           this[j]=new modeldata();  
  
this[1].method="MINSTD"  
this[1].multiplier=16807  
this[1].adding=0  
this[1].divider=2147483647  // or Math.pow(2,31)-1  
  
this[2].method="16bit"  
this[2].multiplier=25173  
this[2].adding=13849  
this[2].divider=65536   // or Math.pow(2,16)  
  
this[3].method="APPLE"  
this[3].multiplier=1220703125  
this[3].adding=0  
this[3].divider=Math.pow(2,35)  
  
this[4].method="Super Duper"  
this[4].multiplier=69069  
this[4].adding=0  
this[4].divider=4294967296   // or Math.pow(2,32)  
  
this[5].method="DRAND48"  
this[5].multiplier=25214903917  
this[5].adding=11  
this[5].divider=Math.pow(2,48)  
  
this[6].method="BEWARE!!"  
this[6].multiplier=44485709377909  
this[6].adding=0  
this[6].divider=Math.pow(2,48)  
  
this[7].method="L'Ecuyer"  
this[7].multiplier=0  
this[7].adding=0  
this[7].divider=2147483589   // about Math.pow(2,31)  
  
this[8].method="PaMi"  
this[8].multiplier=0  
this[8].adding=0  
this[8].divider=2147483647  // or Math.pow(2,31)-1  
}  
  
function seed()   //Produces a seed on the basis of milliseconds elapsed since Jan 1, 1970  
{  
var date=new Date();  
randomseed=date.getTime();  
return randomseed;  
}  
  
function changedata(meth)    //Changes the model  
{  
currentoption=meth.selectedIndex+1  
model=allthedata[currentoption]  
}  
  
function LCG(seed,mult,add,cycle)  
   // produces one random number according to the Linear Congruential Method  
{  
var lcg=((mult*seed+add)%cycle)  
return lcg;  
}  
  
function Lecuyer()  
  //produces one random number according to L'Ecuyer Portable Generator  
{  
var k=Math.round(seed1/53668-0.5)  
seed1=40014*(seed1-(k*53668))-(k*12211)  
if (seed1<0) seed1=seed1*1+2147483563*1  
  
var k=Math.round(seed2/52774-0.5)  
seed2=40692*(seed2-(k*52774))-(k*3791)  
if (seed2<0) seed2=seed2*1+2147483399*1  
  
var Z=seed1-seed2;  
if (Z<1) Z=Z*1+2147483562*1  
  
return Z  
}  
  
function pami(seed,m)  
{  
var hi=Math.round(seed/127773)  
var lo=seed%127773  
var test=16807*lo-(2836*hi)  
if (test<1) test=test+m  
return test  
}  
  
function uniform(low,high,numb,givenseed)  
{  
var writeln="\n";  
if (navigator.appVersion.lastIndexOf('Win') != -1)
writeln="\r\n";  
  
if (givenseed=='clock')  
{  
var rdmseed=seed();  
} else   var rdmseed=givenseed;  
  
document.form1.outp.value+="Randomseed eq "+rdmseed+writeln  
                         +"model= "+model.method+"; max= "
                         +model.divider+writeln  
if (model.method=="NAG") document.form1.outp.value+="WARNING! NAG procedure not reliable"+writeln
if (model.method=="BEWARE!!") document.form1.outp.value+="WARNING! anonymous procedure not reliable"+writeln
  
var mult=model.multiplier  
var add=model.adding  
var cycle=model.divider  
  
if (currentoption==numofmeth)
rdmseed=LCG(rdmseed,25173,13849,65536)  
  
if (currentoption!=numofmeth-1)  
for (index=0;index<numb;index++)  
{  
if (currentoption!=numofmeth) rdmseed=LCG(rdmseed,mult,add,cycle)
 
   else rdmseed=pami(rdmseed,cycle)  
outp=rdmseed-1  
if (high!="max")  
  outp=outp/(model.divider-1)*(high-low)+low*1  
document.form1.outp.value+=index+1*1+"\t"+outp+writeln;  
}  
  
if (currentoption==numofmeth-1)  
{  
seed1=LCG(rdmseed,25173,13849,65536)  
seed2=LCG(seed1,25173,13849,65536)  
for (index=0;index<numb;index++)  
{  
rdmseed=Lecuyer()  
outp=rdmseed-1  
if (high!="max")  
  outp=outp/(model.divider-1)*(high-low)+low*1  
document.form1.outp.value+=index+1*1+"\t"+outp+writeln;  
}  
}  
  
document.form1.outp.value+=writeln+"Lastseed eq "
                         +rdmseed+writeln+writeln  
document.form1.seed.value=rdmseed
}  

function normal(low,high,numb,givenseed)
{
var writeln="\n";
if (navigator.appVersion.lastIndexOf('Win') != -1)
writeln="\r\n";

if (givenseed=='clock')
{
var rdmseed=seed();
} else   var rdmseed=givenseed;

document.form1.outp.value+="Randomseed eq "+rdmseed+writeln
                         +"model= "+model.method+"; max= "+model.divider+writeln
if (model.method=="NAG") document.form1.outp.value+="WARNING! NAG procedure not reliable"+writeln
if (model.method=="BEWARE!!") document.form1.outp.value+="WARNING! anonymous procedure not reliable"+writeln

var mult=model.multiplier
var add=model.adding
var cycle=model.divider

if (currentoption==numofmeth)
rdmseed=LCG(rdmseed,25173,13849,65536)

if (currentoption!=numofmeth-1)
for (index=0;index<numb;index++)
{
outp=0
for (jndex=0;jndex<12;jndex++)
     {
if (currentoption!=numofmeth) rdmseed=LCG(rdmseed,mult,add,cycle)
   else rdmseed=pami(rdmseed,cycle)
     outp=outp+rdmseed-(cycle/2)
     }
if (high!="max")
  outp=outp/(model.divider-1)*high+low*1
document.form1.outp.value+=index+1*1+"\t"+outp+writeln;
}

if (currentoption==numofmeth-1)
{
seed1=LCG(rdmseed,25173,13849,65536)
seed2=LCG(seed1,25173,13849,65536)
for (index=0;index<numb;index++)
{
outp=0
for (jndex=0;jndex<12;jndex++)
     {
     rdmseed=Lecuyer()
     outp=outp*1+rdmseed*1-(cycle/2)
     }
if (high!="max")
  outp=outp/(model.divider-1)*high+low*1
document.form1.outp.value+=index+1*1+"\t"+outp+writeln;
}
}

document.form1.outp.value+=writeln+"Lastseed eq "+rdmseed
                         +writeln+writeln
}

function odd(number)    
{    
var isodd=0*1;    
if (Math.round(number/2+0.00001)==number/2) isodd=1*1;  
return isodd    
}    
  
function normal_not_used(low,high,numb,givenseed)  
//this is the non polar version of Box and Mullers transformation
{  
var writeln="\n";  
if (navigator.appVersion.lastIndexOf('Win') != -1)
writeln="\r\n";  
  
if (givenseed=='clock')  
{  
var rdmseed=seed();  
} else   var rdmseed=givenseed;  
  
document.form1.outp.value+="Randomseed eq "+rdmseed+writeln  
                         +"model= "+model.method+"; max= "+model.divider+writeln  
if (model.method=="NAG") document.form1.outp.value+="WARNING! NAG procedure not reliable"+writeln
if (model.method=="BEWARE!!") document.form1.outp.value+="WARNING! anonymous procedure not reliable"+writeln
  
var mult=model.multiplier  
var add=model.adding  
var cycle=model.divider  
  
if (currentoption==numofmeth)
rdmseed=LCG(rdmseed,25173,13849,65536)
var rdms=rdmseed 

if (currentoption!=numofmeth-1)  
for (index=0;index<numb;index++)  
{  
if (currentoption!=numofmeth) rdmseed=LCG(rdms,mult,add,cycle)
   else rdmseed=pami(randseed,cycle)  
if (currentoption!=numofmeth) rdms=LCG(rdmseed,mult,add,cycle)
   else rdms=pami(rdmseed,cycle)  
outp1=(rdmseed)/(model.divider)
outp2=(rdms)/(model.divider)
outp=Math.sqrt(-2*Math.log(outp1))*Math.sin(2*Math.PI*outp2)
outp=outp*high+low*1  
document.form1.outp.value+=index+1*1+"\t"+outp+writeln;  
}  

document.form1.outp.value+=writeln+"Lastseed eq "
                         +rdmseed+writeln+writeln  
}  

function normal2(low,high,numb,givenseed) 
//this is the polar improvement of Box and Mullers algoritm
//the improvement is by Ross (1988)
{  
var writeln="\n";  
if (navigator.appVersion.lastIndexOf('Win') != -1)
writeln="\r\n";  
  
if (givenseed=='clock')  
{  
var rdmseed=seed();  
} else   var rdmseed=givenseed;  
  
document.form1.outp.value+="Randomseed eq "+rdmseed+writeln  
                         +"model= "+model.method+"; max= "
                         +model.divider+writeln  
if (model.method=="NAG") document.form1.outp.value+="WARNING! NAG procedure not reliable"+writeln
if (model.method=="BEWARE!!") document.form1.outp.value+="WARNING! anonymous procedure not reliable"+writeln
  
var mult=model.multiplier  
var add=model.adding  
var cycle=model.divider  

if (high=="max") high=cycle;
  
if (currentoption==numofmeth)
rdmseed=LCG(rdmseed,25173,13849,65536)
var rdms=rdmseed 

if (currentoption!=numofmeth-1)  
   //draw normal random numbers according to the polar method
for (index=0;index<numb;index++)  
{
var w=10;  
for (jndex=0;w>1.0;jndex++)  
{
if (currentoption!=numofmeth) rdmseed=LCG(rdms,mult,add,cycle)
   else rdmseed=pami(rdms,cycle)  
if (currentoption!=numofmeth) rdms=LCG(rdmseed,mult,add,cycle)
   else rdms=pami(rdmseed,cycle)  
outp1=((rdmseed)/(model.divider))*2-1.0;
outp2=((rdms)/(model.divider))*2-1.0;
w=(outp1*outp1)+(outp2*outp2);
}
w=Math.sqrt((-2.0*Math.log(w))/w);
outp=outp1*w;
outp=outp*high+low*1  
document.form1.outp.value+=index+1*1+"\t"+outp+writeln;  
}  
  
if (currentoption==numofmeth-1)  
{  
seed1=LCG(rdmseed,25173,13849,65536)  
seed2=LCG(seed1,25173,13849,65536)  
for (index=0;index<numb;index++)  
{  
rdmseed=Lecuyer()  
outp=rdmseed-1  
var w=10;  
for (jndex=0;w>1.0;jndex++)  
{
rdmseed=Lecuyer()  
rdms=Lecuyer()  
outp1=((rdmseed)/(model.divider))*2-1.0;
outp2=((rdms)/(model.divider))*2-1.0;
w=(outp1*outp1)+(outp2*outp2);
}
w=Math.sqrt((-2.0*Math.log(w))/w);
outp=outp1*w;
outp=outp*high+low*1  
document.form1.outp.value+=index+1*1+"\t"+outp+writeln;  
}  
} 

document.form1.outp.value+=writeln+"Lastseed eq "
                         +rdmseed+writeln+writeln  
}  

function weibull(low,high,numb,givenseed)  
{  
var writeln="\n";  
if (navigator.appVersion.lastIndexOf('Win') != -1)
writeln="\r\n";  
  
if (givenseed=='clock')  
var rdmseed=seed()
 else   var rdmseed=givenseed;  
  
document.form1.outp.value+="Randomseed eq "+rdmseed+writeln  
                         +"model= "+model.method+"; max= "
                         +model.divider+writeln  
if (model.method=="NAG") document.form1.outp.value+="WARNING! NAG procedure not reliable"+writeln
if (model.method=="BEWARE!!") document.form1.outp.value+="WARNING! anonymous procedure not reliable"+writeln
  
var mult=model.multiplier  
var add=model.adding  
var cycle=model.divider  

if (high=="max") high=cycle;
  
if (currentoption==numofmeth)
rdmseed=LCG(rdmseed,25173,13849,65536)  

if (low==0) 
 document.form1.outp.value+="WARNING. Scale should not be zero"+writeln;  
  
if (currentoption!=numofmeth-1)  
for (index=0;index<numb;index++)  
{  
if (currentoption!=numofmeth) rdmseed=LCG(rdmseed,mult,add,cycle)
    else rdmseed=pami(rdmseed,cycle)  
outp=(rdmseed-1)/(model.divider-1)
outp=low*Math.pow((-1*Math.log(outp)),(1/high))
//if (high!="max")  //low=shape
//  outp=outp/(model.divider-1)*(high-low)+low*1  
document.form1.outp.value+=index+1*1+"\t"+outp+writeln;  
}  
  
if (currentoption==numofmeth-1)  
{  
seed1=LCG(rdmseed,25173,13849,65536)  
seed2=LCG(seed1,25173,13849,65536)  
for (index=0;index<numb;index++)  
{  
rdmseed=Lecuyer()  
outp=(rdmseed-1)/(model.divider-1)
outp=low*Math.pow((-1*Math.log(outp)),(1/high))
document.form1.outp.value+=index+1*1+"\t"+outp+writeln;  
}  
}  
document.form1.outp.value+=writeln+"Lastseed eq "
                         +rdmseed+writeln+writeln  
}  

function gamma(scale,shape,numb,givenseed)  //***********************************
{  
var writeln="\n";  
if (navigator.appVersion.lastIndexOf('Win') != -1)
writeln="\r\n";  
  
if (givenseed=='clock')  
var rdmseed=seed()
 else   var rdmseed=givenseed;  
  
document.form1.outp.value+="Randomseed eq "+rdmseed+writeln  
                         +"model= "+model.method+"; max= "
                         +model.divider+writeln  
if (model.method=="NAG") document.form1.outp.value+="WARNING! NAG procedure not reliable"+writeln
if (model.method=="BEWARE!!") document.form1.outp.value+="WARNING! anonymous procedure not reliable"+writeln
  
var mult=model.multiplier  
var add=model.adding  
var cycle=model.divider  

if (shape=="max") shape=cycle;
  
if (currentoption==numofmeth)
rdmseed=LCG(rdmseed,25173,13849,65536)  

if (scale==0) 
 document.form1.outp.value+="WARNING. Scale should not be zero"+writeln;  
  
if (currentoption!=numofmeth-1)  
for (index=0;index<numb;index++)  
{
lambda=0;
for (jndex=1;jndex<shape*1+1*1;jndex++)
{  
if (currentoption!=numofmeth) rdmseed=LCG(rdmseed,mult,add,cycle)
    else rdmseed=pami(rdmseed,cycle)  
lambda+=scale*-1*Math.log((rdmseed-1)/(model.divider-1))
}
document.form1.outp.value+=index+1*1+"\t"+lambda+writeln;  
}  
  
if (currentoption==numofmeth-1)  
{  
seed1=LCG(rdmseed,25173,13849,65536)  
seed2=LCG(seed1,25173,13849,65536)  
for (index=0;index<numb;index++)  
{  
lambda=0;
for (jndex=1;jndex<shape*1+1*1;jndex++)
{
rdmseed=Lecuyer()  
lambda+=scale*-1*Math.log((rdmseed-1)/(model.divider-1))
}
document.form1.outp.value+=index+1*1+"\t"+lambda+writeln;  
}  
}  
document.form1.outp.value+=writeln+"Lastseed eq "
                         +rdmseed+writeln+writeln  
}  

function beta(scale,shape,numb,givenseed)  //***********************************
{  
var writeln="\n";  
if (navigator.appVersion.lastIndexOf('Win') != -1)
writeln="\r\n";  
  
if (givenseed=='clock')  
var rdmseed=seed()
 else   var rdmseed=givenseed;  
  
document.form1.outp.value+="Randomseed eq "+rdmseed+writeln  
                         +"model= "+model.method+"; max= "
                         +model.divider+writeln  
  
var mult=model.multiplier  
var add=model.adding  
var cycle=model.divider  

if (shape=="max") shape=cycle;
  
if (currentoption==numofmeth)
rdmseed=LCG(rdmseed,25173,13849,65536)  

if (scale==0) 
 document.form1.outp.value+="WARNING. Scale should not be zero"+writeln;  
  
if (currentoption!=numofmeth-1)  
for (index=0;index<numb;index++)  
{
lambdaW=0;
for (jndex=1;jndex<shape*1+1*1;jndex++)
{  
if (currentoption!=numofmeth) rdmseed=LCG(rdmseed,mult,add,cycle)
    else rdmseed=pami(rdmseed,cycle)  
lambdaW+=Math.log((rdmseed-1)/(model.divider-1))
}
lambdaV=0;
for (jndex=1;jndex<scale*1+1*1;jndex++)
{  
if (currentoption!=numofmeth) rdmseed=LCG(rdmseed,mult,add,cycle)
    else rdmseed=pami(rdmseed,cycle)  
lambdaV+=Math.log((rdmseed-1)/(model.divider-1))
}
var lambda=lambdaW/(lambdaV*1+lambdaW*1)
document.form1.outp.value+=index+1*1+"\t"+lambda+writeln;  
}  
  
if (currentoption==numofmeth-1)  
{  
seed1=LCG(rdmseed,25173,13849,65536)  
seed2=LCG(seed1,25173,13849,65536)  
for (index=0;index<numb;index++)  
{
lambdaW=0;
for (jndex=1;jndex<shape*1+1*1;jndex++)
{  
rdmseed=Lecuyer()
lambdaW+=Math.log((rdmseed-1)/(model.divider-1))
}
lambdaV=0;
for (jndex=1;jndex<scale*1+1*1;jndex++)
{  
rdmseed=Lecuyer()
lambdaV+=Math.log((rdmseed-1)/(model.divider-1))
}
var lambda=lambdaW/(lambdaV*1+lambdaW*1)
document.form1.outp.value+=index+1*1+"\t"+lambda+writeln;  
}
}  
document.form1.outp.value+=writeln+"Lastseed eq "
                         +rdmseed+writeln+writeln  
}  

function ClearOutp()  
{  
var writeln="\n";  
if (navigator.appVersion.lastIndexOf('Win') != -1)
writeln="\r\n";  
document.form1.outp.value="";  
}  
  
// following are innitialization statements  
currentoption=1  
model=new modeldata()  
numofmeth=8 
//the number should be equal to the number of options in the root html form 
allthedata=new alldata(numofmeth)

