Update: October 6, 2025: Thanks to everyone who participated in this challenge! It was very interesting to see the diverse approaches to random number generation.
Congratulations to the following users for completing the challenge: Nevpzo, Rohit kumar, MNBLabs, Nanigashi, Herry Poter, Hmwat, Syedali Rizvi, MUGWANEZA MANZI Audace, JGK, Tomas Langkaas, Jérôme Migné, Tomodovodoo, DannyNiu
Original Challenge post
Today's challenge is about random numbers. Programming languages usually contain functions to generate random numbers, but in this challenge, we will create our own. And then, we'll measure how random the output really is.
A good approach to generating pseudo-random numbers is a described in this Stack Overflow answer. It uses a linear congruential generator (LCG) algorithm.
The Challenge
Create a Random Number Generator (RNG) using the LCG algorithm (or another of your choice).
For these algorithms, we need some starting inputs to create randomness. You can use your creativity to find the inputs which are somewhat non-deterministic. Some options could be:
user input based, like the time between keystrokes, mouse movement or similar
time based, like the time required to do something like ping a server, write a file, etc.
other ideas, per your creativity
Using this RNG, generate 100,000 random integers with values between 0-100.
Then test the distribution for randomness in two ways:
Shannon Entropy test: This test measures the distribution of the results, i.e., are values uniformly distributed among the possible set of values. An example of how to implement this test is in this Stack Overflow answer. (Follow the discrete distribution approach.) For random numbers from 0-100, the lowest entropy would be 0 and the highest entropy would be around 6.6 bits.
Data compressibility test: Testing for uniform distribution is not a sufficient criteria for randomness. If the RNG produced an output of [0, 1, 2, 3 ...100, 0, 1, 2...] it would have a uniform distribution, but it wouldn't be very random. One way to check for this is by looking at how well the data compresses. Truly random data has no predictable patterns, so it is hard to compress. Data with sequential or repeating patterns compresses much more, revealing that the sequence isn’t truly random. To do this test, compress the data using a DEFLATE algorithm available in most programming languages, and compute the ratio of the size of the compressed and uncompressed data. Ideally we get a ratio close to 1.0, meaning that the data could not be compressed.
Key dates
You have two weeks from the date this challenge is posted to submit your entry.
September 19: Challenge goes live
October 3: Challenge is complete and we'll announce winners soon after. We plan to recognize all participants that complete the challenge.
While we will be recognizing all of the complete entries, we still encourage participants to vote on the responses. We recommend upvoting solutions that have good randomness metrics and/or creative approaches to the problem.
How to Submit
Enter your submission in the text box below.
Your submission should include:
A description of how you created the random numbers and your results on the entropy and data compression tests
The code you used to create the random numbers
[Optional] Anything you learned or any interesting challenges you faced while coding
Your entry is not permitted to be written by AI. For any feedback on this Challenge, please head over to the Meta post.
uses the random module and a while loop to create 100,000 random numbers