www.heteroclinic.net
In this release R-2.2, we tried to repeat the same principle we did in C/C++ in Java. The effect is not quite satisfying by observation.
In my experiments, we have to force the threads sleep/context switch to add the entropy.
Mainly, this should relate particularly to JVM thread slicing settings. By observation, it causes a single thread dominate the shared flip.
In C++/C test, forcing threads to sleep/context switch thus yielding each other is not necessary.
We have to mention, the particular setting shown as below worked at a particular combo of OS, JVM and hardware.
You can tune the parameters so the results can be statistically satisfying.
Note: forcing the threads to sleep/context switch thus yielding each other significantly drags down the performance.
In contrast in C++/C test, forcing threads to sleep/context switch thus yielding each other is not necessary (but again, it is a given combo of conditions).
Again, you can find some key parameters to tune with by studying the source code.
This program due to its nature will not be of high performance. It is suggested to be used as a seeder program for other random generators, e.g. linear congruential random generator etc.
Source code:
Download
Compile
Execute
Introduction:
Race Condition Random Seeder Release 1.0 is a tool
aiming to create random seeds for random generators. It ulilizes modern
computer systems' high frequency CPU clock and multitasking in paralell
features. By creating man-made race condition, we retrieve a volatile boolean
value that threads race upon. The values retrieved thus form a bit-bundle
representing a numerical value.
The C++ implementation has four parts:
Part 1 is a test to validate the bit ops functions.
Part 2 is a test to validate the threading ops functionality.
part 3 runs a statistic or poll. In this part, we want to make sure the 'true'
or 'false' are distributed randomly but statistically evenly.
It is suggested to design more complex functions to do such test/poll. When the
system demonstrates stable behavior, it is time to retrieve the 'shared'
values.
The program will be affected by concrete systems' thread slicing settings,
system work load etc.
Like most engineering products, it needs a permitting condition. Thus we
programmed this part for the users' evaluation.
So there are some values to be tuned with:
- unsigned int sleepinmiddle = 0;
- unsigned int noofthreads = 3; // the more threads, the higher competition,
thus the randomness
- unsigned int start_interval =1000 ;
- unsigned int start_interval2 =1000 ;
- const unsigned int racer_sleep_value = 0;
It is suggested sleepinmiddle would be one more order bigger than
racer_sleep_value. In Cygwin environment, we set both as 0.
Part 4 is an example of how to get unsigned long long random values.
Each part should be compiled and run independently.
This program due to its nature will not be of high performance. It is suggested
to be used as a seeder program for other random generators, e.g. linear
congruential random generator etc.
Due to the limit of resources, this program is only tested in Cygwin
settings.
A Java implementation will come next.
We continue to verify the concept with the following prototypes:
A test prototype to verify the concept using Pthread.
A test prototype to verify the concept using Java.
The two programs would meet the expecation to provide randomness given we use usleep(C++) or TimeUnit.NANOSECONDS.sleep(Java) at the proper places and at a proper scale.
It was a little bit dis-encouraging to find RaceConditionRandomSeeder.cpp only works for Cygwin environment. After more experiments, we provide remedies.
We need some disambiguation about the term 'race condition'. You can better understand what this program tries to do from the following. You have a highly accurate watch. You want to get the time when you start to watch time. But the time you get is the time you finish the action, if you don't freeze/lock/synchronize the watch.
We make some fixes to make the program work under both Cygwin and Linux. So let it be an observation of what happened than sourcing/digging theoretical or technical documents, that a complete understanding or explanation is not in an immediate sight.
To be implemented otherwise (should) be trivia.
How to start only with Cygwin and Linux:
The source code:
We utilize some natural feature of CPU processor and OS.
I am not sure this program will survive all platforms all OSs.
But we are sure whenever there is a race condition, there would be contingency, thus randomness.
It may not perform as good as linear congruential random generator, but it provides a seeding solution.
How to start:
The source code: