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

23rd May 2017, 3:31 PM
Manik Tafralian
Manik Tafralian - avatar
7 Answers
+ 18
Math.round(Math.random()) but this gives only 0 and 1 do : Math.round(Math.random()*(to-from)+from)
23rd May 2017, 7:19 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 17
(I updated original answer, check it out....)
23rd May 2017, 5:42 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 16
No, it'll give all multiples....
23rd May 2017, 5:28 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 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();
23rd May 2017, 5:01 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 10
Not good with JS syntax, so I'll just write the algorithm. for (var i = 1; i < LIMIT; i++) { // print (rand_num * i) }
23rd May 2017, 4:23 PM
Hatsy Rei
Hatsy Rei - avatar
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
23rd May 2017, 5:26 PM
Manik Tafralian
Manik Tafralian - avatar
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>
23rd May 2017, 6:48 PM
Manik Tafralian
Manik Tafralian - avatar