项目原始demo,不改动
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
Este repositorio está archivado. Puede ver los archivos y clonarlo, pero no puede subir cambios o reportar incidencias ni pedir Pull Requests.
 
 
 
 

23 líneas
683 B

  1. 'use strict'
  2. /* A simple π estimation function using a Monte Carlo method
  3. * For 0 to `points`, take 2 random numbers < 1, square and add them to
  4. * find the area under that point in a 1x1 square. If that area is <= 1
  5. * then it's *within* a quarter-circle, otherwise it's outside.
  6. * Take the number of points <= 1 and multiply it by 4 and you have an
  7. * estimate!
  8. * Do this across multiple processes and average the results to
  9. * increase accuracy.
  10. */
  11. module.exports = function (points, callback) {
  12. let inside = 0
  13. , i = points
  14. while (i--)
  15. if (Math.pow(Math.random(), 2) + Math.pow(Math.random(), 2) <= 1)
  16. inside++
  17. callback(null, (inside / points) * 4)
  18. }