Expand INTEGER RANGE 1-5 to 1-7

Expand INTEGER RANGE 1-5 to 1-7

by Ibrahim Tanyalcin

HTML

<script src="//d3js.org/d3.v3.min.js"></script>
<script src="https://deogen2.mutaframe.com/lexicon/beta/lexiconDistribute.js"></script>

CSS

h2 {
	text-align:center;
}

JavaScript

/*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");