/*
Math.random() - JavaScript | MDN - Mozilla Developer Network
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
*/
var RandomValues = (function(){
function getRandomFloat(min, max){
return ((Math.random() * (max - min + 1)) + min);
}
function getRandomDecimal(min, max, fixed){
fixed = fixed || 2;
return +getRandomFloat(min, max).toFixed(fixed);
}
function getRandomInt(min, max) {
return Math.floor(getRandomFloat(min, max));
}
function getRandomIntAsString(min, max) {
var intValue = getRandomInt(min,max);
return (
intValue < 10 ?
"0" + intValue :
intValue.toString()
);
}
function getRandomBoolean(){
return (getRandomInt(0, 1) === 0);
}
// given one array, get one random item
function getRandomItem(items, inNumber){
inNumber = inNumber || items.length;
return items[
Math.floor(
Math.random()*inNumber
)
];
}
//---
// API
return {
float : getRandomFloat,
decimal : getRandomDecimal,
int : getRandomInt,
intAsString : getRandomIntAsString,
boolean : getRandomBoolean,
item : getRandomItem
};
})();
working with mock data these functions helps a lot :D
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.