True randomize
This function pick a randomly integer number between others numbers. If the parameter "lastNumber" is defined, the function will not return a randomly number which equal "lastNumber".
<br/>
<br/>
This function take 3 parameters :
<br/>
- min : (default value : 0)
<br/>
- max : (default value : 1000)
<br/>
- lastNumber : (default value : undefined)
<br/>
<br/>
Usage :
<br/>
- rand(); // return a number between 0 and 1000
<br/>
- rand(-123, 123); // return a number between -123 and 123
<br/>
- rand(0, 2, 1); // return 0 or 2
<br/>
<br/>
Thanks guys, and sorry for bad english, I'm french...
function rand(min, max, lastNumber) {
min = min || 0
max = max || 1000
var n = Math.round(Math.random() * (max - min) + min);
if(n == lastNumber)
n = rand(min, max, n);
return n;
}
Language: JavaScript | User: Hugo Alliaume | Created: Nov 9, 2013 | Tags: javascript random