Rectangle 27 4

Here is a Python implementation of the Linear Congruential Generator from FryGuy's answer. Because I needed to write it anyway and thought it might be useful for others.

import random
import math

def lcg(start, stop):
    N = stop - start

    # M is the next largest power of 2
    M = int(math.pow(2, math.ceil(math.log(N+1, 2))))

    # c is any odd number between 0 and M
    c = random.randint(0, M/2 - 1) * 2 + 1

    # M=2^m, so make (a-1) divisible by all prime factors and 4
    a = random.randint(0, M/4 - 1) * 4 + 1

    first = random.randint(0, M - 1)
    x = first
    while True:
        x = (a * x + c) % M
        if x < N:
            yield start + x
        if x == first:
            break

if __name__ == "__main__":
    for x in lcg(100, 200):
        print x,

algorithm - Generating shuffled range using a PRNG rather than shuffli...

algorithm language-agnostic random shuffle
Rectangle 27 3

Yes, it's absolutely easy to reverse engineer the number stream of a poorly designed pseudo random number generator, such as the Linear Congruential PRNG implementation in the Java programming language (java.util.Random).

In fact, with as few as TWO values from that particular generator, and the information on the order in which the values emerged, the entire stream can be predicted.

Random random = new Random();
long v1 = random.nextInt();
long v2 = random.nextInt();
for (int i = 0; i < 65536; i++) {
    long seed = v1 * 65536 + i;
    if (((seed * multiplier + addend) & mask) >>> 16) == v2) {
        System.out.println("Seed found: " + seed);
        break;
    }
}

This is precisely why it's critical to use cryptographically secure random number generators that have been vetted by the community at large for implementations that require security.

java.util.Random

PS, this should not be used as an architectural feature. I'm just posting this here so that it's known.

java - Is there a way to generate a seed out of a sequence of numbers?...

java random random-seed
Rectangle 27 3

Yes, it's absolutely easy to reverse engineer the number stream of a poorly designed pseudo random number generator, such as the Linear Congruential PRNG implementation in the Java programming language (java.util.Random).

In fact, with as few as TWO values from that particular generator, and the information on the order in which the values emerged, the entire stream can be predicted.

Random random = new Random();
long v1 = random.nextInt();
long v2 = random.nextInt();
for (int i = 0; i < 65536; i++) {
    long seed = v1 * 65536 + i;
    if (((seed * multiplier + addend) & mask) >>> 16) == v2) {
        System.out.println("Seed found: " + seed);
        break;
    }
}

This is precisely why it's critical to use cryptographically secure random number generators that have been vetted by the community at large for implementations that require security.

java.util.Random

PS, this should not be used as an architectural feature. I'm just posting this here so that it's known.

java - Is there a way to generate a seed out of a sequence of numbers?...

java random random-seed