Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2009-2012 Alexander Kerner. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 
 
 package net.sf.kerner.utils.math;
 
RandomFactory provides static methods to generate random numbers.

Example:

 TODO example
 

Author(s):
Alexander Kerner
Version:
2012-03-06
 
 public class RandomFactory {
 
     private final static Random R = new Random();
 
     private RandomFactory() {
 
     }

    
Generate a random double for which the following is true: random = value +- delta.

Parameters:
value
delta
Returns:
new random [@code double}
 
     public static double generateAround(final double valuefinal double delta) {
         final double low = value - delta;
         final double heigh = value + delta;
         return generateBetween(lowheigh);
     }

    
Generate a random double for which the following is true: low <= random <= high.

Parameters:
low lower boundary, inclusive
high upper boundary, exclusive
Returns:
new random [@code double}
 
     public static double generateBetween(final double lowfinal double high) {
         return low + (high - low) * .nextDouble();
     }

    
Generate a random int for which the following is true: random = value +- delta.

Parameters:
value
delta
Returns:
new random [@code int}
 
     public static int generateAround(final int valuefinal int delta) {
         final int low = value - delta;
         final int heigh = value + delta;
         return generateBetween(lowheigh);
     }

    
Generate a random int for which the following is true: low <= random <= high.

Parameters:
low lower boundary, inclusive
high upper boundary, inclusive
Returns:
new random [@code int}
 
     public static int generateBetween(final int lowfinal int high) {
         return low + .nextInt(high + 1 - low);
     }
 
     public static boolean generateWithProbability(double probability) {
         if (probability > 1)
             return true;
         if (probability <= 0)
            return false;
        probability = (int) (probability * 100);
        int result = generateBetween(0, 100);
        // System.out.println("prob="+probability);
        // System.out.println("result="+result);
        return result <= probability;
    }
    public static boolean generate() {
        return .nextBoolean();
    }
New to GrepCode? Check out our FAQ X