This project implements a random number generation program, printrandom
, which uses two types of random number generators: a Linear Congruential Generator (LCG) and a Mersenne Twister (MT). The program is designed to be compiled with a Makefile
, using shared code organization and dynamic linking.
printrandom
generates uniformly distributed random numbers in the range [0, 1)
, with support for two types of random number generators:
- Linear Congruential Generator (LCG)
- Mersenne Twister (MT)
The program accepts the generator type, count of numbers to generate, and a seed as command-line arguments. It then initializes the specified generator, produces the requested count of random numbers, and outputs them in a formatted manner.
To compile the program, ensure that all files are in the project directory, then run:
make
This will produce an executable called printrandom
. To clean up object files and the executable, run:
make clean
The program requires three command-line arguments:
- Generator type (
mt
orlinear
) - Number of random numbers to generate (integer between 0 and 100000)
- Seed for initializing the generator
./printrandom linear 5 12345
This example runs the program with the Linear Congruential Generator, generating 5 random numbers using the seed 12345
.
-
Linear Congruential Generator (LCG):
- Uses multiplier
1103515245
, increment12345
, and modulus2^31
. - Seed is specified as a positive integer, with only the lower 31 bits used.
- Uses multiplier
-
Mersenne Twister (MT):
- Uses a 32-bit Mersenne Twister algorithm.
- Seed is a 32-bit unsigned integer.
- As stated in the task, its implementation was taken from here
The RandomSource
interface allows seamless switching between different generator implementations. Each generator type implements:
double next(RandomSource *src)
: Generates the next random number.RandomSource *destroy(RandomSource *src)
: Cleans up and frees resources associated with the generator.
Each generator has a specific factory function to initialize it:
random_linear_factory
: Initializes an LCG generator.random_mt_factory
: Initializes an MT generator.
The main.c
file loads the correct factory function using dlsym
based on the command-line argument.
To generate 5 random numbers using the Mersenne Twister generator with seed 123456
:
./printrandom mt 5 123456
Expected output:
0.491462737
0.5018269304
0.2805587475
0.2943618261
0.347394317
Each line is a new random number formatted to 10 significant figures.