Information

Author(s) Erik-Jan Lingen, Matthias Möller
Deadline Keine Frist
Submission limit No limitation

Sign in

[Threads] Solution 9 : Thread-safe Random Number Generator

This material is part of the course Practical Introduction to Parallel Programming and meant for educational purposes only.


Task

Implement a thread-safe pseudo random number generator using thread-local storage.

Each thread should generate at least ten million random numbers. Run your program with at least four threads.

Verify in a statistical way that all threads generate the same sequence of pseudo random numbers.

Here is a simple pseudo random number generator:

unsigned long  rand_state = 1;

int calc_random ()
{
  rand_state = rand_state * 1103515245 + 12345;

  return ((unsigned) (rand_state / 65536) % 32768);
}

Hints

The variable rand_state can not be a local variable; its value must be preserved between calls to the function calc_random.

Take a small number of samples (ten, say) to verity that all threads have generated the same sequence.

What happens when you do not use thread-local storage?


Question 1: Thread-safe Random Number Generator
Question 2: Command Line Argument

Specify the command line arguments that should be passed to your program when it is run. Arguments must be given in quotes and separated by commas. Leave this filed empty if you do not want to specify command line arguments.

Example: "1","int","arg=2" is interpreted as three arguments 1, int, and arg=2.