org.randomness
Class Quasirandomess

java.lang.Object
  extended by org.randomness.Randomness
      extended by org.randomness.Quasirandomess
All Implemented Interfaces:
Closeable, Channel, ReadableByteChannel, ScatteringByteChannel

public abstract class Quasirandomess
extends Randomness

This class specify Quasirandom Number Generator techniques for quasirandom analogue of a random process which is a deterministic process specifically designed so that simulation of the quasirandom process gives the same limiting behavior (of some quantities of interest) as the random process, but with faster convergence.

PROVISIONAL API, WORK IN PROGRESS

(Quasirandomness is not to be confused with pseudorandomness. A pseudorandom process is supposed to be statistically indistinguishable from the truly random process it imitates. In contrast, a quasirandom process will typically have many regularities that mark it as non-random. Quasirandom processes only need to be irregular enough for the desired application.)

Most work on quasirandomness (also sometimes called subrandomness) has been motivated by Monte Carlo integration, and involves well-distributed sequences of points in some continuous space (e.g., the sequence of points in the interval [0,1] whose nth element is a0/2 + a1/4 + a2/8 + ..., where n = a0 + 2a1 + 4a< sub>2+ ..., or the sequence whose nth element is the fractional part of n times the golden ratio).

A series of numbers satisfying some mathematical random properties even though no random appearance is provided Good for Monte-Carlo methods Lower discrepancies offer better convergence

Author:
Антон

Constructor Summary
Quasirandomess()
           
 
Method Summary
 Quasirandomess bakerTransformation()
          Apply the baker's transformation to the output of this QRBG.
 void close()
          The uninstantiate function zeroizes the internal state of RBG interrupting generate function, if proceed.
 boolean isOpen()
          Tells whether or not this RBG is open to generate random bytes.
 int minlen()
          Minlen is the minimum block of bytes essentially produced per one iteration of generate function (optional operation).
 int read(ByteBuffer dst)
          The generate function generates the requested random bits using the generate algorithm.
 void reset()
          The instantiate function determines the initial internal state of RBG using the instantiate algorithm.
 String toString()
          Returns the name of the algorithm (or process) implemented by this RBG object (optional operation).
 int tryRead(ByteBuffer buffer)
          Attempts to read from this RBG into the given buffer, if generate function is not owned by other thread.
 
Methods inherited from class org.randomness.Randomness
asRandom, bind, map, mixing, newBuffer, nextBoolean, nextByte, nextChar, nextCharASCII, nextDouble, nextFloat, nextHexString, nextInt, nextInt, nextLong, nextLong, nextProbability, nextShort, read, read, read, readFuture, readSink, reversed, shuffle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Quasirandomess

public Quasirandomess()
Method Detail

isOpen

public boolean isOpen()
Description copied from class: Randomness
Tells whether or not this RBG is open to generate random bytes.

Specified by:
isOpen in interface Channel
Specified by:
isOpen in class Randomness

reset

public void reset()
Description copied from class: Randomness
The instantiate function determines the initial internal state of RBG using the instantiate algorithm. A RBG shall be instantiated prior to the generation of random bits.

If RBG has already initiated a generate operation, then an invocation of this method will block until the first operation is complete. If RBG already initiated a generate function, then an invocation of instantiate function will block until the generate operation is complete.

Opposite to uninstantiate function.

Specified by:
reset in class Randomness

read

public int read(ByteBuffer dst)
Description copied from class: Randomness
The generate function generates the requested random bits using the generate algorithm.

Transfers a sequence generated bytes from this RBG into the given buffer. An attempt is made to read up to r bytes from RBG, where r is the number of bytes remaining in the buffer, that is, buffer.remaining(), at the moment this method is invoked.

Suppose that a byte sequence of length n is read, where 0  <= n <= r. This byte sequence will be transferred into the buffer so that the first byte in the sequence is at index p and the last byte is at index p + n - 1, where p is the buffer's position at the moment this method is invoked. Upon return the buffer's position will be equal to p + n; its limit will not have changed. As many bytes as possible are transferred into each buffer, hence the final position is guaranteed to be equal to that buffer's limit. Also, a read operation might not fill the buffer, and in fact it might not read any bytes at all. Whether or not it does so depends upon the nature and state of the RBG.

This method may be invoked at any time. If another thread has already initiated a read operation upon this RBG, however, then an invocation of this method will block until the first operation is complete.

Calling uninstantiate function in interrupt this metod normally (without exceptions).

Specified by:
read in interface ReadableByteChannel
Specified by:
read in class Randomness
Parameters:
dst - The buffer into which random bytes are to be transferred.
Returns:
The number of bytes read from RBG, possibly zero, or -1 if the RBG has reached end-of-stream.

close

public void close()
Description copied from class: Randomness
The uninstantiate function zeroizes the internal state of RBG interrupting generate function, if proceed. Opposite to instantiate function.

Asynchronous closability

If RBG blocked on generate function than another thread can invoke RBG close method. This will cause to interrupt generate function without throwing any exceptions and close RBG. This method may be invoked at any time. If some other thread has already invoked it, however, then another invocation will block until the first invocation is complete, after which it will return without effect.

After a RBG is closed, any further attempt to invoke read operations upon it will cause a NonReadableChannelException to be thrown.

The closing status can be tested runtime via Randomness.isOpen() method.

The closed status of the RBG is cleared by reset function.

Specified by:
close in interface Closeable
Specified by:
close in interface Channel
Specified by:
close in class Randomness

toString

public String toString()
Description copied from class: Randomness
Returns the name of the algorithm (or process) implemented by this RBG object (optional operation).

In general case, toString description can contain all necessary information about RNG. If type of current RBG is undefined use UNKNOWN as type mark instead.

Specified by:
toString in class Randomness
Returns:
the name of the algorithm, or UNKNOWN if the algorithm name cannot be determined.

bakerTransformation

public Quasirandomess bakerTransformation()
Apply the baker's transformation to the output of this QRBG. It transforms each u∈[0, 1] into 2u if u <= 1/2 and 2(1 - u) if u > 1/2. *

The baker transformation is often applied when the QRBG is actually an iterator over a point set used for quasi-Monte Carlo integration.

Returns:
the baker transformation over this QRBG.
See Also:
Wikipedia - Baker's Map

tryRead

public int tryRead(ByteBuffer buffer)
Description copied from class: Randomness
Attempts to read from this RBG into the given buffer, if generate function is not owned by other thread.

This method behaves exactly as specified in the generate function except that returns immediately if generate function is owned by other thread.

Specified by:
tryRead in class Randomness
Parameters:
buffer - The buffer into which random bytes are to be transferred.
Returns:
The number of bytes read from RBG, possibly zero, or -1 if the RBG is owned by other thread.

minlen

public int minlen()
Description copied from class: Randomness
Minlen is the minimum block of bytes essentially produced per one iteration of generate function (optional operation).

This method may provide additional information about generator to configure generation process more essential for generator. For example, this method should used as follows:

Randomness rnd = Cryptorandomness.from(CSPRNG.SHA1);
ByteBuffer buffer = ByteBuffer.allocate(rnd.minlen() * MULTIPLIER);
rnd.read(buffer); // essential for generator. may be optimized.

Specified by:
minlen in class Randomness
Returns:
number of bytes generated by RBG per one iteration