Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. //Math.random() returns a floating point number from 0 up to but not including 1. For example:
  2.  
  3. /*
  4. > Math.random()
  5. 0.2625259844878184
  6. > Math.random()
  7. 0.06513541210374607
  8. > Math.random()
  9. 0.5629554153276711
  10. */
  11.  
  12. /*
  13. but normally you will want a larger number. To do this you can multiply Math.random() by a number. If we wanted numbers between 0 & up to but not including 100:
  14. > Math.random() * 100
  15. 26.25259844878184
  16. > Math.random() * 100
  17. 6.513541210374607
  18. > Math.random() * 100
  19. 56.29554153276711
  20. */
  21.  
  22. /*
  23. Often you will want a whole number, for that we can use Math.floor() which will strip off the numbers after the decimal:
  24. */
  25.  
  26. Math.floor( Math.random() * 100 );//output any numbers 0,1,2,3,4,5 ...... 97,98,99. 99 ONLY
  27.  
  28.  
  29. /*
  30. If you wanted numbers between 1 & 100 then you would need to + 1. So it becomes
  31. */
  32. Math.floor( Math.random() * 100 ) + 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement