Advertisement
Dimenticare

Normal (Gaussian) Random Number Generator

Jun 5th, 2014
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* double random_normal(mean, standard_deviation);
  2. * Implements the Polar form of the Box-Muller Transformation
  3. *
  4. * © Copyright 1994, Everett F. Carter Jr.
  5. * Ported to GML by Tsa05
  6. * Slightly modified by Dragonite (Dimenticare on Pastebin)
  7. * Permission is granted by the author to use
  8. * this software for any application provided this
  9. * copyright notice is preserved.
  10. *
  11. * Recall that numbers beyond the Standard Deviation
  12. * are possible (just not probable), and should be accounted for.
  13. *
  14. */
  15.  
  16. var m, s;
  17.  
  18. m=argument0;
  19. s=argument1;
  20.  
  21. var x1, x2, w, y1;
  22.  
  23. w=2;
  24.  
  25. while ( w >= 1.0 ){
  26.     x1 = 2.0 * random(1) - 1.0;
  27.     x2 = 2.0 * random(1) - 1.0;
  28.     w = x1 * x1 + x2 * x2;
  29. }
  30.  
  31. var r;
  32. r=m+(x1*(sqrt((-2.0*ln(w))/w)))*s;
  33. if (r>0)
  34.     return r;
  35. return random_normal(argument0, argument1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement