View Full Version : Generate a random number, Spec range, floor is optional
I wanted to be able to generate a random number--but I wanted to specify the range. The only thing I could find allowed me to generate a number between 0 and 10 (or some other multiple of 10).
The following accepts either 1 or 2 arguments (the ceiling and optionally, the floor):
RandomNum = randNum(43); //Generate a number between 0 and 43
RandomNum = randNum(43,134); //Generate a number between 43 and 134
<html>
<body>
<script type="text/javascript">
function randNum(){
var l = arguments.length;
var f = -1;
var c = -1;
switch (l){
case 1:
f = 0;
c = arguments[0];
break;
case 2:
f = arguments[0];
c = arguments[1];
break;
default:
f = -1;
}
if (f >= 0){
var r = Math.random();
var d = c - f;
var rnd = r * d;
var rand = Math.round(rnd) + f;
}
return rand;
}
var RandomNum = randNum(43,134);
document.write("Random Number: " + RandomNum);
</script>
</body>
jmrker
04-30-2010, 08:13 PM
Why so complicated?
<html>
<head>
<title>Random Number and Random Range Functions</title>
<script type="text/javascript">
// For: http://codingforums.com/newreply.php?do=postreply&t=195103
function randNum(range) {
return Math.floor(Math.random() * range); // returns 0 ... (range-1)
}
function randRange(lo,hi) {
var range = Number(hi)-Number(lo);
return randNum(range)+lo; // returns lo ... (hi-1)
}
// For repeated testing purposes only
function TestFunctions() {
var str = '';
var RandomNum = randNum(43);
str += "Random Number (0-43):" + RandomNum;
RandomNum = randRange(43,134);
str += '<p>'+"Random Range (43-134): " + RandomNum;
return str;
}
</script>
<body>
<button onclick="document.getElementById('test').innerHTML=TestFunctions()">Test</button>
<div id="test" style="height:100px;width:200px;border:1px solid red"></div>
</body>
</html>
Only requires 2 short function, one that calls the other!
Why so complicated?
No offense meant but although my code has more lines, I think it's easier to follow.
In fact, as best I can tell, using the code you've given, if I want to only specify the "ceiling", then I should use randNum. But if I also want to specify the floor, I need to use randRange.
The function provided allows ONE FUNCTION to handle both cases.
deniel.volt
05-29-2010, 11:45 AM
Hi Guys,
Can i use this same function in asp.net with free web scripts (http://www.softter.com/) if available or i have to buy scripts (http://www.softter.com/)?
Deniel.
---------------------------------
"Never look down on anybody unless you're helping him up. "
Dormilich
05-29-2010, 01:01 PM
var r = Math.random();
var d = c - f;
var rnd = r * d;
var rand = Math.round(rnd) + f;
you are aware that this produces not evenly distributed results? you’ve got a systematic error of 50% (i.e. 50% less probability) on the min and max values. that’s the reason you always should use Math.floor() to get an even distribution.
jmrker
06-27-2010, 05:25 AM
No offense meant but although my code has more lines, I think it's easier to follow.
In fact, as best I can tell, using the code you've given, if I want to only specify the "ceiling", then I should use randNum. But if I also want to specify the floor, I need to use randRange.
The function provided allows ONE FUNCTION to handle both cases.
OK, just one function ...
BTW, what happens to your one function if the user forgets to put in any arguments? (mine returns -1)
<html>
<head>
<title>Random Number and Random Range Functions</title>
<script type="text/javascript">
// For: http://codingforums.com/newreply.php?do=postreply&t=195103
function randRange() {
var alen = arguments.length; // alert(alen);
if (alen == 0) { return -1; }
if (alen == 2) {
var range = Number(arguments[1])-Number(arguments[0]);
return Math.floor(Math.random() * range)+Number(arguments[0]); // returns lo ... (hi-1)
} else {
return Math.floor(Math.random() * Number(arguments[0])); // returns 0 ... (range-1)
}
}
// For repeated testing purposes only
function TestFunctions() {
var str = '';
var RandomNum = randRange(43);
str += "Random Number (0-43):" + RandomNum;
RandomNum = randRange(43,134);
str += '<p>'+"Random Range (43-134): " + RandomNum;
RandomNum = randRange();
str += '<p>'+"missing argument (): " + RandomNum;
return str;
}
</script>
<body>
<button onclick="document.getElementById('test').innerHTML=TestFunctions()">Test</button>
<div id="test" style="height:150px;width:200px;border:1px solid red"></div>
</body>
</html>
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.