Test randomness in JavaScript


/ Published in: JavaScript
Save to your folder(s)

This function tests how random will the Math.random() function be. You can test it with rnd(1000000) a couple of times to get results.


Copy this code and paste it in your HTML
  1. function rnd(j){
  2. arr = [0,0]
  3. for(i=0;i<=j;i++){
  4. r = Math.round(Math.random())
  5. arr[r] = arr[r]+1
  6. }
  7. a = arr[0] / arr[1] * 100
  8. return a
  9. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.