Created
April 13, 2015 21:52
-
-
Save justinoboyle/bb5ad3531efd42ef1f3b to your computer and use it in GitHub Desktop.
Truly random double example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Random; | |
public class Example2 { | |
/** | |
* A final List<Double> of all previous double generated with | |
* generateUniqueDouble(), turned into a string with Double.valueOf(d); | |
*/ | |
private static final List<Double> PREVIOUS = new ArrayList<Double>(); | |
/** | |
* The RANDOM variable used in the class. | |
*/ | |
private static final Random RANDOM = new Random(); | |
/** | |
* Generates a truly unique double. | |
* | |
* @param previous | |
* A List<Double> of previous doubles, converted into a Double | |
* with Double.valueOf(d); | |
* @return a UUID generated with UUID.randomUUID(); that is not included in | |
* the given List<Double>. | |
*/ | |
public static double generateUniqueDouble(List<Double> previous) { | |
double d = RANDOM.nextDouble(); | |
if (previous.contains(Double.valueOf(d))) { | |
return generateUniqueDouble(previous); | |
} | |
return d; | |
} | |
/** | |
* Generates a truly unique double using the final List<Double> PREVIOUS | |
* variable defined at the top of the class. | |
* | |
* @return A truly random double created with generateUnique(List<Double> | |
* previous); | |
*/ | |
public static double generateUnique() { | |
double d = RANDOM.nextDouble(); | |
PREVIOUS.add(Double.valueOf(d)); | |
return d; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment