From the Stupid PHP Tricks files: rounding numbers and creeping inaccuracy

This morning as I walked to the studio I was doing what geeks do best: pondering a slightly esoteric mathematical quandary.

Glass Half Full by S_novaIngraining the American spirit of optimism at a young age, and under dubious circumstances, our schools always taught rounding numbers in a peculiar way. You always round your decimal values to the nearest integer. That part makes sense. But what if the decimal is .5 — exactly half? In my education, at least until late in high school (or was it college?), we were always taught to round up! The glass is half full. Optimism.

Eventually — far later than it should have been, I think — the concept was introduced that always rounding .5 up is not really that accurate, statistically speaking. It might be nice in the case of a single number to be an optimist and think a solid half is good as a whole, but in aggregate this thinking introduces a problem.

If you have a whole lot of numbers, and you’re always rounding your halves up, eventually your totals are going to be grossly inaccurate.

Of course, the same would happen if you were ever the pessimist and always rounded down.

The solution, I later learned, was to round halves up or down, depending upon the integer value that precedes them. Which way you go doesn’t really matter, as long as you’re consistent, but as it happens, I learned it as such: if the integer is odd, round up; if it is even, round down.

In my work, I write a lot of PHP code. Most of it is of the extremely practical variety; I’m building websites for clients, after all. But every once in a while I like to indulge my coding abilities in a bit of frivolous experimentation, and so today I produced a little PHP script that generates 10,000 random numbers between 1 and 100, with one decimal place, and then it shows the actual sum and average of those numbers, along with what you get as the sum and average if you go through all 10,000 numbers and round them to whole integers by the various methods described above. Try it for yourself!

Any time the rounded average is different from the “precise” (and I use that term somewhat loosely) average, it is displayed in red. Interestingly, and not at all surprisingly, when you always round halves in one direction or the other, at least one of those directions will (almost) always yield an incorrect average. Yet if you use the “even or odd” methods, both of those methods will almost always yield a correct average.

It’s all about the aggregate.

Into the storm

It began like many other Friday evenings. Our kids were on their way to “Kids’ Night Out,” a 3-hour activity at Lake Hiawatha Park, about a mile from our house. It was going to be a more exciting evening than most, as the whole group was going to walk over to nearby Lake Nokomis Park for a small neighborhood carnival-type event that was going on there.

Meanwhile, SLP and I did what we often do for those few hours: got takeout from a Thai restaurant and went home to watch some Netflix. (What can I say? We’re well on our way to old and boring. It’s even worse if you know what we watched.)

All indications for us were that there might be some rain on the way, and we thought it might force the carnival indoors. Unfortunate, but… oh well.

Around 7:40, just as our show was ending, we got a couple of weather alerts on our iPhones — not the usual Notification Center alerts from our weather apps of choice, but the serious government/emergency alerts the cellular networks send out for things like kidnappings or natural disasters.

There were two alerts, in fact: a Flash Flood Warning and a Severe Thunderstorm Warning. I don’t worry too much about floods where we live (though I realize a flash flood is different), and although the skies had definitely darkened considerably, it wasn’t even raining yet at our house.

We took a look at the weather maps, saw a bright patch of red and orange heading our way, and decided we’d better hurry off to the park to pick up the kids. It looked like we had about 20-30 minutes before the storm would hit.

Turns out, it was just a little bit less than that.

By the time we backed the car out of the garage at about 7:45, it had started raining, and by the time we had gone the mile to Lake Hiawatha (calculating that they had either canceled their plans to go to Lake Nokomis, or that they would have already returned by now) the torrents were unleashed and the trees were whipping in the wind.

We were drenched as we ran to the door of the park building. Locked. No one inside. Damn. They were at Lake Nokomis after all.

The storm was coming on fast, so we ran back to the car and prepared to high-tail it to Lake Nokomis, a trip that was made considerably longer by the fact that the main street between the two parks, 28th Avenue, has been torn up for weeks while CenterPoint installs new gas lines.

But that didn’t really matter. By the time we had gone a block and a half from the park, cars ahead of us at the intersection of 42nd Street abruptly stopped. Cars going on 42nd Street were stopped too, pointing in various unexpected directions.

Then, I saw it. Just ahead on the next block, a large piece of a tree — larger than the small Prius we were driving in, in fact — flew across the street about 20 feet in the air. I looked down 42nd Street and saw more trees down in the road.

Cars around us were stopped, but I had no intention of staying there! I carefully made my way around the obstacles — stopped vehicles and giant tree limbs — and drove on another block. I turned and raced down 41st Street. At least I think it was 41st Street. I wasn’t really taking the time to look.

28th Avenue was still under construction at that point, not that it would have mattered, because half a block away a giant tree was downed on top of the construction equipment, blocking the entire street.

I went on another block, and then another, turning down streets and then turning back as one street after another became impassable. All the while I was looking up, trying to make sure nothing was about to fall on us.

At one point we arrived at a dead-end street that we thought was a street some friends of ours live on, and we prepared to go to their door, but it wasn’t their street after all, so we turned back.

Beginning to panic, we considered going to the door of one of the houses in the area that had lights on, but it didn’t seem like the right choice. While I was sure (or at least hopeful) they’d take us in, then what? Our car might have a tree land on it; we might be stuck there. Then how would we get to the kids once the storm passed?

Somehow having gotten to this point without colliding with another car, flying trees, or getting a flat tire from driving over all of the debris, I noticed we were by a school. What school, I wasn’t quite sure, and didn’t care to figure out. I realized in an instant that we were on the side of the building sheltered from the storm, and that there were no trees around. So I raced across the school’s parking lot, up near the building, where I found a small, fenced-in alcove where a number of other cars were parked. We drove through the open gate and I tucked the car into a corner.

And we waited.

We were wet, and cold, and shivering more from fear than from anything else. We turned on the radio, and listened as the announcers cracked jokes about the irony of how the storm was making it so dark on the longest day of the year.

“Sure,” I thought. “You’re comfortable and safe in your studio in downtown St. Paul. You have no idea what it’s like to be in the middle of this.”

We turned the radio off.

We checked our weather apps. We wanted to call the park, or hoped they’d call us, but in our haste to leave home SLP had left her phone on the kitchen counter, and it was her number the park staff had. Well, that and my old cell number, which now rings at my office.

At this point I finally pieced together the clues that we must be at Roosevelt High School. I could see the lights of the football field peeking up over the side of the building. I realized that all of the other cars parked around us — empty — in the fenced-in area were neither storm refuge seekers nor school employees’ cars. We were in front of the school’s auto shop.

A few minutes later I heard a roar of an engine behind us. Someone in a black sports car was in the parking lot, squealing tires, racing back and forth, doing donuts. At once I thought both that I wished I was as carefree in the moment as they were, but also that it was idiots like that who go out in these kinds of storms and get themselves hurt or killed. We continued to cower in our little Prius, hazard lights flashing, tucked into our relatively safe little corner by the Roosevelt High School auto shop.

The National Weather Service had issued a Severe Thunderstorm Warning for our area until 8:15. We had about 15 minutes to go. I kept turning around and looking at the large tree across the street from the school, maybe 100 feet away. It was my indicator of the current wind speed. Every time its movements would slow, we’d start to wonder if, despite the still-pounding rain, maybe things had tapered off enough that we could either get home or get to the park to pick up the kids. But before we could act, the tree whipped up into a frenzy again.

So we waited.

Even in my fear and panic, I was still myself. There were two doors to the school, a few feet from each other, directly in front of us, numbered 11 and 12. I noticed, and pointed out to SLP, that the “11” was in Arial and the “12” was in Helvetica. It’s the ones. I hate how ones look in Arial. I was ready to take a picture of it to post on Instagram, but I thought better of it. SLP was shaking and upset — and so was I — and I think there was maybe even a brief flash in my mind of, “What if the worst happens to us tonight? Is this really the last statement I’d want to leave the world?”

At one point the weather seemed to have calmed enough that we could leave, but as soon as I turned the corner and was beyond the protective wall of the school, we realized that this was not over.

8:15 finally came, but the weather didn’t seem done with us yet, so we waited a few more minutes. At 8:20 we finally pulled out of the parking lot and headed towards… well, where should we go? And where could we go? We debated going home and waiting out the rest of the storm, or going to Lake Nokomis to find the kids.

We decided that we needed to go home first, because who knew if this was over, whether we’d even be able to get to the kids, etc. We knew the kids were far safer in a fallout-shelter-grade Minneapolis park building than we were in our car. I was also hoping the park staff might have left a message for us on SLP’s phone, and I wanted to at least be able to get word to them that we were OK.

I managed to navigate by a circuitous and mildly treacherous route back to 42nd Street, where this all had begun for us. In addition to the large branches blocking part or all of some streets, we noticed several uprooted trees, tipped against buildings. All of the construction signs on 28th Avenue were flattened. 42nd Street itself was relatively clear, so we got back to 34th Avenue and headed towards our house. At one point I had to accelerate and pass someone who apparently was out for a leisurely drive. As we turned onto Minnehaha Parkway, we noticed more damage, although not as bad as what we had seen a half mile to the north, until we got to the house on the corner of our street and noticed its front boulevard tree, a very large, old tree, was uprooted and on the house. At least the house appeared not to have sustained much damage, but something this close to our house? What did that mean for us?

We were lucky. We had a few large tree branches down on our driveway, but our trees were still standing and our house was intact. We got out of the car and cleared a path to the garage, parked the car and ran into the house.

I checked SLP’s phone. Two messages from the park staff, one of which had been left within a minute of us leaving the house. I called the park and let them know we were OK — and exactly what had ensued over the past 45 minutes — and that we were on our way.

I elected to take 50th Street to Lake Nokomis and loop around the back of the park building, and I’ve never so greatly exceeded the speed limit on 50th Street in my life. We were at the park within 3 minutes, and I was glad I’d taken the route I had. Just to the west, the parkway was blocked completely by a large tree, and there were a few uprooted trees in the park by the building where the kids were waiting for us. I’m not sure we’d have even been able to get through if we’d gone another way.

We ran into the building, embraced the kids, and realized that neither they nor the park staff, who greeted us with smiles and laughter, had any idea of the extent of what had just happened, nor what it felt like to witness it through a car windshield.

We rushed the kids into the car and raced back home. More storms were on the way, we had seen, and we weren’t about to repeat the last hour.

At 8:45, we were home. It was over. Barely more than an hour had passed, but it’s an hour that will be burned into my memory for years to come.

I know now just how lucky we were. We were in the middle of the storm’s path of destruction, as it was happening, in a tiny car. We’re safe, our car is fine, our house is unscathed. Many others in the city and the surrounding metro area weren’t so lucky. I haven’t heard any reports of deaths or injuries, but there’s plenty of damage, and hundreds of thousands of people were without power for at least part of the night. There were gas leaks and fires.

And there were tireless and fearless first responders — police, firefighters and EMTs — not to mention crews from Xcel and CenterPoint who were on the scene restoring power and fixing gas lines. I have rarely had more appreciation for their dedication and courage than I did last night.

This morning, SLP and I took a walk around our neighborhood to survey the damage. Our neighborhood was lucky… the damage was significantly worse about a mile to the north, and also farther to the east. But these photos show how bad it was even within a half mile of us.

jpeg

jpeg-1

jpeg-2

The Raspberry Pi Arcade Project, Part 2: The Essential Gear

The first thing you need when building your Raspberry Pi Arcade is… well… a Raspberry Pi. When they were first released, they were hard to come by, but now they’re readily available in the U.S., for about $45, on Amazon.com.

Unfortunately the Raspberry Pi, by itself, is completely useless. Fortunately, the other stuff you need to make a Raspberry Pi work is also fairly inexpensive and readily available on Amazon, so with an expenditure of about $150 and a few days’ wait for UPS to drop everything at your door (and a TV, which I’ll assume you have), you’ll have your complete Raspberry Pi set-up.

While this blog series is focused specifically on building an arcade cabinet powered by the Raspberry Pi, this post will serve well as a general introduction to the basics you’ll need (or at least want) to put together a core Raspberry Pi setup for any purpose.

The bare minimum you need to use a Raspberry Pi is:

  • Raspberry Pi
  • 5V 700mA (or greater) Micro USB power supply
  • HDMI (or RCA video and 1/8-inch stereo audio) cable
  • 4 GB or larger SD card
  • USB keyboard and mouse
  • Ethernet cable

Practically speaking, however, you’re going to also need the following:

  • TV or monitor with HDMI or RCA audio/video input
  • PC or Mac with an SD card slot (or get your SD card with Raspbian Linux preinstalled)
  • USB WiFi adapter (instead of Ethernet)
  • USB wireless all-in-one keyboard/trackpad (instead of separate keyboard and mouse)
  • USB hub (at least 3 ports; powered is better)
  • A case

I strongly recommend HDMI instead of RCA video if possible, as HDMI delivers an all-digital signal for a much clearer picture. I have not tested the Raspberry Pi with RCA video output, so from here on out we’ll just assume you’re using HDMI.

While you don’t need a case for the Raspberry Pi, it sure looks nicer (and will be better protected from damage) inside one.

Get the Goods

As noted above, everything you need is available on Amazon, which is where I got all of my components. My preferred options for each are shown below, but bear in mind that a lot of these exact parts and suppliers come and go, so the links may not continue to work in the future. Where this is especially a concern, I have included general notes on what to look for when picking an alternative.

This list cuts to the chase, and includes the things I think you need, including the WiFi adapter, wireless keyboard/trackpad combo, and USB hub.

TV not included.

(All images shown here are from the respective Amazon product pages. And, full disclosure, all of the Amazon links herein include my affiliate code. It won’t cost you any more, but if you use these links Amazon will throw a few pennies my way. Thanks!)

raspberrypiRaspberry Pi
There are a few different options for the Raspberry Pi itself, but don’t mess around: get Model B Rev. 2.0, which adds Ethernet and a second USB port missing from Model A, resolves a couple of technical issues with the first versions, and bumps up the built-in RAM from 256 MB to 512 MB.

power5V 700mA Micro USB Power Supply
There are lots of different options for these power supplies, many of which seem to come and go quickly on Amazon. But don’t sweat it. This is a de facto standard charger for many cell phones these days. As long as the charger is 5 volts with at least 700 milliamps and a Micro USB connector, it will power the Raspberry Pi, regardless of the brand or what devices it is advertised as working with.

hdmiHDMI Cable
Don’t get me started on HDMI cables. An HDMI cable is an HDMI cable. The only thing you need to concern yourself with is how long it is and whether or not your Raspberry Pi will be that close to your TV. Well, that and whether or not you’re dealing with a reputable seller. That’s why I like to go with the Amazon Basics cable. It’s cheap, it works, and it’s direct from Amazon.

sdcardSanDisk Extreme 16GB SD Card
Unlike HDMI cables, there is a difference between SD cards. You want one that’s fast, reliable, and spacious. 4 GB is considered the minimum for a Raspberry Pi, but I like to go with 16 GB, since it seems to be today’s best balance between size and affordability. (Translation: the price difference between an 8 GB and a 16 GB SD card is much smaller than the price difference between a 16 GB and a 32 GB.)

wifiEdimax EW-7811Un USB WiFi Adapter
There are a few options here as well, but I love this particular adapter because it’s cheap (about ten bucks) and tiny… which is essential for the Raspberry Pi. Don’t worry about software. It just works… or at least, it will once you get Raspbian installed (see my next blog post for that) and run the included WiFi setup app.

keyboardLogitech K400 Wireless Keyboard/Trackpad
OR FAVI Entertainment SmartStick Wireless Keyboard/Trackpad

It didn’t take long after I got my Raspberry Pi to realize two things: 1) I want as few wires connected to it as possible, and 2) two USB ports get filled quickly. The second is mitigated somewhat by using a USB hub, which you’ll eventually need for the X-Arcade Tankstick in the MAME cabinet project, but even if you’re not doing that, without a USB hub, the only way to use a Raspberry Pi with WiFi is to get an all-in-one keyboard and trackpad.

For practical purposes, the full-size keyboard of the Logitech K400 is the only way to go. The K400 is cheap plastic, but hey… it’s only about $35, and it works. I love it.

On the other hand, if you’re really trying to stay true to the micro-sized spirit of the Raspberry Pi, the FAVI SmartStick or something similar may be the only way to go. About the size of a TV remote control, the SmartStick includes a reasonably usable thumb keyboard and built-in mini trackpad, with the added bonus of a laser pointer so you can mess with your cat when not playing with the Raspberry Pi. In practice I’ve found the SmartStick is a bit touchy… thumb typing on it often requires looking down, and sometimes key presses infuriatingly don’t register. But it’s still fun to have.

hubBelkin USB 2.0 4-Port Powered Ultra-mini Hub
This is the one item on the list that I don’t actually own, as I am relying on an older USB hub I already had lying around. But if I were to buy a new hub specifically for this project, this is probably the one I’d choose. In practice, so far I have been able to do what I need to do with an unpowered hub, but a lot of what I’ve read on the subject suggests a powered USB hub is preferable for use with the Raspberry Pi, to keep the Pi itself from overheating.

caseSB Raspberry Pi Case (assorted colors)
There are a ton of Raspberry Pi cases out there, but this one is my favorite, because it’s nearly indestructible, it’s super-tiny, the Raspberry Pi snaps snugly into it with no tools, it includes slots for all connectors plus two mounting screw slots, and best of all comes in a variety of translucent colors, plus black and white.

I currently own two Raspberry Pi’s, and I have one of these cases for each, one in pink and one in orange.

The Bottom Line

So, how much is all of this going to cost you? I created an Amazon “Listmania!” list (with only the Logitech keyboard, not both), and the subtotal is $143.86. Not bad for all of the essentials (except a display) you need to run a reasonably capable general-purpose Linux computer.

Next time we’ll take a closer look at the SD card, or more specifically, what’s going to go on it: Raspbian, a modified version of the popular Debian Linux distribution that’s been tailored for an optimal experience on the Raspberry Pi.

Update: It’s come to my attention that the Belkin USB hub I have recommended actually is not powered. I will update this post in the near future with an alternate, powered hub recommendation.

February 1983 / February 2013

The sting in my nostrils as I step out the back door
Into the pre-dawn cold

The smell of car exhaust mixing
With the frozen winter air

The stretching shifting halos
Around the streetlights seen through squinting half-awake eyes

The snow stomped from my boots before I step
Through the front door

Ode to the locker room

Being a runner in Minnesota can be difficult, because it forces you to make one of three choices:

  1. Run outside in subzero weather.
  2. Get a gym membership and run on a track or treadmill for 4-5 months.
  3. Stop running altogether in the winter.

Since #3 is not a viable option, you’re left with either bundling up with many layers and tiptoeing hesitantly along icy sidewalks or park paths with blustery winds buffeting your face, or paying a monthly fee for the privilege of driving to a building and running indoors on a treadmill or (if you’re lucky) a track, a tedious but climate-controlled solution.

Being an uncharacteristically wimpy Minnesotan, I’ve gone with the gym membership. I’m very fortunate, I suppose, to live close to the Midtown YWCA in Minneapolis, where I have access to first-rate facilities including a 1/6 mile indoor track. I loathe running on a treadmill. The track can be tedious, but at least I’m actually moving. And if I pick the right soundtrack, I can even visualize running around Lake Nokomis instead. (I’ve run Nokomis to the sounds of my own The Long Run enough times that I know precisely where I am in relation to the lake as each of the 11 sections of the 40-minute piece comes on.)

But as much as I can trick myself into enjoying (or at least tolerating) indoor running in the winter, there’s one aspect of Y membership that I will never like or be able to reconcile with my desire to be outside and alone when I run: the locker room.

I was not a jock in school. In fact, I was pretty much exactly whatever the opposite of a jock is. So what little time I did spend in a locker room was an exercise in taunting and humiliation (real or imagined, and probably more imagined than I believed at the time). I’m no longer afraid of the locker room. I just don’t like it.

I don’t like how crowded it is. I don’t like having to find a space on a bench to put my stuff while I change, or coming back to the locker room after my run to see someone else has chosen bench space directly in front of my locker.

I don’t like listening to other people whistling in the showers. What is so great about this experience to make them want to whistle their tuneless little non-melodies?

I don’t like people who are too comfortable being naked in the locker room, and I also don’t like people who are too uncomfortable with it. Be naked in the shower, the sauna, and at your locker, but nowhere else. Don’t be afraid to take off your swim trunks in the shower. Conversely, don’t stand at the sink naked while you shave, or at the counter by the hair dryers, reading a newspaper. (It kind of just seems logical to me to cover up certain parts when you’re wielding a razor blade, electronics, or paper. Especially paper.)

I don’t like listening to other people’s conversations, even when I am deliberately eavesdropping. I don’t want to be eavesdropping. I especially don’t like listening to teenagers swear loudly. And get off my lawn.

I don’t like how hot it is in the locker room, and how by the time I’m done drying off after my shower, I’ve started sweating again before I can even put on my shirt.

Given my dislike of winter in general, and especially my dislike of the compromises it requires (like spending so much time on the corollary disliking of myriad characteristics of spending time in the Y locker room), I’ve been asked by certain individuals in my life why I want to live in Minnesota at all.

They just don’t understand.

I’m not sure if it’s the harsh conditions of life in the Upper Midwest, much like the harsh conditions in the Scandinavian countries where many of our ancestors came from, or whether we’re just resentful of how easily our existence is ignored by the rest of the country, but part of the joy of being Minnesotan is to be able to complain about being Minnesotan. For us, to love something is to feel comfortable complaining about it.

Of course, that would suggest that perhaps I really love the locker room. But love and hate are not opposites. The opposite of love is indifference. And whether I love the locker room, or hate it, the one thing I clearly am not is indifferent.

But whatever the reason for my strong feelings, there is one that is stronger than all. Spring can’t get here soon enough.