Questions tagged [lcg]

A linear congruential generator (LCG) is an algorithm that yields a sequence of pseudo-randomized numbers calculated with a discontinuous piecewise linear equation.

The linear congruential generator LCG is defined by recurrence relation:

    equation

where:
    X is the sequence of pseudo-random values
    m is the modulus (m > 0)
    a is the multiplier (0 < a < m)
    c is the increment (0 <= c < m)
    X0 is the "seed" or start value (0 <= X0 < m)

45 questions
14
votes
7 answers

Why is the use of rand() considered bad?

Usage of rand() is usually frowned upon despite using a seed via srand(). Why would that be the case? What better alternatives are available?
Uncertainly Certain
  • 357
  • 1
  • 4
  • 14
10
votes
3 answers

why 48 bit seed in util Random class?

Why this class uses 48 bit seed in its linear congruence formula? I would have expected 32 or 64... I know it takes higher order bits when asked for 32 bit values. But why only 16 more additional bits? Was it a "random" choice?
Fakrudeen
  • 5,778
  • 7
  • 44
  • 70
8
votes
4 answers

Is Lightweight Code Generation (LCG) dead?

In the .NET 2.0-3.5 frameworks, LCG (aka the DynamicMethod class) was a decent way to emit lightweight methods at runtime when no class structure was needed to support them. In .NET 4.0, expression trees now support statements and blocks, and as…
Greg Beech
  • 133,383
  • 43
  • 204
  • 250
5
votes
2 answers

How to generate a predictable shuffling of a sequence without generating the whole sequence in advance?

The following python code describes exactly what I want to achieve for a sequence of arbitrary size (population): import random fixed_seed = 1 #generate the same sequence every time with a fixed seed population = 1000 sample_count = 5 #demonstration…
Russ
  • 10,835
  • 12
  • 42
  • 57
5
votes
3 answers

pseudo random distribution which guarantees all possible permutations of value sequence - C++

Random question. I am attempting to create a program which would generate a pseudo-random distribution. I am trying to find the right pseudo-random algorithm for my needs. These are my concerns: 1) I need one input to generate the same output every…
Jonathan Basile
  • 649
  • 1
  • 10
  • 20
4
votes
1 answer

Speeding up modulo operations in CPython

This is a Park-Miller pseudo-random number generator: def gen1(a=783): while True: a = (a * 48271) % 0x7fffffff yield a The 783 is just an arbitrary seed. The 48271 is the coefficient recommended by Park and Miller in the…
wim
  • 338,267
  • 99
  • 616
  • 750
4
votes
2 answers

Custom linear congruential generator in JavaScript

I am trying to create a custom linear congruential generator (LCQ) in JavaScript (the one used in glibc). Its properties as it's stated on Wikipedia are: m=2^31 , a=1103515245 , c=12345. Now I am getting next seed value with x = (1103515245 * x +…
Stano
  • 8,749
  • 6
  • 30
  • 44
3
votes
1 answer

Linear congruential generator - how to choose seeds and statistical tests

I need to do a linear congruential generator that will successfully pass the selected statistical tests. My question is: how to choose numbers for the generator properly and which statistical tests should I choose? I thought about: Chi-Square…
tbone
  • 1,148
  • 5
  • 19
  • 35
3
votes
1 answer

On-the-fly pseudo-random permutation of very large set without repeating and with inverse operation

I have a very large set of values (0-300000^700) and I would like to find an algorithm which would bijectively assign a unique value within the same set. It is the equivalent of a permutation, but because of obvious memory problems, that would have…
3
votes
1 answer

Maple: RNG is not random

i was "finding Pi" with Monte Carlo Method, but the answer was incorrect. The oryginal code was: RandomTools[MersenneTwister]: with(Statistics): tries := 10000: s := 0; for i to tries do if GenerateFloat()^2+GenerateFloat()^2 < 1 then s :=…
Z-DNA
  • 133
  • 1
  • 10
2
votes
2 answers

Length of r array

I try to return the Y array from the second loop. I expect it to be (n - [k + 2]) length but I get output of n length array a1q1 <- function (n , j , k , m, X0) { X <- numeric(length=n) Xn <- X0 for (i in 1:n) { Xn <- (103515245*Xn +…
2
votes
1 answer

Unable to understand the code for linear congruential generator

I am making another python script to perform linear congruential generator to generate 55 pseudo-random number but having trouble understanding the algorithm of linear congruential generator and how does my scripts work even though the script is…
2
votes
1 answer

Linear congruential generator gives wrong output

I created a linear congruential generator (LCG), but it appears to give me the wrong output. // Instance variables private long currentRandomNumber; private long a; private long c; private long m; public static void main(String[] args) { //…
Wilko
  • 41
  • 4
2
votes
1 answer

Making a customizable LCG that travels backward and forward

How would i go about making an LCG (type of pseudo random number generator) travel in both directions? I know that travelling forward is (a*x+c)%m but how would i be able to reverse it? I am using this so i can store the seed at the position of the…
2
votes
1 answer

how does java.util.random work?

In order to understand how java.util.random works, I wrote a piece of simple code to simulate the java random functions and compared the results of java random function's and my function's. However, the results are different. It means either I made…
Alan Wu
  • 115
  • 9
1
2 3