0

How we can make a range of Math.random output numbers?

Math.random gives us no. from 0 to 9 but if we want to set a range like 3 to 8. so is that any specific function which can do this?

4th Feb 2017, 1:37 PM
Md.Enam
Md.Enam - avatar
3 odpowiedzi
+ 3
That's what I already previouly done ^^ If you mean write the code who generate an array of random numbers, simply do a loop: var count=42; var aRand=[]; while (count--) aRand.push(Math.random()); ... give you an array 'aRand' with 42 float items randomly generated; basicaly :P
4th Feb 2017, 2:47 PM
visph
visph - avatar
+ 1
Math.random() return a float between 0 and 1 ( 1 excluded: interval [0;1[ ), so to have a range between [0,n[ you need to: n=5; var num = n * Math.random(); // [0;5] ... to define the range itself, and add the wanted shift: num += 3; // [3;8[
4th Feb 2017, 1:51 PM
visph
visph - avatar
0
can you give an example on this?
4th Feb 2017, 2:32 PM
Md.Enam
Md.Enam - avatar