|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectpeersim.util.WeightedRandPerm
public class WeightedRandPerm
This class provides a weighted random permutation of indexes.
Useful for weighted random sampling without replacement.
The next sample is taken according to the weights given as a parameter
to reset(int)
.
The weights work as follows.
The first sample is drawn according to the probability distribution
defined by the (normalized) weights.
After this the remaining k-1 elements and the associated k-1
(re-normalized) weights
define a new probability distribution, according to which the 2nd element
is drawn, and so on.
Constructor Summary | |
---|---|
WeightedRandPerm(java.util.Random r,
double[] weights)
Set the source of randomness to use and the weights. |
Method Summary | |
---|---|
boolean |
hasNext()
Returns true if IndexIterator.next() can be called at least one more time. |
int |
next()
The first sample is drawn according to the probability distribution defined by the (normalized) weights. |
void |
reset(int k)
It initiates a random weighted permutation of the integeres 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 |
---|
public WeightedRandPerm(java.util.Random r, double[] weights)
reset(int)
to fully initialize the object.
r
- source of randomnessweights
- The array that holds the weights for the calculation of the
permutation. The length of the array will be an upper bound on the
parameter reset(int)
accepts. If reset(int)
is called with a
parameter less than the length of weights, the prefix of the same length
is used.
The vector elements must be positive, that is, zero is not accepted either.Method Detail |
---|
public void reset(int k)
next()
.
If the previous permutation was of the same length, it is more efficient.
The weights set at construction time work as follows.
The first sample is drawn according to the probability distribution
defined by the (normalized) weights.
After this the remaining k-1 elements and the associated k-1
(re-normalized) weights
define a new probability distribution, according to which the 2nd element
is drawn, and so on.
reset
in interface IndexIterator
k
- the set is defined as 0,...,k-1public int next()
next
in interface IndexIterator
reset(int)
public boolean hasNext()
IndexIterator
IndexIterator.next()
can be called at least one more time.
Note that IndexIterator.next()
can be called k times after IndexIterator.reset(int)
.
hasNext
in interface IndexIterator
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |