peersim.util
Class RandPermutation

java.lang.Object
  extended by peersim.util.RandPermutation
All Implemented Interfaces:
IndexIterator

public class RandPermutation
extends java.lang.Object
implements IndexIterator

This class provides a random permutation of indexes. Useful for random sampling without replacement.


Constructor Summary
RandPermutation(int k, java.util.Random r)
          Sets source of randomness and initial size.
RandPermutation(java.util.Random r)
          Sets source of randomness to be used.
 
Method Summary
 int get(int i)
          Returns the ith element of the permutation set by setPermutation(int).
 boolean hasNext()
          Returns true if IndexIterator.next() can be called at least one more time.
 int next()
          Next random sample without replacement
 void reset(int k)
          It initiates a random permutation of the integers from 0 to k-1.
 void setPermutation(int k)
          It calculates a random permutation of the integers from 0 to k-1.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RandPermutation

public RandPermutation(java.util.Random r)
Sets source of randomness to be used. You need to call reset(int) to fully initialize the object.

Parameters:
r - Source of randomness

RandPermutation

public RandPermutation(int k,
                       java.util.Random r)
Sets source of randomness and initial size. It calls setPermutation(int) to fully initialize the object with a permuation ready to use.

Parameters:
r - Source of randomness
k - size of permutation
Method Detail

setPermutation

public void setPermutation(int k)
It calculates a random permutation of the integers from 0 to k-1. The permutation can be read using method get(int). If the previous permutation was of the same length, it is more efficient. Note that after calling this the object is reset, so next() can be called k times, even if get(int) was called an arbitrary number of times. Note however that mixing get(int) and next() results in incorrect behavior for get(int) (but next() works fine). The idea is to use this method only in connection with get(int).


get

public int get(int i)
Returns the ith element of the permutation set by setPermutation(int). If next() is called after setPermutation(int) and before this method, then the behavior of this method is unspecified.


reset

public void reset(int k)
It initiates a random permutation of the integers from 0 to k-1. It does not actually calculate the permutation. The permutation can be read using method next(). Calls to get(int) return undefined values, so next() must be used. If the previous permutation was of the same length, it is more efficient.

Specified by:
reset in interface IndexIterator

next

public int next()
Next random sample without replacement

Specified by:
next in interface IndexIterator

hasNext

public boolean hasNext()
Description copied from interface: IndexIterator
Returns true if IndexIterator.next() can be called at least one more time. Note that IndexIterator.next() can be called k times after IndexIterator.reset(int).

Specified by:
hasNext in interface IndexIterator