why you can use a bag of M&Ms to find the love of your life

Today I wrote an adorable program. It simulated buying a bag of M&Ms and told you the probability of getting the color distribution of the M&Ms in that bag. There are three reasons I felt the need to share this story. One is because I am just learning R but have learned to program long ago. I have initial thoughts that I would like to express after trying to figure out a cute little algorithm for this problem. Second, I find it quite amusing that Mars Company actually lists a distribution of colors for the M&Ms in your bag. And third, I want you to now appreciate every bag of M&M’s that you will ever eat for the rest of your life.

First: Now after actually writing something useful in R instead of just typing command after command, I am still struggling with why this object-oriented language is so amazing. R actually drives me up the wall. It feels naked, bare, and exposed without any semicolons to end your statements. It feels like R people try to be hipsters by using “<-” instead of the “=” assignment. I mean, I guess you would look really silly if you used “=” in R and it is apparently bad etiquette anyways. But still, I’m not buying any sort of logical reason for this absurd-looking operator. There are no “+=” operators or “++”. This made me quite sad. The variables use “.” inside of them which is unnecessarily confusing looking. There are no types and every variable just feels like it can be whatever it wants. AND to top it all off, it doesn’t index to zero. I will show you a snip of my code in both R and C++ and you can see why R just makes me a bit on the uneasy side.

In R:

Screen Shot 2013-02-13 at 12.01.40 AM

In C++:

Screen Shot 2013-02-13 at 12.04.37 AM

Sigh, the comfort that is the semicolon. After reading up on some R coding etiquette, I was also quite sad to see that I must also deprive myself of the spaces in between the parentheses.

However, I do find myself being far too harsh. Iterating through an array (or vector, as R likes to call it) is a dream. Packages basically do all the heavy lifting for you. I literally wrote my program as fluently as I am writing this blog post. This is because I never once had to write a method for generating a random bag of M&Ms or calculating the multinomial distribution. It is already all there for you. Maybe some call it a crutch but I call it avoiding the reinvention of the wheel. That’s what open source is all about, right?

I also read quite a disheartening review of the R project. The author dealt with why you would put all this effort into a huge open-source project in the first place. I find it to be extremely rewarding, but if you think about it–it is hard to convince yourself at times what is really going to come about this in the first place. The paper mentions all these lovely things about R and then it goes to show that R will pretty much fall to shambles if the current group working on the project (basically just for fun-sies) either gets bored, or to put it bluntly, dies. Unless there is an army of successors, it seems to not be a sustainable solution to biologists in the future as it seems to be destined for dismal disassembly. Kind of like that show, Life After People.   But maybe I’m wrong and should place my faith in the goodness of humanity. I guess it is something to still keep in the back of your mind.

Second: Everyone should know that Mars Company states that every standard bag of M&M’s should have the following color distribution: 24% blue, 14% brown, 16% green, 20% orange, 13% red, and 14% yellow. Now if there is a smart-alec out there, they would notice that this does not add up to 100, but apparently stats is not their thing. Anyways, the point is that they made brown more awesome than red and that is just not true at all. And to think that there was life before the blue M&M! Remember when we all voted on what would be the next color of new M&M? (I’m still bitter about purple not winning.) They really should have made the blue M&M to begin with–it seriously appears that people cannot live without it.

Third: Every bag of M&M you ever eat should now be cherished with the utmost sacredness. The chances of you getting THAT bag with THAT distribution of color is so so so so small, that you should feel very special.

3 randomly generated bags of M&Ms

3 randomly generated bags of M&Ms

So basically, you know you will find your soulmate when you find another person with the exact same bag of M&Ms as you–as it would be an event so astounding that your’s and their’s stars would surely be in alliance!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s