Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/06/10 11:59:50 (14 years ago)
Author:
gkronber
Message:

Minor improvements concerning efficiency of symbolic expression tree encoding data structures and operators. #1073

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Random/3.3/NormalDistributedRandom.cs

    r3376 r3997  
    5858    private IRandom uniform;
    5959
    60     private double[] w = new double[] {
     60    private static double[] w = new double[] {
    6161      1.7290404664e-09,
    6262      1.2680928529e-10,
     
    188188      1.6030947680e-09
    189189    };
    190     private long[] k = new long[] {
     190    private static long[] k = new long[] {
    191191      1991057938,
    192192      0,
     
    318318      2010539237
    319319   };
    320     private double[] f = new double[] {
     320    private static double[] f = new double[] {
    321321      1.0000000000e+00,
    322322      9.6359968185e-01,
     
    510510    /// <returns>A double random number.</returns>
    511511    public double NextDouble() {
    512       double signFactor = uniform.Next() % 2 == 0 ? 1.0 : -1.0;
    513       return sigma * signFactor * NextPositiveDouble() + mu;
    514     }
    515 
    516     private double NextPositiveDouble() {
    517       int j = uniform.Next();
    518       int i = (j & 127);
    519       if (Math.Abs(j) < k[i]) {
    520         return j * w[i];
    521       } else {
    522         double r = 3.442620;
    523         double x, y;
    524         x = j * w[i];
    525         if (i == 0) {
    526           do {
    527             x = -Math.Log(ScaledUniform()) * 0.2904764;
    528             y = -Math.Log(ScaledUniform());
    529           } while (y + y < x * x);
    530           return (j > 0) ? r + x : -r - x;
    531         }
    532         if (f[i] + ScaledUniform() * (f[i - 1] - f[i]) < Math.Exp(-0.5 * x * x)) {
    533           return x;
    534         } else {
    535           // recurse
    536           return (NextPositiveDouble());
    537         }
    538       }
    539     }
    540 
    541     private double ScaledUniform() {
    542       return 0.5 + uniform.Next() * 0.2328306e-9;
    543     }
    544 
     512      return NormalDistributedRandom.NextDouble(uniform, mu, sigma);
     513    }
    545514
    546515    #endregion
     
    560529      return clone;
    561530    }
     531
     532    public static double NextDouble(IRandom uniformRandom, double mu, double sigma) {
     533      double signFactor = uniformRandom.Next() % 2 == 0 ? 1.0 : -1.0;
     534      return sigma * signFactor * NextPositiveDouble(uniformRandom) + mu;
     535    }
     536
     537    private static double NextPositiveDouble(IRandom uniformRandom) {
     538      int j = uniformRandom.Next();
     539      int i = (j & 127);
     540      if (Math.Abs(j) < k[i]) {
     541        return j * w[i];
     542      } else {
     543        double r = 3.442620;
     544        double x, y;
     545        x = j * w[i];
     546        if (i == 0) {
     547          do {
     548            x = -Math.Log(ScaledUniform(uniformRandom)) * 0.2904764;
     549            y = -Math.Log(ScaledUniform(uniformRandom));
     550          } while (y + y < x * x);
     551          return (j > 0) ? r + x : -r - x;
     552        }
     553        if (f[i] + ScaledUniform(uniformRandom) * (f[i - 1] - f[i]) < Math.Exp(-0.5 * x * x)) {
     554          return x;
     555        } else {
     556          // recurse
     557          return (NextPositiveDouble(uniformRandom));
     558        }
     559      }
     560    }
     561
     562    private static double ScaledUniform(IRandom uniformRandom) {
     563      return 0.5 + uniformRandom.Next() * 0.2328306e-9;
     564    }
    562565  }
    563566}
Note: See TracChangeset for help on using the changeset viewer.