This post was kindly contributed by The SAS Dummy - go there to comment and to read the full post. |
WARNING: This blog post references Avengers: Infinity War and contains story spoilers. But it also contains useful information about random number generators (RNGs) — tempting! If you haven’t yet seen the movie, you should make peace with this inner conflict before reading on.
Throughout the movie, Thanos makes it clear that his goal is to eliminate half of the population of every civilization in the universe. With the power of all six infinity stones imbued into his gauntlet, he’ll be able to accomplish this with a “snap of his fingers.” By the end of the film, Thanos has all of the stones, and then he literally snaps his fingers. (Really? I kept thinking that this was just a figure of speech he used to indicate how simple this will be — but I guess it works more like the ruby slippers in The Wizard of Oz. Some clicking was required.)
So, Thanos snaps his huge fingers and — POOF — there goes half of us. Apparently the universe already had some sort of population-reduction subroutine just waiting for a hacker like Thanos to access it. Who put that there? Not a good plan, universe designer. (Check here to see if you survived the snap.)
But how did Thanos (or the universe) determine which of us was wiped from existence and which of us was spared? I have to assume that it was a seriously high-performing, massively parallel random number generator. And if Thanos had access to 9.4 Maintenance 5 or later (part of the Power [to Know] stone?), then he would have his choice of algorithms.
(Tony Stark has been to SAS headquarters, but we haven’t seen Thanos around here. Still, he’s welcome to download SAS University Edition.)
Your own RNG gauntlet, built into SAS
I know a little bit about this topic because I talked with Rick Wicklin about RNGs. As Rick discusses in his blog post, a recent release of SAS added support for several new/updated RNG algorithms, including Mersenne twister, PCG, Threefry, and one that introduces hardware-based entropy for “extra randomness.” If you want to save yourself some reading, watch our 10-minute discussion here.
Implementing my own random Avengers terminator
I was going to write a SAS program to simulate Thanos’ “snap,” but I don’t have a list of every single person in the universe (thanks GDPR!). However, courtesy of IMDB.com, I do have a list of the approximately 100 credited characters in the Infinity War movie. I wrote a DATA step to pull each name into a data set and “randomly decide” each fate by using the new PCG algorithm and the RAND function with a Bernoulli (binomial) distribution. I learned that trick from Rick’s post about simulating coin flips. (I hope I did this correctly; Rick will tell me if I didn’t.)
%let algorithm = PCG; data characters; call streaminit(2018,"&algorithm."); infile datalines dsd; retain x 0 y 1; length Name $ 60 spared 8 x 8 y 8; input Name; Spared = rand("Bernoulli", 0.5); x+1; if x > 10 then do; y+1; x = 1;end; datalines; Tony Stark / Iron Man Thor Bruce Banner / Hulk Steve Rogers / Captain America /* about 96 more */ ; run; |
After all of the outcomes were generated, I used PROC FREQ to check the distribution. In this run, only 48% were spared, not an even 50%. Well, that’s randomness for you. On the universal scale, I’m not sure that anyone is keeping track.
Using a trick I learned from Sample 54315: Customize your symbols with the SYMBOLCHAR statement in PROC SGPLOT, I created a scatter plot of the outcomes. I included special Unicode characters to signify the result in terms that even Hulk can understand. Hearts represent survivors; frowny faces represent the vanished heroes. Here’s the code:
data thanosmap; input id $ value $ markercolor $ markersymbol $; datalines; status 0 black frowny status 1 red heart ; run; title; ods graphics / height=400 width=400 imagemap=on; proc sgplot data=Characters noautolegend dattrmap=thanosmap; styleattrs wallcolor=white; scatter x=x y=y / markerattrs=(size=40) group=spared tip=(Name Spared) attrid=status; symbolchar name=heart char='2665'x; symbolchar name=frowny char='2639'x; xaxis integer display=(novalues) label="Did Thanos Kill You? Red=Dead" labelattrs=(family="Comic Sans MS" size=14pt); /* Comic Sans -- get it ???? */ yaxis integer display=none; run; |
For those of you who can read, you might appreciate a table with the rundown. For this one, I used a trick that I saw on SAS Support Communities to add strike-through text to a report. It’s a simple COMPUTE column with a style directive, in a PROC REPORT step.
proc report data=Characters nowd; column Name spared; define spared / 'Spared' display; compute Spared; if spared=1 then call define(_row_,"style", "style={color=green}"); if spared=0 then call define(_row_,"style", "style={color=red textdecoration=line_through}"); endcomp; run; |
Remember, my results here were generated with SAS and don’t match the results from the film. (I feel like I need to say that to preempt a few comments.) The complete code for this blog post is available on my public Gist.
Learn more about RNGs
Just as the end of Avengers: Infinity War has sent throngs of viewers to the Internet to find out What’s Next, I expect that readers of this blog are eager to learn more about these modern random number generators. Here are the go-to articles from Rick that are worth your review:
- How to use the new random-number generators in SAS
- Independence and overlap in streams of random numbers
- Runs in coin tosses; patterns in random seating
- Head-tail versus head-head: A counterintuitive property of coin tosses
Unanswered questions
Before Thanos completed his gauntlet, his main hobby was traveling around the cosmos reducing the population of each civilization “the hard way.” With the gauntlet in hand when he snapped his fingers, did he eliminate one-half of the remaining population? Or did the universe’s algorithm spare those civilizations that had already been culled? Was this a random sample with replacement or not? In the film, Thanos did not express concern about these details (typical upper management attitude), but the grunt-workers of the universe need to know the parameters for this project. Coders need exact specifications, or else you can expect less-than-heroic results from your infinity gauntlet. I’m pretty sure it says so in the owner’s manual.
The post Which random number generator did Thanos use? appeared first on The SAS Dummy.
This post was kindly contributed by The SAS Dummy - go there to comment and to read the full post. |