r/dailyprogrammer • u/Coder_d00d 1 3 • May 19 '14
[5/19/2014] Challenge #163 [Easy] Probability Distribution of a 6 Sided Di
Description:
Today's challenge we explore some curiosity in rolling a 6 sided di. I often wonder about the outcomes of a rolling a simple 6 side di in a game or even simulating the roll on a computer.
I could roll a 6 side di and record the results. This can be time consuming, tedious and I think it is something a computer can do very well.
So what I want to do is simulate rolling a 6 sided di in 6 groups and record how often each number 1-6 comes up. Then print out a fancy chart comparing the data. What I want to see is if I roll the 6 sided di more often does the results flatten out in distribution of the results or is it very chaotic and have spikes in what numbers can come up.
So roll a D6 10, 100, 1000, 10000, 100000, 1000000 times and each time record how often a 1-6 comes up and produce a chart of % of each outcome.
Run the program one time or several times and decide for yourself. Does the results flatten out over time? Is it always flat? Spikes can occur?
Input:
None.
Output:
Show a nicely formatted chart showing the groups of rolls and the percentages of results coming up for human analysis.
example:
# of Rolls 1s 2s 3s 4s 5s 6s
====================================================
10 18.10% 19.20% 18.23% 20.21% 22.98% 23.20%
100 18.10% 19.20% 18.23% 20.21% 22.98% 23.20%
1000 18.10% 19.20% 18.23% 20.21% 22.98% 23.20%
10000 18.10% 19.20% 18.23% 20.21% 22.98% 23.20%
100000 18.10% 19.20% 18.23% 20.21% 22.98% 23.20%
1000000 18.10% 19.20% 18.23% 20.21% 22.98% 23.20%notes on example output:
- Yes in the example the percentages don't add up to 100% but your results should
- Yes I used the same percentages as examples for each outcome. Results will vary.
- Your choice on how many places past the decimal you wish to show. I picked 2. if you want to show less/more go for it.
Code Submission + Conclusion:
Do not just post your code. Also post your conclusion based on the simulation output. Have fun and enjoy not having to tally 1 million rolls by hand.
11
u/dongas420 May 19 '14 edited May 19 '14
Perl, also spits out a distribution graph. Wrote this one a while ago:
Output, using "1 6 10 100 1000 10000 100000 1000000" as arguments:
It evens out as the number of rolls increases.
Also works with any number of dice with any given number of faces. Using "5 8 100000", for instance:
6
u/Coder_d00d 1 3 May 19 '14
C
Example output:
Conclusion:
3
u/[deleted] May 19 '14
Doesn't this depend on the RNG? If the RNG's distribution is even, the outcome percents should all be pretty much same, right?
15
u/the_dinks 0 1 May 20 '14 edited May 20 '14
Just to spice things up, I did this in Snap!, which is essentially a port of Scratch.
Yes, Scratch, the thing you used in high school with the colorful blocks.
I had a chat with the high school teacher who introduced me to CS the other day about Scratch, which she uses in her classes (she was a CS major in the 80's, still one of the smartest people I know). We were talking about how easy it was for me to understand other languages because of how I didn't really have to learn syntax and programming concepts at the same time. You can do a lot in Snap!, and it even has some neat built in functions. However, they leave a lot out, which is probably intentional. But I digress.
WARNING: SPOILERS BELOW
Output:
Here is my output table.
Snap! doesn't really have anything resembling a "string" data type so this was the best I could do :/
One nice thing about Snap! is how my final work looked. Very tidy.
Code:
And here's the overall code
It uses several functions which I had to write, the first of which was a "power" function. I included negative integer powers as a second base case because I wanted to feel cool.
After completing that, it was easy to complete the rest. Here's the actual roll randomizer.
I also put the percentages calculator in another block because the code was getting crowded.
By using Snap!, I had to optimize my runtime as much as possible, which I don't have a lot of practice with. Even with recursion and mapping and the like, it still took at least fifteen seconds to generate the 111,110 rolls. Therefore, I didn't attempt to add a million more rolls to my tally, as my poor laptop was struggling with barely ~111k.
Analysis:
Looking at my output, it's clear that while the distribution somewhat flattens out, the RNG isn't truly random, which is to be expected. Thinking about percentages, those differences in tenths of a percent make up for large numbers when the number of rolls climbs. I didn't see any real patterns in the output over different sets of rolls, but I'm going to be reading the rest of the comments eagerly.
P.S: If anyone enjoyed seeing this, please let me know. I might do it again :^)
EDIT: I didn't want to manually copy down the output data, since Snap! has no I/O, as far as I can tell. Props to everyone who took the challenge to the next step and showed their output graphically.
23
u/skeeto -9 8 May 19 '14 edited May 19 '14
C99. Went for something a little different, showing a live curses plot rather than a table. Here's an animation of the output (for 3d6). It's intentionally slowed down (usleep) for the sake of animation.
It uses the OpenBSD arc4random() kernel entropy function to get cryptographic-quality random numbers.
6
u/dohaqatar7 1 1 May 20 '14
A simple task to complete. To have some extra fun, I made the program print the data in the format of a Reddit table.
4
u/snarf2888 May 19 '14
Solution in Javascript. Based on the results, if you roll enough, the probability of getting any one side over the other evens out and you have an equal chance of getting any side. To quote Joshua from WarGames, "The only winning move is not to play."
The output:
The code:
4
u/Puzzel May 19 '14 edited May 24 '14
I first made a processing script that displays an animated histogram and generates a gif.
Processing 2.1.2 script
GIF
But more to the point, I made a python script that does the rolls and plots a graph of both the standard deviation and the % rolls. It's from the graph that you can make a conclusion. The rolls start out uneven but quickly level and become more similar. Additionally, the decreasing standard deviation shows the percentages are converging towards a single value.
Python 3.4 script
Graph
I also then made a simple version that only outputs the roll chart.
Python 3.4 script
Output:
6
u/Edward_H May 19 '14 edited May 20 '14
COBOL.
EDIT: Added results on Lubuntu.
The code:
Output on Windows:
Output on Lubuntu:
Conclusion:
3
u/darthjoey91 May 19 '14
C++, maybe 11?
The output:
The code:
Conclusion:
3
u/dmv1975 May 26 '14 edited May 26 '14
I did mine in Python. I am not happy with how I kind of manually formatted the output and repeated several lines. I could have done it cleaner and shorter, but I didn't. Anyway, feedback appreciated:
https://gist.github.com/anonymous/be5753bff86b02458864
Edit: here is my output:
2
u/Boompje May 19 '14
Please read it through and criticise. I am new to C++ and would love to get better at it :).
2
u/mtko May 19 '14
C#. Not the most elegant of solutions, but it works well. As for a conclusion, there were occasional spikes even in the large sample size tests, but never more than 1/10th of a percent or so. So with any sufficiently large sample size, I would say that all numbers are weighted equally and any spikes are within the margin of error/chance.
2
u/CodeMonkey01 May 19 '14
Java. Percentages start to even out at 10,000 rolls.
2
u/TASER_NINJA May 20 '14
Pretty new to C++ so this might be terrible.
Code:
Output:
Conclusion:
2
u/[deleted] May 20 '14 edited May 20 '14
C
My first C program other than project euler and the K&R book, so tell me how it is. I mostly copied the example formatting exactly.
Output
The formatting was slightly messed up when pasting, I manually adjusted the amount of spaces to align it. It is aligned in the terminal, though.
The RNG of C on my machine looks to be good, Ther are no values that are much higher than any other values. The formatting does assume that a tab width on the terminal is 8 characters, but I think that value is fairly standard.
2
u/IceDane 0 0 May 20 '14
Here's my Haskell and C++ solutions. It was fun playing around with C++xx features for the first time in a long time, though using std::cout for formatting output is ridiculously painful. Likewise, the formatting in Haskell was not very pleasant and neither tables are very beautiful.
Both can be run with ./dprog163 pow10 sides
Haskell
Output
C++11. Please note that I have barely touched C++ for a couple of years, and I've only just begun taking a look at the newer features, and as such, I would definitely not mind critique.
Output
You all know the conclusion.
2
u/killedbythegrue May 20 '14 edited May 21 '14
I haven't done one of these for a while. So here is an erlang solution.
I decided to take the problem in a slightly different direction. So I wrote an erlang solution using concurrent programming. It runs 100 process each of which calculates 10,000 dice rolls, and does that three times.
1
u/KillerCodeMonky May 19 '14 edited May 19 '14
PowerShell. Yay for free table formatting. Dropped into .NET to generate the random numbers, as the
Get-Randomcommand has horrible performance in comparison.The sides and split-points are parameters, so go crazy. For instance, I'd recommend powers of six for the split points:
Results:
1
u/dijumx May 19 '14
In C - Might have tried to be too clever with a Uniform Random Number Generator, borrowed from here
Conclusion:
More rolls leads to better convergence on expectation of 1/6 for each side. Also, with a number of rolls not a multiple of the number of sides, the probabilities can never be exact.
Output:
Code:
1
u/r_s May 19 '14
C++11
1
u/pbeard_t 0 1 May 19 '14
C
Output
As expected all outcomes goes to approximately 1/6 once the samplesize gets large enough. Perhaps someone more versed in statistics can tell why that happens around n=1000.
Conclusion: rand() is random.
1
u/Gprime5 May 19 '14
Javascript
This is my first post and I'm still a noob at this so any criticisms and advice is always welcome.
Outcome:
==================================== 10 10.00% 30.00% 10.00% 20.00% 30.00% 0.00% 100 13.00% 21.00% 11.00% 19.00% 17.00% 19.00% 1000 18.90% 15.90% 16.20% 18.00% 14.80% 16.20% 10000 16.87% 16.69% 16.52% 17.21% 16.41% 16.30% 100000 16.73% 16.72% 16.49% 16.71% 16.81% 16.54% 1000000 16.73% 16.60% 16.71% 16.69% 16.61% 16.66%
1
u/Scroph 0 0 May 19 '14 edited May 19 '14
Written in D2 :
Output : http://i.imgur.com/eGc1E8F.png
Conclusion :
1
u/ehcubed May 19 '14
Python 3.3.2.
Code:
Sample Output:
Conclusion:
1
u/jeaton May 19 '14
JavaScript:
Output:
1
u/badgers_uk May 19 '14 edited May 20 '14
Python 3. Feedback is always welcome :)
Output:
conclusion:
edit: added ljust and rjust, and improved formatting of % after reading docs.
1
u/cobratbq May 19 '14
My solution in Go: (This is actually, my second one. In the first one I did one batch of die rolls up to 1000000 and reported intermediate results.)
It gives the following result:
Conclusion:
(I hope I'm doing this post correctly, since I haven't posted much on Reddit.) ... editing is really handy :-P
1
u/[deleted] May 19 '14
PHP: Code and Example Output. How can I format my code like the other people easily? It's my first post.
Conclusion
1
u/Hormander May 19 '14 edited May 19 '14
Python 3.3.
Code:
Output: (I put it on a picture because when I try to copy it here, it messes up the formatting)
http://imgur.com/8EHNPGa
1
u/GroverGoesToSpace May 19 '14 edited May 20 '14
Python 3.4.0
Output
All percentages converge on 16.66%.
Seems well.
Edit: added output and formatted to make similar to terminal output styling.
1
u/Godspiral 3 3 May 19 '14 edited May 19 '14
in J,
results
the conclusion is what I would have told you prior to experiment. Fewer trials leads to more variability.
better formatted version:
1
u/netgremlin May 20 '14 edited May 20 '14
Java.
First time poster, please leave feedback.
EDIT: How do I make a spoiler screen? EDIT: Thanks! It's behind a spoiler screen now.
1
u/Reverse_Skydiver 1 0 May 20 '14
Results:
While these results may seem messy, they provide me with the information I needed. I also recorded the time (shown in milliseconds) for how long each set of results took. I did not bother with rounding either. Here is the code I used:
As you can see, I made an object to represent a Die/Dice, which made this considerably easier.
It is clear that when 100 million rolls occur, the percentage for each value is almost the same at ~16.66% for each.
1
u/[deleted] May 20 '14 edited May 20 '14
My feeble attempt in C++
And here are my results:
Conclusion:
1
u/[deleted] May 20 '14
In QBASIC, compiled with FreeBasic (fbc -lang qb):
Results:
1
u/dangerbird2 May 20 '14 edited May 20 '14
Here is my attempt in C (gnu99 dialect), using GLib's random number gen utilities. Law of Large numbers triumphs yet again.
source:
output:
ninja: here's the makefile for gcc with pkg-config installed.
1
u/Moonwalkings May 20 '14 edited May 20 '14
C++ solution. The output format is messed up in reddit, and I dont know how to fix it:(
Output:
Conclusion:
1
u/Dizzant May 20 '14 edited May 20 '14
Python. I used this challenge as an opportunity to try out docopt, which pretty fantastic. I used a meta-function to generate a dice roller based on number of dice and number of sides per die. Source
here. UPDATE: Corrected source here. I guess it's time to get a GitHub account and actually use version control...Sample output:
Results are as the others have already mentioned:
Does anybody know a more pythonic way to repeat a function call? Line 22 in my source irks me.
1
u/chunes 1 2 May 20 '14 edited May 20 '14
Java.
My focus was on breaking up the logic into small, well-defined methods and commenting well. Any input about this stuff is especially welcome.
The conclusion I reach from the output is that the occurrences vary more wildly the fewer times you roll the die. With a six-sided die, the occurrence % for each side seems to settle on 16.66...%. Or more generally 100 / (number of sides).
1
u/fvandepitte 0 0 May 20 '14 edited May 20 '14
C#
Conclusion
1
u/itssalamitime May 20 '14
As the number of rolls increase, the distribution of rolls appears to even out.
Code:
Sample Output:
1
u/JerMenKoO 0 0 May 20 '14
My simple Python 3 solution:
1
u/spfy May 20 '14
So many people have posted already! I didn't do anything special, but here's another C solution. It's not as fast as it could be. I wanted the program to count how many rolls there were instead of passing more variables.
And what the output looks like:
1
u/minikomi May 20 '14
Racket using plot library in Dr.Racket:
Example output: http://imgur.com/CLu3EGb
1
u/srp10 May 20 '14
Java solution.
Sample Result:
1
u/abigpotostew May 20 '14
Lua! Lua is just so fun to program in. You can really make the language your own. My simulation output is to be expected. I do wish the output could compare bigger roll counts. Here's my solution duplicating OP's output.
1
u/lamiata May 20 '14
Still learning the language, so happy for any feedback. I'm not sure the way I'm formatting with setw() is the best.
Program output:
Conclusion:
C++ code:
1
u/travelerspb May 20 '14
R. And im realy noob.
I have problems with printing out. Tabulation works quite tricky in this language :(
1
u/kevn57 May 20 '14
R163Challenge_Easy
This was a good challenge for me about 30 days into trying to learn Python. I'm trying to learn more about dictionaries and I learned the hard way that I needed to copy the dictionary not just use an assignment statement. Also I wasn't sure how to access a dictionary element inside of a list, so it was quite profitable learning experience for me as I learned both.
As to my conclusion about the random data it was much as I suspeted, the more rolls the more evenly the die rolls were distributed across all 6 die. By the time it's up to 1 million rolls it's very close to the expected 16.67 expected.
1
u/kohai_ May 20 '14 edited May 20 '14
C++ I'm still learning, so harsh criticism would be much appreciated!
Code:
Results:
edit: formatting.
1
u/joeyGibson May 20 '14
Here's the code:
Here are the results:
Conclusions:
Spikes are pretty much assured at the low end of rolls. As the number of rolls increases, the spikes smooth out and the distribution gets fairly even.
A pretty-printed version of the code is available at https://gist.github.com/joeygibson/9d7dc6d85e38149a963f
1
u/thirdegree May 20 '14
Python 2.7, I didn't really feel like doing any formatting, so I just did everything in interactive prompt
1
u/cdombroski May 20 '14
Java code here, I'll probably do a Clojure one in a bit as well. I tried modifying the dice roll bit to see if I could invoke the bias noted in Effective Java, but no such luck with this result set. As expected, over time the distribution of outcomes equals their probability (i.e. the probability of each face is 1/6 and over time each face shows 1/6 of the time)
Code:
Output:
1
u/[deleted] May 20 '14
language: C# Please provide feedback.
The code:
The output:
The conclusion:
1
u/Ratheronfire May 20 '14
I've been doing a bit of C# lately, so I decided to put this together for practice:
Output:
1
u/Kiwi332 May 20 '14 edited May 20 '14
C# I took inspiration from u/dongas420 but I didn't rip it off and to be honest, it's not as impressive in my solution. :P
None the less, it did kill an hour and a bit of time I'd otherwise have done nothing with.
Output:
1
u/djhworld May 20 '14
Solution in golang
Output (needs cleaning up!)
Conclusion
More dice rolls = more equal distribution of probability of getting each roll, or closer to 16.66%
1
u/thecravenone May 20 '14
Perl:
Output:
Conclusion: As expected, the more rolls, the closer to even everything is
Notes:
1
u/dohaqatar7 1 1 May 21 '14
I'm back with another solution. This time it's in Batch. It's slow, very slow. It's so slow the code bellow only goes up to 1000 because it's to slow after that. Batch does not support floating point numbers, so I had to use a Haskell program to calculate the percents, but the last thing I wanted to was manually create a Haskell file, I did that from inside the batch file.
1
u/cooper6581 May 21 '14
Erlang:
Conclusion:
1
u/TieSoul 0 1 May 21 '14 edited May 21 '14
well, here's my Python solution. It accepts any amount of rolls, dice, and faces. Also brute force. And yes, I took a few suggestions from the output of some of the programs here (didn't look at the code)
output:
so the distribution flattens out as you get to higher amounts of rolls.
1
u/ftl101 May 21 '14
C++. I know I'm late but whatever, contributing!
And my output would be this.
1
u/JackyCore06 May 21 '14
Hello,
It is my first submission so I will appreciate feedbacks.
Python, here is my solution:
I also draw a chart of standard deviation from 1000 to 20 000 000 number of rolls with a step of 500 rolls (configurable):
I can't display image here, but you can generate it with the above code (matplotlib and numpy libs needed).
On the graph we can see that around 6 000 000 - 7 000 000, the standard deviation stop decreasing.
1
u/YouAreNotASlave May 22 '14
Flat out this week but here's mine for last Monday's challenge.
OUTPUT
1
u/redliness May 22 '14
Ruby
Lots of
ljustin there, I don't know of a better way to format stdout. But it works! By 10,000 throws every number has well under <1% variation.1
u/Fruglemonkey 1 0 May 22 '14
Made this while waiting for dinner to cook.
results:
Everything flattened out as expected.
1
u/_chebasitan May 22 '14
Thesis: The curve should level out as the number of rolls increases
This is my first program written in GO, and written while at work so I used GO Playground: http://play.golang.org/
Code in GO:
Output: Num: 10 14.000000000000002 ************** 22 ********************** 15 *************** 19 ******************* 14.000000000000002 ************** 16 ****************
Conclusion: Yes it did!
1
u/[deleted] May 22 '14 edited May 22 '14
Java
/* UI.java */
/* RollCounter.java */
/* Di.java */
Conclusion: Results even out the more times the Di is rolled.
1
u/CollegeBytes May 22 '14 edited May 22 '14
Ruby Solution
** CODE **
** OUTPUT **
1
u/[deleted] May 22 '14
VB.NET
Any criticism would be appreciated (This is a horrible mess but also my first ever VB code), I have to learn VB for a job interview so I thought I'd start off doing these challenges :D
1
u/things_random May 23 '14 edited May 23 '14
Java. First time posting my solution. Feedback would be greatly appreciated.
I added a last column with the standard deviation. The probability distribution significantly decreased between the lower numbers of rolls. std(10 rolls) > 10 and std(100 rolls) = ~3. By the time I got to 108 rolls the deviation was 0.01 so I stopped there.
}
Output:
1
u/Ir0nh34d May 23 '14
Sorry for the delay, but I really wanted to post this as I'm proud of it being my second submission and all!
Ruby Solution
1
u/oreo_fanboy May 23 '14
Python 2.7:
1
u/sobek696 May 24 '14
Golang solution
1
u/Saltyfork May 25 '14 edited May 25 '14
Python 2.7.6
I wouldn't call my chart "nicely formatted" but its functional and readable enough.
Please feel free to critique - I'm still very much in the early stages of learning how to code.
Output:
1
u/lmayo5678 May 25 '14
Java, didnt quite finish the output, but its late and i want to sleep :)
}
1
u/tmoravec May 25 '14
Learning Scheme (Racket). Any kind of comments welcome!
1
u/[deleted] May 26 '14 edited May 26 '14
I'm learning to use PythonTEX, and this seemed like a good opportunity to practice automating tables. I ended up with an attractive output:
http://imgur.com/YNLMwlX
And here is the code:
https://gist.github.com/anonymous/5d04611b5e3cd0bb3c86
These results show a bias toward 4!
1
u/noahcampbell May 26 '14
golang version
Results
Conclusion More rolls...the closer to the normal distribution the results become.
1
u/CaptainCa May 27 '14 edited May 27 '14
I aimed to solve the solution with minimal amount of code, memory allocation and library imports. It uses rand() from stdlib and gives the expected distribution values of 16.7% for each number. Here's my solution in C:
Output
[e] forgot to add output
1
u/tigershen23 May 30 '14
Ruby. Any and all feedback highly appreciated, if anyone's still here...
1
u/flightcrank 0 0 May 30 '14 edited Jun 01 '14
I get similar results to every one else
code: https://github.com/flightcrank/daily-programmer/blob/master/challange_163_e.c
output:
1
u/slowmyrole19 May 31 '14
first post here - I've only taken one java class and i just started my second one. I couldn't figure out how to round the doubles to two decimal places so i said fuck it.
}//closes class
output is the same as everyone else. around 16%
1
u/spiritpickle Jun 01 '14 edited Jun 01 '14
Late to the party but here's my Python solution using pandas.
1
u/solalmande Jun 06 '14
C#
1
u/salonabolic Jun 09 '14
C++
1
u/danneu Jun 11 '14
Clojure
Demo
1
u/VindictiveRakk Jul 07 '14
Kind of a weird way to do it, but hey, it works.
Code (Java):
Output:
1
u/[deleted] Jul 16 '14
Late for the party. C. Getting time in Mac OSX is hell.
Output:
Spikes are more likely the fewer rolls there are. More rolls = more normality.
1
u/kurtlocker Sep 17 '14
JavaScript. I think the readability is good here. Copy/paste this in your browser to test. Argument to probDist is an array of roll counts ie [10,100,1000,..,n]
1
u/ethnicallyambiguous May 19 '14 edited May 19 '14
Python 3.4
A larger number of rolls smooths out the distribution. This is expected from the law of large numbers. Sample output:
Code:
0
u/Frigguggi 0 1 May 19 '14 edited May 19 '14
Java:
Output:
0
u/toodim May 19 '14 edited May 19 '14
Python 3. More rolls causes the percentages to converge on the underlying probability of 1/6 or 16.6666%.
Output:
0
u/XenophonOfAthens 2 1 May 19 '14
Python 2.7, printing using format strings:
Result:
Edit: Conclusion:
0
u/mhalberstram May 19 '14
C# beginner here.
Conclusion: As the number of rolls increase, the likelihood of rolling a number gets close to being equal.
Output:
0
u/easher1 May 19 '14 edited May 19 '14
As the the number of rolls increase the distribution becomes more uniform. Newish to programming.Python 2.7. Let me know if you have any Suggestions!
output
0
u/glaslong May 19 '14 edited May 19 '14
C#
Seems like the .NET pRNG has a fairly even distribution across relatively large samples sizes.
Click here to run the code yourself!
code:
results:
0
u/greshick May 19 '14
py3
output
code
0
u/[deleted] May 19 '14
[deleted] — view deleted comment
0
u/[deleted] May 19 '14
[deleted] — view deleted comment