0
The multiple of a random number
What method/ function shall I create to find the multiples of a random number from a range using for loop? F.ex var i=a i <b, I +=c
7 Answers
+ 18
Math.round(Math.random())
but this gives only 0 and 1
do :
Math.round(Math.random()*(to-from)+from)
+ 17
(I updated original answer, check it out....)
+ 16
No, it'll give all multiples....
+ 15
//Extending @Hatsy's answer
//use prompt() to let user decide
num = Math.random();
range = [prompt("from"),prompt("to")];
for (var stp = range[0]; stp <= range[1]; stp++) {
document.write(num*stp+"<br>");
//or alert
}
document.close();
+ 10
Not good with JS syntax, so I'll just write the algorithm.
for (var i = 1; i < LIMIT; i++)
{
// print (rand_num * i)
}
0
It will give one number, wont it ? l' f like to have all the possible multiples between that range, like for 3 multiples 3,6,9 ,etc
0
The output is float numbers. How can I fix it?
<!DOCTYPE html>
<head>
<title>Random</title>
</head>
<body>
<script>
var i=5,num=prompt("insert a number");
for(i<=100;i+=1;){
num=Math.random()
document.write("num*i+<br>")
}
</script>
</body>
</html>