Ticket #4276: 4276_boxmuller.diff
File 4276_boxmuller.diff, 1.2 KB (added by , 8 years ago) |
---|
-
binaries/data/mods/public/simulation/components/Attack.js
Attack.prototype.GetAttackBonus = functi 441 441 442 442 // Returns a 2d random distribution scaled for a spread of scale 1. 443 443 // The current implementation is a 2d gaussian with sigma = 1 444 444 Attack.prototype.GetNormalDistribution = function(){ 445 445 446 // Use the Box-Muller transform to get a gaussian distribution 447 let a = Math.random(); 448 let b = Math.random(); 449 450 let c = Math.sqrt(-2*Math.log(a)) * Math.cos(2*Math.PI*b); 451 let d = Math.sqrt(-2*Math.log(a)) * Math.sin(2*Math.PI*b); 452 453 return [c, d]; 446 // Use the Box-Muller transform with the polar method to get a gaussian distribution 447 let s, a, b; 448 do 449 { 450 a = 2 * Math.random() - 1; 451 b = 2 * Math.random() - 1; 452 s = a * a + b * b; 453 } 454 while (s>=1 || s==0) 455 s = Math.sqrt(-2*Math.log(s) / s); 456 return [a * s, b * s]; 454 457 }; 455 458 456 459 /** 457 460 * Attack the target entity. This should only be called after a successful range check, 458 461 * and should only be called after GetTimers().repeat msec has passed since the last