7 kyu

PRNG: Linear Congruential Generator

247 of 457depperm

Description:

The Linear Congruential Generator (LCG) is one of the oldest pseudo random number generator functions.

The algorithm is as follows:

Xn+1=(aXn+c)modmX_{n+1} = (a \cdot X_n + c) \mod m

where:

  • a/A is the multiplier (we'll be using 2)
  • c/C is the increment (we'll be using 3)
  • m/M is the modulus (we'll be using 10)

X0X_0 is the seed.

Your task

Define a method random/Random in the class LCG that provides the next random number based on a seed. You never return the initial seed value.

Similar to random return the result as a floating point number in the range [0.0, 1.0)

Example

# initialize the generator with seed = 5
LCG(5)

# first random number (seed = 5)
LCG.random() = 0.3      # (2 * 5 + 3) mod 10 = 3 --> return 0.3

# next random number (seed = 3)
LCG.random() = 0.9      # (2 * 3 + 3) mod 10 = 9 --> return 0.9

# next random number (seed = 9)
LCG.random() = 0.1

# next random number (seed = 1)
LCG.random() = 0.5
# initialize the generator with seed = 5
LCG(5)

# first random number (seed = 5)
LCG.random() = 0.3      # (2 * 5 + 3) mod 10 = 3 --> return 0.3

# next random number (seed = 3)
LCG.random() = 0.9      # (2 * 3 + 3) mod 10 = 9 --> return 0.9

# next random number (seed = 9)
LCG.random() = 0.1

# next random number (seed = 1)
LCG.random() = 0.5
# initialize the generator with seed = 5
LCG(5)

# first random number (seed = 5)
LCG.random() = 0.3      # (2 * 5 + 3) mod 10 = 3 --> return 0.3

# next random number (seed = 3)
LCG.random() = 0.9      # (2 * 3 + 3) mod 10 = 9 --> return 0.9

# next random number (seed = 9)
LCG.random() = 0.1

# next random number (seed = 1)
LCG.random() = 0.5
// initialize the generator with seed = 5
LCG(5)

// first random number (seed = 5)
LCG.random() = 0.3      # (2 * 5 + 3) mod 10 = 3 --> return 0.3

// next random number (seed = 3)
LCG.random() = 0.9      # (2 * 3 + 3) mod 10 = 9 --> return 0.9

// next random number (seed = 9)
LCG.random() = 0.1

// next random number (seed = 1)
LCG.random() = 0.5
LCG myLCG = new LCG(5); // Initialize the generator with a seed of 5
myLCG.Random(); // => 0.3 ((2 * 5 + 3) mod 10 = 3)
myLCG.Random(); // => 0.9 ((2 * 3 + 3) mod 10 = 9)
myLCG.Random(); // => 0.1 ((2 * 9 + 3) mod 10 = 1)
myLCG.Random(); // => 0.5 ((2 * 1 + 3) mod 10 = 5)
Algorithms

More By Author:

Check out these other kata created by depperm

Stats:

CreatedJun 20, 2017
PublishedJun 20, 2017
Warriors Trained848
Total Skips16
Total Code Submissions1053
Total Times Completed457
Python Completions247
Crystal Completions11
Ruby Completions44
JavaScript Completions150
C# Completions58
Total Stars10
% of votes with a positive feedback rating85% of 142
Total "Very Satisfied" Votes110
Total "Somewhat Satisfied" Votes21
Total "Not Satisfied" Votes11
Total Rank Assessments11
Average Assessed Rank
6 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • depperm Avatar
  • GiacomoSorbi Avatar
  • anter69 Avatar
  • smile67 Avatar
  • donaldsebleung Avatar
  • aweleshetu Avatar
  • hobovsky Avatar
  • Just4FunCoder Avatar
  • saudiGuy Avatar
Ad