Package org.graphstream.algorithm.util
Class RandomTools
java.lang.Object
org.graphstream.algorithm.util.RandomTools
public class RandomTools extends Object
This class provides several static methods for generating random numbers and
sets
-
Constructor Summary
Constructors Constructor Description RandomTools()
-
Method Summary
Modifier and Type Method Description static int
binomial(int n, double p, Random rnd)
Returns a pseudorandom number drawn from binomial distribution B(n, p).static double
exponential(Random rnd)
Returns a pseudorandom number drawn from exponential distribution with mean 1.static Set<Integer>
randomKsubset(int n, int k, Set<Integer> subset, Random rnd)
Generates a pseudorandom subset of size k of the set {0, 1,...static Set<Integer>
randomPsubset(int n, double p, Set<Integer> subset, Random rnd)
Generates a pseudorandom subset of the set {0, 1,...
-
Constructor Details
-
RandomTools
public RandomTools()
-
-
Method Details
-
exponential
Returns a pseudorandom number drawn from exponential distribution with mean 1. Uses the von Neumann's exponential generator.- Parameters:
rnd
- source of randomness- Returns:
- a pseudorandom number drawn from exponential distribution with mean 1.
- Computational Complexity :
- O(1) average complexity. The expected number of uniformly distributed numbers used is e^2 / (e - 1)
-
binomial
Returns a pseudorandom number drawn from binomial distribution B(n, p). Uses a simple waiting time method based on exponential distribution.- Parameters:
n
- number of triesp
- success probabilityrnd
- source of randomness- Returns:
- a pseudorandom number drawn from binomial distribution
- Computational Complexity :
- Average complexity O(np)
-
randomKsubset
Generates a pseudorandom subset of size k of the set {0, 1,...,n - 1}. Each element has the same chance to be chosen. Uses Floyd's method of subset generation with only k iterations. Note that the quality of this generator is limited by Java's random generator. Java stores the internal state in 48 bits, so in the best case we can only generate 2^48 different subsets.- Parameters:
n
- the size of the initial setk
- the size of the generated setsubset
- if not null, this set is cleared and the result is stored here. This avoids creations of sets at each call of this methodrnd
- source of randomness- Returns:
- a pseudorandom subset of size k of the set {0, 1,...,n}
- Computational Complexity :
- Depends on the set implementation. If add and lookup operations take constant time, the complexity is O(k)
-
randomPsubset
Generates a pseudorandom subset of the set {0, 1,...,n - 1}. Each element is chosen with probability p.- Parameters:
n
- the size of the initial setp
- the probability to choose each elementsubset
- if not null, this set is cleared and the result is stored here. This avoids creations of sets at each call of this methodrnd
- source of randomness- Returns:
- a pseudorandom subset of the set {0, 1,...,n}.
- Computational Complexity :
- Depends on the set implementation. If add and lookup operations take constant time, the complexity is O(np)
-