Posted By

shuchii on 08/26/10


Tagged

math random round floor Ceil


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

davidino1978
alptugan
tspitzr
albertomori
pepperpunk


Math basics


 / Published in: ActionScript 3
 

URL: http://www.flashfiles.nl/ShowDetail.asp?NewsId=6080

I always seem to forget about the Math class and its functions and possibilities… So I created this snippet as an easy reminder. Feel free to send me suggestions or extras I might add!

  1. n = Math.random();
  2. // 0 <= n < 1
  3.  
  4. n = Math.round(x);
  5. // rounds x to the closest integer
  6.  
  7. n = Math.floor(x);
  8. // rounds x downwards to the closest integer
  9.  
  10. n = Math.ceil(x);
  11. // rounds x upwards to the closest integer
  12.  
  13. n = Math.round(Math.random()*10);
  14. // 0 <= n <= 10
  15.  
  16. n = Math.ceil(Math.random()*10);
  17. // 1 <= n <= 10
  18.  
  19. n = Math.floor(Math.random()*10);
  20. // 0 <= n <= 9
  21.  
  22. n = Math.random()*10 + 5;
  23. // 5 <= n <= 15
  24.  
  25. n = Math.round(Math.random())*2 - 1;
  26. // n will randomly be -1 or 1

Report this snippet  

You need to login to post a comment.