Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  Copyright � 1999 CERN - European Organization for Nuclear Research.
  Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose 
  is hereby granted without fee, provided that the above copyright notice appear in all copies and 
  that both that copyright notice and this permission notice appear in supporting documentation. 
  CERN makes no representations about the suitability of this software for any purpose. 
  It is provided "as is" without expressed or implied warranty.
  */
  package org.apache.mahout.math.jet.random;
 
 
 
 public class Uniform extends AbstractContinousDistribution {
 
   private double min;
   private double max;

  
Constructs a uniform distribution with the given minimum and maximum, using a org.apache.mahout.math.jet.random.engine.MersenneTwister seeded with the given seed.
 
   public Uniform(double mindouble maxint seed) {
     this(minmax, RandomUtils.getRandom(seed));
   }

  
Constructs a uniform distribution with the given minimum and maximum.
 
   public Uniform(double mindouble maxRandom randomGenerator) {
     setRandomGenerator(randomGenerator);
     setState(minmax);
   }

  
Constructs a uniform distribution with min=0.0 and max=1.0.
 
   public Uniform(Random randomGenerator) {
     this(0, 1, randomGenerator);
   }

  
Returns the cumulative distribution function (assuming a continous uniform distribution).
 
   @Override
   public double cdf(double x) {
     if (x <= ) {
       return 0.0;
     }
     if (x >= ) {
       return 1.0;
     }
     return (x - ) / ( - );
   }

  
Returns a uniformly distributed random boolean.
 
   public boolean nextBoolean() {
     return randomDouble() > 0.5;
   }

  
Returns a uniformly distributed random number in the open interval (min,max) (excluding min and max).
 
   @Override
   public double nextDouble() {
     return  + ( - ) * randomDouble();
   }

  
Returns a uniformly distributed random number in the open interval (from,to) (excluding from and to). Pre conditions: from <= to.
 
   public double nextDoubleFromTo(double fromdouble to) {
     return from + (to - from) * randomDouble();
   }

  
Returns a uniformly distributed random number in the open interval (from,to) (excluding from and to). Pre conditions: from <= to.
 
   public float nextFloatFromTo(float fromfloat to) {
     return (floatnextDoubleFromTo(fromto);
   }

  
Returns a uniformly distributed random number in the closed interval [from,to] (including from and to). Pre conditions: from <= to.
 
   public int nextIntFromTo(int fromint to) {
     return (int) (from + (long) ((1L + to - from) * randomDouble()));
   }

  
Returns a uniformly distributed random number in the closed interval [from,to] (including from and to). Pre conditions: from <= to.
 
   public long nextLongFromTo(long fromlong to) {
     /* Doing the thing turns out to be more tricky than expected.
        avoids overflows and underflows.
        treats cases like from=-1, to=1 and the like right.
        the following code would NOT solve the problem: return (long) (Doubles.randomFromTo(from,to));
 
       rounding avoids the unsymmetric behaviour of casts from double to long: (long) -0.7 = 0, (long) 0.7 = 0.
       checking for overflows and underflows is also necessary.
    */
    // first the most likely and also the fastest case.
    if (from >= 0 && to < .) {
      return from + (longnextDoubleFromTo(0.0, to - from + 1);
    }
    // would we get a numeric overflow?
    // if not, we can still handle the case rather efficient.
    double diff = (doubleto - (doublefrom + 1.0;
    if (diff <= .) {
      return from + (longnextDoubleFromTo(0.0, diff);
    }
    // now the pathologic boundary cases.
    // they are handled rather slow.
    long random;
    if (from == .) {
      if (to == .) {
        //return Math.round(nextDoubleFromTo(from,to));
        int i1 = nextIntFromTo(..);
        int i2 = nextIntFromTo(..);
        return ((i1 & 0xFFFFFFFFL) << 32) | (i2 & 0xFFFFFFFFL);
      }
      random = Math.round(nextDoubleFromTo(.to + 1));
      if (random > to) {
        random = .;
      }
    } else {
      random = Math.round(nextDoubleFromTo(from - 1, to));
      if (random < from) {
        random = to;
      }
    }
    return random;
  }

  
Returns the probability distribution function (assuming a continous uniform distribution).
  public double pdf(double x) {
    if (x <=  || x >= ) {
      return 0.0;
    }
    return 1.0 / ( - );
  }

  
Sets the internal state.
  public void setState(double mindouble max) {
    if (max < min) {
      setState(maxmin);
      return;
    }
    this. = min;
    this. = max;
  }


  
Returns a String representation of the receiver.
  public String toString() {
    return this.getClass().getName() + '(' +  + ',' +  + ')';
  }
New to GrepCode? Check out our FAQ X