/*generates random integer between 0-4 INCLUSIVE*/
function rand4(){
return Math.random() * 5 | 0;
}
/*generates random integer between 1-5 INCLUSIVE*/
function rand1_5(){
return rand4()+1;
}
/*converts a integer generator to a float range*/
function toFloat(f,b,p){
b = b || 2;
p = p || 3;
return (Array.apply(null,Array(p))
.map(function(d,i){return f()})
.map(function(d,i){return Math.pow(b,i)*d})
.reduce(function(ac,d,i){return ac += d;}))
/
(
(Math.pow(b,p) - 1)
/(b-1)
)
}
/*draws a histogram*/
function draw(data,theID){
(new lexiconDistribute.constructor)
.zoom(10000)
.container("body")
.colorControllers(["DarkSlateGray","DarkSlateGray","AntiqueWhite","DarkSlateGray","Green","Green","DarkSlateGray"])
.submitText("base (b) is" + (theID + 2))
.w(600)
.h(600)
.sW("400px")
.sH("400px")
.barColors(["RoyalBlue","Orange"])
.barHighlights(["White","Blue"])
.position("relative")
.sTop("0px")
.sLeft("0%")
.sMargin("0px auto 0px auto")
.color("Green")
.opacity(0.1)
.offSet(0)
.fontConstant(1.25)
.values(function factory(_data_,dataset){
return dataset;
},
data,
0
)
.lexID("dist_"+theID)
.labelAxes(true,true)
.append()
.render();
}
function addTitle(title){
var el = document.createElement("h2");
el.textContent = title;
document.body.appendChild(el);
}
/*Generate 7 histograms that plot values of randomly generated integer values between 0-4 by rand4 that are convertd to FLOAT by toFloat Function, the base (b) is incremented starting from 2 to 8*/
Array.apply(null,Array(7)).map(function(d,i){
addTitle("d = " + (i + 2));
var dataset = Array.apply(null,Array(6000))
.map(function(dd,ii){return (toFloat(rand4,i+2,6)/4*6)});
draw(dataset,i);
});
/*Draw native Math.random*/
addTitle("default Math.random");
draw(Array.apply(null,Array(3000)).map(function(d,i){return Math.random();}),"random");
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.