Ticket #4276: 4276_boxmuller.diff

File 4276_boxmuller.diff, 1.2 KB (added by fatherbushido, 8 years ago)
  • binaries/data/mods/public/simulation/components/Attack.js

    Attack.prototype.GetAttackBonus = functi  
    441441
    442442// Returns a 2d random distribution scaled for a spread of scale 1.
    443443// The current implementation is a 2d gaussian with sigma = 1
    444444Attack.prototype.GetNormalDistribution = function(){
    445445
    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];
    454457};
    455458
    456459/**
    457460 * Attack the target entity. This should only be called after a successful range check,
    458461 * and should only be called after GetTimers().repeat msec has passed since the last