Pure Programmer
Blue Matrix


Cluster Map

Project: LCG Pseudo-Random Number Generator

The [[Linear congruential generator]] (LCG) [[pseudorandom number generator]] is the simplest and most studied way to generate a series of numbers that appear random.

The generator is defined by recurrence relation:

`X_(n+1) = (aX_n+c) mod m`

where `X_0` is the seed or starting value and a, c and m define the period and behavior of the generator. A bad choice of a, c or m results in poorly generated values that repeat too frequently or correlate in other ways.

Write a LCG function to generate pseudorandom numbers. It should maintain an internal state `X_n` as a 64-bit integer but return the low 31-bits as the result of the generator. The function should accept the multiplier (a), increment (c) and modulus (m) as arguments. Start with `X_0` as 3261963.

Write a program to test the function. The program should accept the multiplier (a), increment (c), modulus (m) and number of pseudorandom numbers to generate as arguments on the command line. Each time you call the generator print the raw value in hexadecimal and the floating point fraction in the range [0,1) computed using the raw value divided by `2^31`

Notice that over multiple runs with the same a, c, and m, you will get the same results. This is because the seed is always the same. How could we get a seed that would be different each run?

See [[Linear_congruential_generator#Parameters_in_common_use|Common LCG Parameters]] for some choices to use when testing the LCG.

Output
$ rustc LCGPseudorandomGenerator.rs error: unknown format trait `f` --> LCGPseudorandomGenerator.rs:36:44 | 36 | println!("{}", format!("{0:08x} => {1:.6f}", r, f)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error[E0425]: cannot find value `args` in this scope --> LCGPseudorandomGenerator.rs:16:5 | 16 | if args.len() != 5 { | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::env::args; | error[E0425]: cannot find function `exit` in this scope --> LCGPseudorandomGenerator.rs:19:3 | 19 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::process::exit; | error[E0425]: cannot find value `Utils` in this scope --> LCGPseudorandomGenerator.rs:21:27 | 21 | let mut multiplier:i64 = Utils.fromBase(args[1], 16); | ^^^^^ not found in this scope error[E0425]: cannot find value `args` in this scope --> LCGPseudorandomGenerator.rs:21:42 | 21 | let mut multiplier:i64 = Utils.fromBase(args[1], 16); | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::env::args; | error[E0425]: cannot find value `Utils` in this scope --> LCGPseudorandomGenerator.rs:22:26 | 22 | let mut increment:i64 = Utils.fromBase(args[2], 16); | ^^^^^ not found in this scope error[E0425]: cannot find value `args` in this scope --> LCGPseudorandomGenerator.rs:22:41 | 22 | let mut increment:i64 = Utils.fromBase(args[2], 16); | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::env::args; | error[E0425]: cannot find value `Utils` in this scope --> LCGPseudorandomGenerator.rs:23:24 | 23 | let mut modulus:i64 = Utils.fromBase(args[3], 16); | ^^^^^ not found in this scope error[E0425]: cannot find value `args` in this scope --> LCGPseudorandomGenerator.rs:23:39 | 23 | let mut modulus:i64 = Utils.fromBase(args[3], 16); | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::env::args; | error[E0425]: cannot find value `Utils` in this scope --> LCGPseudorandomGenerator.rs:24:25 | 24 | let mut count:isize = (Utils.fromBase(args[4], 16)) as isize; | ^^^^^ not found in this scope error[E0425]: cannot find value `args` in this scope --> LCGPseudorandomGenerator.rs:24:40 | 24 | let mut count:isize = (Utils.fromBase(args[4], 16)) as isize; | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::env::args; | error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields --> LCGPseudorandomGenerator.rs:35:40 | 35 | let f:f64 = (r) as f64 / 2147483648.f64; | ^^^ | help: if intended to be a floating point literal, consider adding a `0` after the period | 35 | let f:f64 = (r) as f64 / 2147483648.0f64; | + error: aborting due to 12 previous errors Some errors have detailed explanations: E0425, E0610. For more information about an error, try `rustc --explain E0425`.

Solution