/*** Returns a random number between min (inclusive) and max (exclusive)*/function getRandomArbitrary(min, max) {return Math.random() * (max - min) + min;}
/*** Returns a random integer between min (inclusive) and max (inclusive).* The value is no lower than min (or the next integer greater than min* if min isn't an integer) and no greater than max (or the next integer* lower than max if max isn't an integer).* Using Math.round() will give you a non-uniform distribution!*/function getRandomInt(min, max) {min = Math.ceil(min);max = Math.floor(max);return Math.floor(Math.random() * (max - min + 1)) + min;}
function getRandomizer(bottom, top) {return function() {return Math.floor( Math.random() * ( 1 + top - bottom ) ) + bottom;}}
用法:
var rollDie = getRandomizer( 1, 6 );
var results = ""for ( var i = 0; i<1000; i++ ) {results += rollDie() + " "; // Make a string filled with 1000 random numbers in the range 1-6.}
function getRandomInt(lower, upper){//to create an even sample distributionreturn Math.floor(lower + (Math.random() * (upper - lower + 1)));
//to produce an uneven sample distribution//return Math.round(lower + (Math.random() * (upper - lower)));
//to exclude the max value from the possible values//return Math.floor(lower + (Math.random() * (upper - lower)));}
<html><head><script type="text/javascript">function getRandomInt(lower, upper){//to create an even sample distributionreturn Math.floor(lower + (Math.random() * (upper - lower + 1)));
//to produce an uneven sample distribution//return Math.round(lower + (Math.random() * (upper - lower)));
//to exclude the max value from the possible values//return Math.floor(lower + (Math.random() * (upper - lower)));}
var min = -5;var max = 5;
var array = new Array();
for(var i = 0; i <= (max - min) + 2; i++) {array.push(0);}
for(var i = 0; i < 1000000; i++) {var random = getRandomInt(min, max);array[random - min + 1]++;}
var maxSample = 0;for(var i = 0; i < max - min; i++) {maxSample = Math.max(maxSample, array[i]);}
//create a bar graph to show the sample distributionvar maxHeight = 500;for(var i = 0; i <= (max - min) + 2; i++) {var sampleHeight = (array[i]/maxSample) * maxHeight;
document.write('<span style="display:inline-block;color:'+(sampleHeight == 0 ? 'black' : 'white')+';background-color:black;height:'+sampleHeight+'px"> [' + (i + min - 1) + ']: '+array[i]+'</span> ');}document.write('<hr/>');</script></head><body>
</body></html>
Now for [0,10) ==> n*10 (i.e. one digit) and for[10,100) ==> n*100 (i.e., two digits) and so on. Here square bracket indicates that the boundary is inclusive and a round bracket indicates the boundary is exclusive.
function genRandomNumber(how_many_numbers, min, max) {
// Parameters//// how_many_numbers: How many numbers you want to// generate. For example, it is 5.//// min (inclusive): Minimum/low value of a range. It// must be any positive integer, but// less than max. I.e., 4.//// max (inclusive): Maximum value of a range. it must// be any positive integer. I.e., 50//// Return type: array
var random_number = [];for (var i = 0; i < how_many_numbers; i++) {var gen_num = parseInt((Math.random() * (max-min+1)) + min);do {var is_exist = random_number.indexOf(gen_num);if (is_exist >= 0) {gen_num = parseInt((Math.random() * (max-min+1)) + min);}else {random_number.push(gen_num);is_exist = -2;}}while (is_exist > -1);}document.getElementById('box').innerHTML = random_number;}
// Get random number within provided base + exponent// By Goran Biljetina --> 2012
function isEmpty(value) {return (typeof value === "undefined" || value === null);}
var numSeq = new Array();
function add(num, seq) {var toAdd = new Object();toAdd.num = num;toAdd.seq = seq;numSeq[numSeq.length] = toAdd;}
function fillNumSeq (num, seq) {var n;for(i=0; i<=seq; i++) {n = Math.pow(num, i);add(n, i);}}
function getRandNum(base, exp) {if (isEmpty(base)) {console.log("Specify value for base parameter");}if (isEmpty(exp)) {console.log("Specify value for exponent parameter");}
fillNumSeq(base, exp);
var emax;var eseq;var nseed;var nspan;emax = (numSeq.length);eseq = Math.floor(Math.random()*emax) + 1;nseed = numSeq[eseq].num;nspan = Math.floor((Math.random())*(Math.random()*nseed)) + 1;return Math.floor(Math.random()*nspan) + 1;}
console.log(getRandNum(10, 20), numSeq);
//Testing://getRandNum(-10, 20);//console.log(getRandNum(-10, 20), numSeq);//console.log(numSeq);
<!DOCTYPE html><html><head><meta charset="utf-8" /></head>
<body><script>/*Assuming that window.crypto.getRandomValuesis available, the real range would be from0 to 1,998 instead of 0 to 2,000.
See the JavaScript documentationfor an explanation:
https://developer.mozilla.org/en-US/docs/Web/API/RandomSource/getRandomValues*/var array = new Uint8Array(2);window.crypto.getRandomValues(array);console.log(array[0] + array[1]);</script></body></html>
Generate a 4-bit integer in the range 1-16.If we generated 1, 6, or 11 then output 1.If we generated 2, 7, or 12 then output 2.If we generated 3, 8, or 13 then output 3.If we generated 4, 9, or 14 then output 4.If we generated 5, 10, or 15 then output 5.If we generated 16 then reject it and try again.