How NumPy can help you with weirdos who accost you on the street with riddles

This is the first in a series of blog posts in which I discuss data science related topics with an overly familiar stranger. Here are parts 2 and 3.

So, the other day I was walking down the street, as one does, and this guy came up to me and was like...

Ayo my man, I got 24 balls in my pocket, right?
12 of them are black and 12 of them are red, right?
If you take out one, and then another, without replacing the first, what are the odds that they’ll both be the same colour?

That’s an oddly specific question, I thought to myself, but I was like whatever, it’s not as if I have any place to be.

So I went into my bag to get my computer out.

He didn’t like that.

Ayo my man, the answer ain’t in your computer.

Told you he didn’t like that.

Oh, but it is, I retorted.

I loaded up python, imported NumPy blah blah blah.

I then said to him, “you told me you had 24 balls in your pocket, right? 12 black and 12 red, correct?”

Yeah.

So kinda like this:

Yeah, but rounder

I’ll work on that for next time.

What kinda computer nerdery are you gonna do to answer my question?

It’s really not nerdy. It’s actually pretty easy. The computer does all the work for me.

I’m going to use this function called:

A function is a little piece of code that performs a task for you. The task this function does is if I give it something like a list, it’ll randomly select items from it for me.

Sounds like nerdery to me.

Anyhow, I’m going to create a variable (a variable is just something that holds a value), call it draws because I lack creativity, and set it to the list of balls you said weren’t round enough for your liking.

Then I’ll set the size to 2 since I’m taking 2 balls out of your pocket and set replace to False since I’m not replacing the ball I take out:

Ayo my man, do you know the answer or not?
Sounds to me like you stalin’

Homestretch.

All I have to do now is create a basic function that allows me to iterate that process as many times as I want. It'll count the number of times my draws are the same and divide that amount by the total attempts made.

I ain't got all day.

In that case, then, I’m going to say roughly 48%.

You mother...