public class RandomStructs extends Object
Calculation is based on one particular instance of Random
,
the Java runtime random generator. stored in
generator
, and some methods are simply forwarded.
A message receiver can be used to generate a (multi lingual) protocol of all
decisions, to make e.g. the genesis of a composition transparent for analysis.
FIXME config methods not yet defined.
Modifier and Type | Field and Description |
---|---|
(package private) Random |
generator
One single Java built-in random generator, used sequentially for all incoming requests.
|
(package private) @Opt String |
ident
Name of this instance in protocol lines.
|
(package private) static byte[] |
masks
Aux data for masking out the bit patterns generated by
generator . |
(package private) @Opt MessageReceiver<SimpleMessage<?>> |
protocol
If set, protocol lines are generated for each random selection operation.
|
(package private) @Opt MessageFormatter.Formandum |
purpose
A temporary register to integrate the purpose of a generated random value into
the protocol message.
|
static CatalogByString |
translations |
Constructor and Description |
---|
RandomStructs()
Constructor without protocol generation.
|
RandomStructs(MessageReceiver<SimpleMessage<?>> protocol,
@Opt String ident)
Constructor with protocol generation.
|
Modifier and Type | Method and Description |
---|---|
int |
integerWeighted(int[] weights)
Returns by random an index into an array of relative weights.
|
List<Integer> |
intPermutation(int limit)
Calculates randomly a permutation of the integers 0 to limit-1.
|
BitSet |
intSet(int limit)
Calculate a
BitSet in which the lowest "limit" bits are randomly 0 or 1. |
BitSet |
intSet(int limit,
int min,
int max)
Calculate a
BitSet in which the lowest "limit" bits are randomly 0 or 1,
with given minimum and maximum of 1 bits. |
(package private) void |
log(String text,
Object... args) |
static long |
makeSeed(String phrase)
Create a 32bit value as a start seed from a human language input text.
|
boolean |
ofOne(float p)
Calculates by random a boolean with the probability of p (of one) to be true.
|
void |
setPurpose(MessageFormatter.Formandum purpose)
Set the text which describes the purpose of the very next generated random value into
the protocol message.
|
void |
setPurpose(String purpose)
Set the text which describes the purpose of the very next generated random value into
the protocol message.
|
void |
setSeed(long seed)
Set the starting point of the random generator, to make all results reproducable.
|
int |
singleInteger(int limit)
Calculates by random a positive integer between 0 and (limit-1).
|
int |
singleInteger(int[] values)
Selects by random one integer value from the array, all with equal probability.
|
int |
singleInteger(int lo,
int hi)
Calculates by random a positive integer between lo and (hi-1).
|
final Random generator
@Opt final @Opt MessageReceiver<SimpleMessage<?>> protocol
@Opt final @Opt String ident
@Opt @Opt MessageFormatter.Formandum purpose
public static CatalogByString translations
static final byte[] masks
generator
.public RandomStructs()
public RandomStructs(MessageReceiver<SimpleMessage<?>> protocol, @Opt @Opt String ident)
protocol
- where to send the protocol messagesident
- to identify this instance, may be null.public void setPurpose(MessageFormatter.Formandum purpose)
public void setPurpose(String purpose)
public static long makeSeed(String phrase)
public void setSeed(long seed)
generator
.public int singleInteger(int limit)
generator
.IllegalArgumentException
- if limit<0public int singleInteger(int lo, int hi)
generator
.IllegalArgumentException
- if hipublic boolean ofOne(float p)
public int singleInteger(int[] values)
IllegalArgumentException
- if hipublic int integerWeighted(int[] weights)
[3,6,3]
in 50% of arbitrary many tries,
the index "1" will be returned, and in 25% each "0" and "2".public BitSet intSet(int limit)
BitSet
in which the lowest "limit" bits are randomly 0 or 1.
Calls Random.nextBytes(byte[])
which promises to distribute 0 and 1
randomly.IllegalArgumentException
- if limit<0public BitSet intSet(int limit, int min, int max)
BitSet
in which the lowest "limit" bits are randomly 0 or 1,
with given minimum and maximum of 1 bits.
Calculates first by random the number of 1-bits, then a list of
random positions to be set, until this numver of set bits is reached.
ATTENTION may not terminate if integer number generation is not truely random.limit
- the total length of the bit setmin
- possible minimal count of one-bitsmax
- possible maximal count of one-bits, inclusive.IllegalArgumentException
- if limit<0 or max<0public List<Integer> intPermutation(int limit)