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?