Skip to content
Jun 25 16

Monte Carlo experiment

by aloshbennett

Monte Carlo simulations are a powerful way to solve a bunch of problems using random sampling. These include, and are not limited to, estimating computations that are costly in nature.

Here’s a fun way of computing value of π. Consider a circle of radius r centered at (0,0). This could be bound by a square with corners at (-r, -r), and (r, r). The area of the circle is πr² while the area of the binding square is 4r². If you were to randomly mark n points within the square, the probability p that it would be within the circle will be area of circle / area of square => π/4.

In a Monte Carlo experiment, you would randomly mark n points, and measure the probability p by counting the number of points within the circle. You have,

p/n = πr²/4r²


π = 4p/n.

Try it out, you could see that the estimated value of π becomes better with n. Have fun!

Jan 23 15

Profiling Spark using YourKit

by aloshbennett

Hook up your spark-shell to YourKit profiler by adding the following lines to spark/conf/

SPARK_DAEMON_JAVA_OPTS+=" -agentpath:/root/yjp-12.0.5/bin/linux-x86-64/"
SPARK_JAVA_OPTS+=" -agentpath:/root/yjp-12.0.5/bin/linux-x86-64/"

This would attach the YourKit profiler in ‘sampling’ mode for all spark-submits, including the ones from spark-shell. Sometimes I prefer ‘tracing’, and at times skip profiling completely. The following code snippet helps you specify the profiling option while launching the spark-shell.

for curr in $@
  if [[ "$curr" =~ -Dprofiling=(.*) ]] ; then
    if [ "$option" == "sampling" ]; then
    if [ "$option" == "tracing" ]; then
if [ "$prof" != "none" ]; then
  SPARK_DAEMON_JAVA_OPTS+=" -agentpath:/opt/yourkit/yjp-2014-build-14116/bin/linux-x86-64/$prof"
  SPARK_JAVA_OPTS+=" -agentpath:/opt/yourkit/yjp-2014-build-14116/bin/linux-x86-64/$prof"
Oct 1 14

TLA+ on Ubuntu

by aloshbennett

I am convinced. Completely sold on TLA+. You’ll see a lot more posts on TLA+ here. This one is on how to set up your ubuntu machine for TLA+ development.

TLA Toolbox is a pretty decent IDE for TLA+, built on top of eclipse. On my machine, the ToolBox jvm process kept crashing. Adding the following line to toobox/toolbox.ini fixed this. For more details, refer this eclipse bug.


The ASCII to PDF export (LaTex) would fail unless you have pdflatex installed.

sudo apt-get install texlive-latex-base

As for running the Model Checket, I prefer to use the TLA+ tools from the cli. Make sure to add the tla2tools.jar to your classpath and you should be good to go!

May 26 14

Code challenge: Flow of control

by aloshbennett

Can you make this better?

The API needs to store the latest version of a record. It does so by checking the revision number of the record being inserted against the current version in the db. If the revision number of the new record is greater, it is inserted. Otherwise it is ignored. The db provides conditional APIs to perform check-and-update operations. Give it a shot.


//Gets the current record. Returns null if no record is found.
public Record getRecord(String primaryId);
//Inserts a record. Throws exception if a record is already present.
public void insert(Record rec) throws ConstraintViolationException;
//Updates a record. Throws exception if the record is not of a 
//newer revision - basically the verison number should be increasing
public void update(Record rec) throws ConstraintViolationException;

And here is my attempt:

public void storeLatestVersion(Record newRec) {
    String primaryKey = newRec.primaryKey;
    Record currentRec = getRecord(primaryKey);
    if (currentRec==null) {
        try {
            //done. lets go home
        } catch(ConstraintViolationException ce) {
            //Some one bet me to it. Let me fetch the current rec
            currentRec = getRecord(primaryKey);
    //at this point currentRec cannot be null.
    //making such an assumption makes me sad :(
    if (currentRec.versionNo < newRec.versionNo) {
        try {
            //yey! job well done
        } catch(ConstraintViolationException ce) {
            //someone has updated the record in the mean time?
            currentRec = getRecord(primaryKey);
    if (currentRec.versionNo >= newRec.versionNo) {
        //nothing to do as there is later version in the db.
        //lets ignore the current record.
    } else {
        //this can't be happening. something went wrong.
Oct 23 11

30 sec puzzle: spot the null pointer

by aloshbennett

I got a null pointer exception at the line mentioned. What could have caused it?

private ARTTimerTask timerTask = ARTTimerTask.getInstance();
public void notify(String messageId, Long messageTime) {
    //NPE in the line below
                  System.currentTimeMillis() - messageTime);

Here’s the relevant section from ARTTimerTask:

private static final ARTTimerTask _INSTANCE 
                                = new ARTTimerTask();
public static ARTTimerTask getInstance() {
    return _INSTANCE;

(Debashish comes over to the desk, takes a look and proclaims, “This A*** B***** is pathetic”. 7 seconds!)

Apr 30 11

Moving on

by aloshbennett

I’ve decided to call it a day at Oracle. And its been a long day too.

As part of Innovation Labs at 24/7 Customer, my new organization, we are building a platform for predictive analysis and intervention in CLM area. A lot of exciting things on the plate already and the team is very passionate and dynamic. Small organizations are really nimble footed and flexible, and I’m liking it.

In fact, I find myself recruiting already. In case you are interested, reach out to me at The id would be aloshbennett. A more detailed job description can be found here.

Mar 31 11

Introduction to WebServices – IEEE Session

by aloshbennett

An introductory session on web services was conducted at BITS Plianl campus at Hyderabad in association with IEEE on 26th March 2011. The talks introduced the students to XML, WSDL and SOAP and REST styles of services.

I have to say that I am impressed with the students who turned up at the event. Some of them are into very interesting projects and it feels great to see them do things over and above the curriculum.

With me were Bhaskar Ghosh and Neil Ghosh who actually delivered the talks and conducted the hands-on lab.

Overall, a very pleasant experience. Special thanks to Dr Geetha Deviprasad who made the event possible.

The event is covered in detail at Neil’s blog.

Feb 15 11

HTML5 Canvas Sample – Building a maze

by aloshbennett

There are a lot of features that HTML5 bring. What makes canvas popular?


Canvas provides a screen area onto which you could draw 2D shapes and images. The set of APIs are simple. But the strength of the canvas comes from how you could manipulate it using javascript.

We start by defining a canvas. Note that the canvas element would be rendered only if your browser supports it.

<canvas id="canvas" width="100" height="100">Your browser does not support canvas.</canvas>

Next, we define an array in javascript to hold the maze data, initialize it and start building our maze by venturing out into the first cell.

    var board = new Array();
    function maze() {
        init();            //initialize the maze
        explorePath(1,1);           //start exploring
    function init() {
        var canvas = document.getElementById("canvas");
        if (canvas.getContext) {
            context = canvas.getContext("2d");
            //paint the maze black
        for(i=0; i< SIZE; i++) {
            board[i] = new Array();
            for(j=0; j<SIZE; j++) {
                board[i][j] = 0; //fill the board array with zeroes

For the explorePath method, we use a recursive algorithm

Paint the cell white
Initialize options list {UP, DOWN, FORWARD, BACKWARD}
While options:
    Select option randomly
    If valid option - Cell is unoccupied, we don't create tunnels etc.
     remove explore option from list
end loop.

In short, we are doing a back-tracking traversal (DFS) of the option tree.

The algorithm works fine until we try to put a delay between each explorePath call to get that nice effect.
You see, it is not really easy to sleep() in javascript. There is no equivalent of Java sleep.

What we could do is this:

  setTimeOut('explorePath()', 100);

This would call the explorePath after a delay of 100 milliseconds. But, the call to setTimeOut() is non-blocking and it would continue execution while scheduling explorePath() to be executed after 100 milliseconds.

We could maintain our own call stack and choose not to call the explorePath() recursively. But it is easier to just push the maze positions to be painted white into a stack and continue execution. Once the display stack is ready we could pop from it and render onto canvas in a delayed manner using setTimeOut().

Finally, we could add a nice touch to the maze by not changing its directions too often, but in a timely manner based on a seeded probability.

Here’s the complete source (right click and view source). And don’t forget to save your maze as a PNG (again, right click and save).

Dec 19 10

Implementation and Testing of Software Projects

by aloshbennett

Computer Society of India, Hyderabad Chapter had organized a Workshop on Project Development and I was lucky to give a talk on its implementation and testing aspects. Among the speakers was Mr Raju Kanchibhotla, Chairman of CSI, Hyderabad who gave an inspiring talk on taking IT to the masses.

Here are the slides from the talk.

Implementation and Testing of Software Projects

Dec 13 10

Functional programming the LISP way

by aloshbennett

Lately, there’s been a lot of talk about functional programming. The newer generation languages advertise themselves as multi-paradigm ( as opposed to java’s object oriented style). Java is doing catching up by bringing in Project Lambda in JDK 8, and Predicates has already started becoming popular (here’s a good starter by Arun Vydianathan).

This post is an introduction to a very potent and heady LISP cocktail of Higher Order Functions, Lamdas and Macros.


Higher Order Functions: Functions that take other function as its argument. A function that filters a list can be converted into higher order function by providing the actual filtering function as an argument.

Lambda Expression: Dynamically creates functions and attaches a context to them. The filtering function can be created at runtime as a lambda and its bounds set at runtime.

Macro: Creates code at runtime by substituting the macro with corresponding expression. While Lambdas let you replace the bound conditions, macros let you decide the structure of your filter function.

Higher Order Functions
You could print all students belonging to “ELX” department from a list like this:

(defun print-students (students-lst)
  (loop for student in students-lst
       do ( if (equal "ELX" (getf student :department))
	       (print student))))

Now if you want to print the students who had received ‘A’ grade, you are at a loss. You cannot do that without writing a new function by duplicating the looping logic.

Enter Higher Order Functions. Higher Order Functions are functions which take another function as its argument. The example above can be modified into a generic higher order function if we could abstract the evaluation logic into another function.

Consider the following re-write

(defun print-students (students-lst, eval-student)
  (loop for student in students-lst
       do ( if (funcall eval-student student)
	       (print student))))
(defun eval-elx-department (student)
  ( if (equal "ELX" (getf student :department)) t))
(defun eval-a-grade (student)
  ( if (equal "A" (getf student :grade)) t))
(print-students *students-lst* #'eval-a-grade)

Note: LISP built-in functions REMOVE-IF and REMOVE-IF-NOT takes a list and a evaluation function, applies the evaluation function to every element in the list and returns a new list which contains/doesn’t contain the element. The idea of predicates is very similar to this.

Lambda Expressions
If we could parameterize the grade from the eval-a-grade function, we would have a generic eval-grade and that would have been just great!
Unfortunately, the print-students function expects an evaluation function that takes in a single argument: student.
Using Lambda expressions, it is possible to custom make a function with a context. Lambda expressions are used to create anonymous functions with a scope attached to them. The variables used within the lambda expressions are evaluated in the context they are run in.

(defun eval-grade (grade)
  (lambda (student)
    ( if (equal grade (getf student :grade)) t)))
(print-students *students-lst* (eval-grade "A"))
(print-students *students-lst* (eval-grade "B"))

LISP macro is a very powerful beast that can be used to generate code dynamically. Instead of having multiple evaluation functions, it is possible to write a LISP macro to dynamically generate a lisp function based on the inputs you pass to it.

(defmacro eval-student (&key dept grade)
       (and ,(if dept
		 `(equal ,dept (getf student :department)))
	    ,(if grade
		 `(equal ,grade (getf student :age))))))
(print-students *students-lst* (eval-student :dept "ELX" :grade "A"))

The eval-student macro expands to


Note: Though very powerful, it is easy to cut yourself using macros. Use discretion when using them.

Functional programming is a great way to modularize and reduce the size of your code. More than that, it is fun! Happy coding.