+ 1
rand
How is possible to improve function function getRandomArbitary(min, max){ return Math.random() * (max - min) + min; } if i want to return 'max' variable as possible?
5 Respuestas
+ 9
just add + 1 to (max - min) so it becomes
(max - min + 1) and wrap it all with Math.floor()
function randVal(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
+ 1
i came to this solution (with user input min and max and rounded number (you can remove those))
var max = prompt("max");
var min =prompt("min");
var x = Math.floor(Math.random(min)*max);
document.write(x);
0
sorry for my misunderstanding, but did you ment you want a random number between min and max?
0
yes
0
hues