May 6, 2010

Can I Trust Random Number Generators

Can I Trust Random Number Generators

A few days ago I had read this article about how a random number generator had been used to create a flawed shuffle of an array. As it happens, today I had to generate some random numbers myself and I started to think, how much do I really trust the random() function in my code. I knew that Random Number Generators in computers are just Pseudo-Random Number Generators, i.e. they produce values that look sufficiently random, but are not truly random. I also knew that Hardware Random Number Generators existed which can convert unpredictable physical stimuli (like thermal noise, photoelectric effects or other quantum phenomenon) into electrical stimuli and are considered genuinely more accurate. But let’s ignore that for now, since I am purely interested in the software side of this. So I wanted to find out why computers can’t generate real random numbers, what are their limitations and if I need to worry about the outputs of pseduo-random number generators.

Why can’t computers generate real random numbers?
There must be a whole host of reasons since there is a whole field of research dedicated to this area. So one lesson to learn is this: never try to create your own random number generator or you might end up with something stupid like thinking random() generates increasingly randomer numbers the more times you call it. Use an implementation of someone’s algorithm who has already done research in the area. But to answer the above question in the most simplest of terms: Its because computers just follow instructions, and random numbers are the opposite of following instructions. If you make a random number by following instructions, then it’s not very random! Imagine trying to give someone instructions on how to choose a random number.

What are the limitations of pseudo-random number generators?
Several, but what resonates the most with me is that the numbers generated tend to repeat after a while. Good generators have a period large enough such that the repition rate is not noticeable for practical purposes. Special care needs to be taken when using random number generators for cryptography. Even though a generator might be ‘good’, that does not neccessiraly make it secure enough for the task at hand. But I’m not working with anything this critical.

Do I need to worry about the non-random nature of pseudo-random generators?
The Mersenne Twister (based on Mersenne prime numbers) is a well-estabilshed (i.e. publicly scrutinized by professionals) algorithm that generates very high-quality pseudorandom numbers quickly with a sufficiently large period. It is the default implementation in R, Maple, MATLAB, Python and Ruby. Since I’m working with python right now, this is enough to assure me that I don’t need to spend more time researching this topic.

This is my first entry in what I plan to be a series titled ‘What I Learned Today’, where I’ll write up a little entry about something new I learned that day (if I did any learning that day at all!). I will choose the item to be always something new that I discovered, and something that I think not everyone might be aware of. I figure this will help me better remember things and have a reference to go back to if needed. I will try to keep the content brief and in layman terms, but will link to other sources for those interested in more details.

April 24, 2010

Sukho Thai Restaurant Review

My friend Wing wanted to try out Sukho Thai Restaurant in the Regent Park area (Dundas and Parliament) because of a recent positive review he had read in NOW magazine.

We initially checked out their website which looked pretty impressive and very well done. I also spotted Shrimp Chips in the menu which I had been dying to eat for quite a long time but wasn’t quite sure where to get them from. So we decided to check the place out.

The website had raised our expectations high. But when we got to the neighbourhood, it didn’t look so great. My friend also pointed out that this was a high crime rate area (news to me!). We finally found the restaurant and it doesn’t look that great at first sight. A narrow corridor which is pretty much the entire restaurant. There was a lineup when we got there and a lineup was there as long as we were eating – which was past the 10pm closing time for the restaurant. We also had to wait about an hour in line to get a seat.

However when you do start to look around, the place is nicely decorated. We also started noticing the food that other people were being served, which looks appetizing and the servings were large. Considering the fact that most of the items are under $10, this looked like a great find.

The place even has translations posted up all over the place so you can learn some Thai while waiting around. Word of advice for anyone in groups larger than 2: make a reservation before hand – this place gets crowded! A group of 4 who were waiting in line before us weren’t able to find a table at all and eventually ordered take-out.

After I had ordered my choice selections off the menu, I noticed a sign on the wall certifying the place as Halal. For those of you that know me, you know that this is a big deal for me. This is first ever halal Thai place I’ve encountered in Toronto. I’ll certainly be coming back here more often.

And the food was simply amazing too. The Shrimp Chips fulfilled my craving. The spicy Pad Kee Mao with shrimp was full of flavours from its different ingredients. And so aromatic. The mango salad on the side was a nice retreat from the hot dish. As was the orange milky drink (which tasted somewhat like bubble tea). I’ve always wanted to have bubble tea with a meal! And in the end, I couldn’t resist and had to order desert. Tapioca with Coconut Milk.

Simply amazing. I had a hard time finishing up the desert after that big meal. I chatted with our waiter, complementing the food. It turned out he was the owner (or part-owner along with his wife and parents). His wife was the chief cook who is originally from Thailand. He even let me take some Shrimp Chips for takeout!

Really nice people and really nice restaurant. With great prices!

March 5, 2010

Google’s starred search results is annoying

Google's starred search results is annoying

Google recently introduced displaying starred search results along with regular results in order to better personalize the searching experience.

Although well-intentioned, I find this rather annoying. I already have a whole bunch of Google Bookmarks saved and now they show up in most of my searches. I have yet to find a single instance where this was relevant to my search. I have been using Google bookmarks for a couple years now and I have some very old bookmarks in there that I don’t visit anymore, but do not want to delete either. When these show up on the top of my search results – the place where I am expecting to find the most relevant search hit – it is quite frankly very annoying. Google should keep the bookmarking and the starring system as separate entities. Or at least give us an option to turn off the ability to show starred/bookmarked results directly in Google search results. I mean, if I wanted to search my Google bookmarks, I can do that through the Google bookmarks site. I don’t want my regular results being cluttered with that too!

August 4, 2009

Ali’s Brief Introduction to Coffee Beverages

Espresso[1] [2]:
Espresso is a concentrated coffee beverage brewed by forcing hot water
under pressure through finely ground coffee. It is a relatively new
drink, introduced in the last century, since it requires special
machines (espresso machines) for its brewing. Espresso differs from
‘normal’ brewed coffee in the fact that the espresso machine is used
to create high pressure steam (up to 15 atmosphere) to pump through
the ground coffee. This results in a much higher concentration of the
final solution than is possible with just regular drip coffee makers
using gravity to move the water through the coffee grounds. Espresso
contains approximately two to three times the caffeine content of
regular drip brewed coffee. A shot of espresso has about half the
caffeine of a standard cup of drip brewed coffee.
Espresso ‘Shot’:
The act of producing a shot of espresso is often termed ‘pulling’ a
shot, originating from lever espresso machines which require pulling
down a handle attached to a spring-loaded piston, forcing hot water
through the coffee at high pressure. This process produces an almost
syrupy beverage by extracting and emulsifying the oils in the ground
As a result of the pressurized brewing process, all of the flavors and
chemicals in a typical cup of coffee are very concentrated. For this
reason, espresso is the base for other drinks:
Latte (Caffe Latte)[3]:
(1/3 espresso, 2/3 steamed milk)
What in English-speaking countries is now called a latte is shorthand
for ‘caffelatte’ meaning ‘coffee and milk’. Ordering a ‘latte’ in
Italy will get a large glass of hot milk, as latte simply means milk
in Italian. A Caffe Latte is typically prepared with approximately 1/3
espresso and 2/3 steamed milk, with a layer of foamed milk
approximately 5 mm thick on the top. Among all the drinks mentioned in
this guide, the Latte is the one with the most milk in it. Variants
include replacing the coffee with another drink base such as chai[4],
mate[5] or matcha[6].
(1/3 espresso, 1/3 steamed milk, 1/3 foam)
A Cappuccino differs from a Caffe Latte in that it is prepared with
much less steamed or textured milk than the Caffe Latte. A cappuccino
is traditionally served in a porcelain cup, which has far better
heat-retention characteristics than glass or paper. The foam on top of
the cappuccino acts as an insulator and helps retain the heat of the
liquid, allowing it to stay hotter longer. A skilled barista will pay
special attention to attaining the correct texture of the milk and the
ratio of the foam while steaming the milk, thus making the Cappuccino
one of the most difficult espresso-based beverages to make properly.
Moreover, some may even obtain artistic shapes while pouring the milk
on the top of the espresso coffee[8].


Mochaccino (Cafe Mocha)[9]:
(1/3 espresso, 2/3 steamed milk, with chocolate)
A Cafe Mocha is a variant of the Caffe Latte except that a portion of
chocolate is added, typically in the form of sweet cocoa powder,
although many varieties use chocolate syrup. Unlike Cappuccino, Cafe
Mochas do not contain the well-known milk froth on top. They may have
whipped cream instead. A ‘White Mocha’ is a variant made with white
chocolate instead of milk or dark.
Macchiato is espresso with a small amount of hot, foamed milk.
‘Macchiato’ simply means ‘marked’ or ‘stained’, and in the case of
Caffe Macchiato, this means literally ‘espresso stained/marked with
milk’. The ‘mark’ or ‘stain’ refers to the foamed milk that is put on
top to indicate the beverage has a little milk in it (usually about a
Other beverages:
There are lots more coffee beverages. Here are some links for further reading:
Happy drinking!

[8] Latte Art: