org.graphstream.algorithm.util
Class RandomTools

java.lang.Object
  extended by org.graphstream.algorithm.util.RandomTools

public class RandomTools
extends Object

This class provides several static methods for generating random numbers and sets


Constructor Summary
RandomTools()
           
 
Method Summary
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,...
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RandomTools

public RandomTools()
Method Detail

exponential

public static double exponential(Random rnd)
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

public static int binomial(int n,
                           double p,
                           Random rnd)
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 tries
p - success probability
rnd - source of randomness
Returns:
a pseudorandom number drawn from binomial distribution
Computational Complexity :
Average complexity O(np)

randomKsubset

public 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,...,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 set
k - the size of the generated set
subset - if not null, this set is cleared and the result is stored here. This avoids creations of sets at each call of this method
rnd - 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

public static Set<Integer> randomPsubset(int n,
                                         double p,
                                         Set<Integer> subset,
                                         Random rnd)
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 set
p - the probability to choose each element
subset - if not null, this set is cleared and the result is stored here. This avoids creations of sets at each call of this method
rnd - 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)


Copyright © 2013. All Rights Reserved.