1. 程式人生 > >The RNG I Wrote To Pick Weekly Prize Winners

The RNG I Wrote To Pick Weekly Prize Winners

The RNG I Wrote To Pick Weekly Prize Winners

If you’ve been following my activity, you will notice that I recently launched a new project named “thisweek.club”. It’s a weekly hand-curated email newsletter for Web Developers. Every week, I hand-pick the top articles for Web Developers, all across the web, that sum up recent news, entertain, and educate. The goal is for it to become the email to look forward to every Friday. I think I’m well on my way to making that a reality.

Later on, I will write in detail about how the articles are picked but for now, I would like to talk about prizes. Each week when the issue is sent, I will also be sending a second email to a lucky subscriber detailing the prize that they won. Sure, this is a marketing effort to gain new subscribers but it’s also a way to give back to the community. The newsletter is completely free. I am still getting on my feet and was recently homeless. Check out my article titled

“My Story as a Homeless Developer” for more information on that…

So first, here’s more information on the potential prizes that subscribers could win. For the first issue, which sends out today (6PM EST, October 5th, 2018), I will be randomly selecting a subscriber and sending them a new paperback copy of

“High Performance Web Sites” and “RESTful Web APIs” (combined value ~$55). Currently, there are only around 30 subscribers, so odds are not too bad. If I am able, I will be sure to make each weekly prize just as valuable if not more valuable than the last.

Now onto the nitty-gritty…

“man wearing red long-sleeved shirt standing beside wall” by bruce mars on Unsplash

The answer to the question you have in your head is “No. I will not be simply generating a random number between 0 and number-of-subscribers” using the standard random function of my favorite programming language.

Python, for example, uses the Mersenne Twister algorithm which is widely used to generate pseudo-random numbers in software. The problem? According to the following article, it's completely predictable after 624 outputs. So technically speaking, after 624 weeks of prizes, if someone knew the number of subscribers (assuming that number remained the same), they could guess who would win each week. Now, to be honest, this isn’t a problem at all, as the subscriber list should grow and I likely wouldn’t continue for 624 weeks if it didn’t. Also, there is an assumption that people would unsubscribe during that time, which would change the position of users in the list. I just needed any reason at all to get a bit fancier. There would be no reason to read this article if I did not.

In the following page on Wikipedia, PCGs are explained. PCG stands for Permuted Congruential Generator. It’s a pseudo-random number generation algorithm developed in 2014. Basically, it applies a permutation function to linear congruential generators. That means that an LCG generates a state and the PCG uses parts of that states on itself to generate a more random state. You see…generating random numbers is hard! Technically speaking, we can’t generate truly random numbers at all. We can just develop software to create enough variables to make a prediction as hard as the situation permits, reasonably.

To sum that article up, deterministic algorithms aren’t for generating random numbers. It’s just common because of available resources to have our PRNGs pretend…poor things… Some RNGs take data such as atmospheric conditions and apply permutations to the output, which is a bit more complex and harder to predict than most RNGs. If you want to take a deep leap into the world of determinism and uncertainty, check out the following Wikipedia page on the Uncertainty Principle.

“silhouette of person standing on triangle window” by Florian Pérennès on Unsplash