Advertisement
luizaspan

[AULA CANZIAN] (pseudo)Random numbers

Nov 11th, 2015
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2.  
  3. // Math.random() devolve números float entre 0 e 1
  4.  
  5. // var x = Math.random();
  6. // document.write(Math.sqrt(x));
  7.  
  8. var hst = new Array(10); // fazendo paple do float x[10]
  9.  
  10. for (var i=0; i<hst.length; i++) hst[i]=0; // construindo uma matriz com 10 elementos e enchendo de 0
  11.  
  12. var dx = 1/hst.length;
  13. var N = 10000;
  14. for (var i=0; i<N; i++) {
  15.     var x = Math.sqrt(Math.random());
  16.     for (var j=0; j<hst.length; j++) {
  17.         if (x>=j*dx && x<(j+1)*dx) {
  18.             hst[j]++;
  19.         }
  20.     }
  21. }
  22.  
  23. for (var i=0; i<hst.length; i++) {
  24.     document.write(hst[i] + "<br>");
  25. }
  26.  
  27. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement