Skip to content

Commit

Permalink
v0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
csoroiu committed Aug 15, 2017
1 parent ae434cc commit 9a1e46b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,31 @@

Also, besides their normal use, I have added functions to go backward. The **Mersenne Twister**
and **LCG**'s are reversible.
I got the idea of reversing the MT from here: [Cracking Random Number Generators - Part 3](https://jazzy.id.au/2010/09/22/cracking_random_number_generators_part_3.html).
Also, **LCG**'s and **subtractive generators** are trivial to revert.

I got the idea of reversing the generators from several places, after I had to break the seed of some generator, in order to reduce the size of the archived dataset.

## Disclaimer
*There are some cases in which the reverse does not work as expected. Try not to mix next and prev versions of the methods as you might run into strange situations.*
*Some of the situations are captured by unit tests which are currently marked as ignored.*
*Most of the time things go well.*

***Nevertheless, use it at your own risk. This library comes with no guarantees.***

## Usage
#### Maven dependency
```xml
<dependency>
<groupId>ro.derbederos</groupId>
<artifactId>untwist</artifactId>
<version>0.2</version>
</dependency>
```
#### Gradle dependency
```groovy
compile 'ro.derbederos:untwist:0.2'
```

Enjoy using them!

[build-status-svg]: https://travis-ci.org/csoroiu/untwist.svg?branch=master
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<groupId>ro.derbederos</groupId>
<artifactId>untwist</artifactId>
<version>0.2-SNAPSHOT</version>
<version>0.2</version>

<name>untwist</name>
<description>Java 8+ collection of PRNG's from CLR (.NET), FreePascal, TurboPascal, Python.</description>
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/ro/derbederos/untwist/CLRRandom.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ private void initialize(int seed) {

/**
* The reverse of {@link #sample()}.
* <p>
* Returns a random floating-point number between {@code 0.0} and {@code 1.0}.
*
* @return A double-precision floating point number that is greater than or equal to {@code 0.0},
Expand Down Expand Up @@ -323,6 +324,7 @@ public int nextInt() {

/**
* The reverse of {@link #nextInt()}.
* <p>
* Returns a non-negative random integer.
* <p>
* <font color="red">This violates the contract of {@link ReverseRandomGenerator#prevInt()}</font>
Expand Down Expand Up @@ -363,6 +365,7 @@ public int nextInt(int minValue, int maxValue) {

/**
* The reverse of {@link #nextInt(int, int)}.
* <p>
* Returns a random integer that is within a specified range.
*
* @param minValue the inclusive lower bound of the random number returned.
Expand Down Expand Up @@ -405,6 +408,7 @@ public int nextInt(int maxValue) {

/**
* The reverse of {@link #nextInt(int)}.
* <p>
* Returns a non-negative random integer that is less than the specified maximum.
*
* @param maxValue the exclusive upper bound of the random number to be generated. {@code maxValue} must
Expand Down Expand Up @@ -435,6 +439,7 @@ public double nextDouble() {

/**
* The reverse of {@link #nextDouble()}.
* <p>
* Returns a random floating-point number that is greater than or equal to {@code 0.0},
* and less than {@code 1.0}.
*
Expand Down Expand Up @@ -484,6 +489,7 @@ public void nextBytes(byte[] buffer) {

/**
* The reverse of {@link #nextBytes(byte[])}.
* <p>
* Fills the elements of a specified array of bytes with random numbers.
*
* @param buffer an array of bytes to contain random numbers.
Expand Down Expand Up @@ -515,6 +521,7 @@ public long nextLong() {

/**
* The reverse of {@link #nextLong()}.
* <p>
* Returns a 64 bit random integer (long). Unlike {@link #prevInt()}, all 2<sup>64</sup> possible
* {@code long} values should be produced with (approximately) equal probability.
*
Expand All @@ -541,6 +548,7 @@ public boolean nextBoolean() {

/**
* The reverse of {@link #nextBoolean()}.
* <p>
* Returns a random boolean.
* Uses the formula {@code nextInt(2) == 1}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public abstract class ReverseBitsStreamGenerator extends BitsStreamGenerator imp

/**
* The reverse of {@link #next(int)}.
* <p>
* Generate previous pseudorandom number.
* <p>
* This method is the core generation algorithm.
Expand Down

0 comments on commit 9a1e46b

Please sign in to comment.