<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>lelanthran.com</title>
	<atom:link href="http://lelanthran.com/deranged/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://lelanthran.com/deranged</link>
	<description>On the Shoulders of Giants</description>
	<lastBuildDate>Wed, 22 Feb 2012 13:04:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Reference Management in HTML</title>
		<link>http://lelanthran.com/deranged/?p=148</link>
		<comments>http://lelanthran.com/deranged/?p=148#comments</comments>
		<pubDate>Mon, 20 Feb 2012 21:21:05 +0000</pubDate>
		<dc:creator>lelanthran</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://lelanthran.com/deranged/?p=148</guid>
		<description><![CDATA[Writing well-referenced material is a chore. If you&#8217;re using LaTeX, then you&#8217;re in luck because it works with BibTeX to lessen the tedium. If you&#8217;re using a word-processor, you&#8217;re still in luck as you can use something like EndNote to &#8230; <a href="http://lelanthran.com/deranged/?p=148">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Writing well-referenced material is a chore. If you&#8217;re using LaTeX, then you&#8217;re in luck because it works with BibTeX to lessen the tedium. If you&#8217;re using a word-processor, you&#8217;re still in luck as you can use something like EndNote to manage your references. How about if you are writing a blog, or an HTML book, or contributing to a Wiki? Then, it seems, you&#8217;re out of luck.</p>
<p>Until now. Enter JSNote (Javascript Note). It works pretty much the same way that BibTeX does with LaTeX. You maintain a list of references in a separate file, and in your material you cite those references by means of calling a javascript function. For example;<br />
<code><br />
// Filename /downloads/references.js<br />
addRef ({name : "john",<br />
year : "2012",<br />
volume : "17",<br />
author : "Charles Dodgeson",<br />
title : "Obtuse Works of Mathematics in 'Alice in WonderLand'",<br />
});<br />
addRef ({name : "boehm_1",<br />
year : "2006",<br />
month: "May",<br />
conference: "ICSE '06",<br />
author : "Barry Boehm",<br />
publisher: "ACM",<br />
title : "A View of 20th and 21st Century Software Engineering",<br />
});<br />
</code><br />
And then, in your HTML you pull the jsnote.js script in, as well as your list of references and use them like so:<br />
<code><br />
&lt;script src="/downloads/jsnote.js"&gt;&lt;/script&gt;<br />
&lt;script src="/downloads/references.js"&gt;&lt;/script&gt;<br />
&lt;p&gt;I now will reference Boehms article&lt;script&gt;cite ("boehm_1")&lt;/script&gt;and then reference Johns article like this&lt;script&gt;cite ("john")&lt;/script&gt;&lt;/p&gt;</code></p>
<p>&lt;heading3&gt;References&lt;/heading3&gt;<br />
&lt;script&gt;printRefs()&lt;/script&gt;</p>
<p>The practical result is that the citations are all printed properly. The above example <a title="example usage of jsnote" href="http://www.lelanthran.com/apps/jsnote-example.html" target="_blank">looks like this</a>. Note that the list of references in reference.js works exactly like it would in BibTeX, namely that you can put the arguments in any order, or even leave some of them out if you don&#8217;t want them displayed. As long as each reference in the references list has a name, jsnote will be able to cite and display all the information you gave when adding it as a reference.</p>
<p>Literate Programming Is Not Dead Yet.</p>
<p><a href="/downloads/jsnote.js">Get jsnote.js</a><br />
<a href="/downloads/references.js">Get a sample references file</a></p>
]]></content:encoded>
			<wfw:commentRss>http://lelanthran.com/deranged/?feed=rss2&#038;p=148</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Memory accounting routines for C</title>
		<link>http://lelanthran.com/deranged/?p=136</link>
		<comments>http://lelanthran.com/deranged/?p=136#comments</comments>
		<pubDate>Mon, 13 Feb 2012 15:59:13 +0000</pubDate>
		<dc:creator>lelanthran</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://lelanthran.com/deranged/?p=136</guid>
		<description><![CDATA[Managing memory is Hard. Very Hard. If you are programming in C you have a few options, notably valgrind. However, if you find yourself in the situation that I find myself in now in which valgrind cannot work, then you &#8230; <a href="http://lelanthran.com/deranged/?p=136">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Managing memory is Hard. Very Hard. If you are programming in C you have a few options, notably valgrind. However, if you find yourself in the situation that I find myself in now in which valgrind cannot work, then you need something more.</p>
<p>For example, I&#8217;m writing code in ECL to call out to a few C libraries (that I&#8217;ve written myself). Valgrind is incompatible with ECL. In fact, it might just be incompatible with any GC-language. Whatever. I still need to keep accounting of my memory during testing.</p>
<p>Hence, I wrote this <a title="Xmalloc" href="http://www.lelanthran.com/downloads/xmalloc-1.0.2.tar.gz">little replacement/wrapper for malloc</a>. It stores all allocated memory as a list of blocks internally. It stores the filename, function and line number and the amount of memory that was allocated by each invocation of xmalloc. Usage is fairly standard:</p>
<pre width=100%>int *foo = XMALLOC (sizeof *foo);
...
XMALLOC_FREE (foo);</pre>
<p>&nbsp;</p>
<p>Easy? You can look through the header for all the options (how to dump the list of blocks allocated, some general statistics on allocation, etc). Also, this can be turned off while the program is running, and normal malloc() is then used.</p>
<p>Enjoy.</p>
<p><a href="http://www.lelanthran.com/downloads/xmalloc-1.0.2.tar.gz" title="xmalloc and co.">Download the the xmalloc library</a></p>
]]></content:encoded>
			<wfw:commentRss>http://lelanthran.com/deranged/?feed=rss2&#038;p=136</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>iBurst USB Drivers for Linux</title>
		<link>http://lelanthran.com/deranged/?p=132</link>
		<comments>http://lelanthran.com/deranged/?p=132#comments</comments>
		<pubDate>Mon, 13 Feb 2012 11:10:41 +0000</pubDate>
		<dc:creator>lelanthran</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://lelanthran.com/deranged/?p=132</guid>
		<description><![CDATA[Got iBurst yesterday, along with a USB modem. The salesperson assured me that the modem works under Linux. Well, chalk one up to the forces of sales and marketing, because the modem definitely did not work, and the enclosed CD &#8230; <a href="http://lelanthran.com/deranged/?p=132">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Got iBurst yesterday, along with a USB modem. The salesperson assured me that the modem works under Linux. Well, chalk one up to the forces of sales and marketing, because the modem definitely did not work, and the enclosed CD definitely did not have drivers for Linux. I downloaded the open source drivers (ibdriver-1.3.5) from sourceforge, but the compilation failed.</p>
<p>So, having &#8220;fixed&#8221; the compilation problems by means of removing the pcmcia functionality, I offer the updated drivers to the world. This iBurst USB modem driver will work under Ubuntu, Debian, Mint, Redhat, CentOS, slackware or anything else that has a linux kernel 2.6 and above (including kernel 3.0). <a title="iBurst USB Modem Driver for Linux" href="http://www.lelanthran.com/downloads/ibdriver-1.3.5-linux-2.6.36-without-pcmcia-1.0.sh">Download it here</a>.</p>
<p>Note that this is self-extracting script, so before running it make sure that it has execute permissions set. Please let me know if this does not work by emailing me the output of the executable. If it <em>does</em> work, then you can thank me in your will (of course, if you don&#8217;t own much, then just leave a comment here thanking me:)</p>
<p><a title="iBurst USB Modem Driver for Linux" href="http://www.lelanthran.com/downloads/ibdriver-1.3.5-linux-2.6.36-without-pcmcia-1.0.sh">Download iBurst USB Modem Linux driver</a></p>
]]></content:encoded>
			<wfw:commentRss>http://lelanthran.com/deranged/?feed=rss2&#038;p=132</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Writing fiction is  for Dummies</title>
		<link>http://lelanthran.com/deranged/?p=121</link>
		<comments>http://lelanthran.com/deranged/?p=121#comments</comments>
		<pubDate>Mon, 12 Dec 2011 09:56:58 +0000</pubDate>
		<dc:creator>lelanthran</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://lelanthran.com/deranged/?p=121</guid>
		<description><![CDATA[I&#8217;ve been writing short works of fiction (not on this blog, of course, everything here is as fact-based as I can muster), and I&#8217;ve now got a full six short stories up on various sites. You can get a nice &#8230; <a href="http://lelanthran.com/deranged/?p=121">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been writing short works of fiction (not on this blog, of course, everything here is as fact-based as I can muster), and I&#8217;ve now got a full six short stories up on various sites.</p>
<p>You can get a nice overview of the short stories <a title="Smashwords Author Listing" href="https://www.smashwords.com/profile/view/lelanthran" target="_blank">over here at smashwords.</a> Feel free to read, review and rate. They are free, after all, although this state of affairs is not going to last if I can manage to keep up the pace for another four  months.</p>
<p>Why four months? Well, in about four months I figure I&#8217;ll have enough to put together a collection into a single book, which will then go on sale. At the moment though, if you like science-fiction, OR, wicked mind-games, OR short humour, OR Zombie apocalypse tales, then head on over to the smashwords site, and read, review, rate.</p>
<p>I&#8217;m open to suggestions, by the way, so feel free to email me about any of the short stories.</p>
]]></content:encoded>
			<wfw:commentRss>http://lelanthran.com/deranged/?feed=rss2&#038;p=121</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Baby Sex-selection at Conception</title>
		<link>http://lelanthran.com/deranged/?p=103</link>
		<comments>http://lelanthran.com/deranged/?p=103#comments</comments>
		<pubDate>Sat, 20 Aug 2011 23:52:30 +0000</pubDate>
		<dc:creator>lelanthran</dc:creator>
				<category><![CDATA[Data analysis]]></category>
		<category><![CDATA[Mobile development]]></category>
		<category><![CDATA[Research]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Useful software]]></category>

		<guid isPermaLink="false">http://lelanthran.com/deranged/?p=103</guid>
		<description><![CDATA[Everyone knows how to make a baby. When a mummy and a daddy love each other very much, they make a baby. Well, thats what we tell the kids anyway. There is more to that, however. The ovum is released  &#8230; <a href="http://lelanthran.com/deranged/?p=103">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Everyone knows how to make a baby. When a mummy and a daddy love each other very much, they make a baby. Well, thats what we tell the kids anyway. There is more to that, however.</p>
<p>The ovum is released  roughly 14 days into the womens monthly cycle. The sperm cells can survive for between 3 and 5 days. The ovum will be viable for only 24 hours (or less). Hence, intercourse must occur <strong>before</strong> ovulation, and ovulation must occur before the sperm cells all die.</p>
<p>Hence the fertile period  (the best time to make love in order to conceive a child) is during those 4- 6 days (sperm cells will survive for 3 &#8211; 5 days, and you can still make love on the 4th &#8211; 6th day when the ovum is descending the fallopian tube). It&#8217;s pretty easy to mark these days off on a calendar.</p>
<p>What most woman don&#8217;t realise is that not all fertile days are equally likely to result in a boy or a girl baby. Making love on certain days is more likely to result in a boy baby, and making love on other days is more likely to result in a girl baby. This is due to two particular characteristics of the sperm cells. For a more thorough explanation please see <a href="http://www.lelanthran.com/conception/for-prj.pdf">this document over here</a>.</p>
<p>So, lets say you want a girl baby &#8211; how do you know which day will be the best to conceive a girl? Well, you can either use the <a href="http://www.lelanthran.com/conception">Conception Fertility Calendar</a> which will mark off the days that are better for conceiving a girl, and mark off those days that are better for conceiving a boy, or you can do the calculation manually and mark it off on a calendar yourself. Let me demonstrate.</p>
<p>First, figure out when your ovulation is due to occur. This is generally 14 days from the end of your cycle, so measure your cycles to get an accurate number (not all woman have a 28 day cycle), add that to the start date of your period and then subtract 14 days from that.</p>
<p>For example, if your cycle length is 26 days, and your period starts on the 2nd of the month, then add 26 to the 2nd, and you get 28th, then subtract 14 days from that, and you get the 14th of the month.</p>
<p>Now that you know when your ovulation will occur, you need to figure out when the fertile period starts. The start of the fertile period is simply the day of ovulation minus the lifespan of the sperm cells<em></em>. So, if we assume that sperm cells will last for 3 days, then the start of the fertile period is &#8230; <em>&#8220;the 14th (the day of ovulation) minus 3 days (the lifespan of the sperm cells)&#8221;</em>, which works out to the 11th of the month.</p>
<p>This means that if you made love on the 11th, with a sperm lifespan of 3 days, they&#8217;ll all be dead  at the end of the day on the 13th, there will be no more sperm cells left on the 14th when the ovum is viable, therefore we have to add one more day so that the start of the fertile period works out to be the 12th.</p>
<p>To determine which of the days are better for a girl (or a boy) is a little more complicated:</p>
<ul>
<li>Do integer division on the fertile period by 2. In our case it is 4 (the total number of days you are fertile for) divided by 2, which is 2 with no remainder. Hence the first two days of the fertile period are better for a girl and the final two days are better for a boy.</li>
<li>The above won&#8217;t work if the fertile period is an odd number &#8211; for example if the fertile period is 5, the results of the integer division is 2 with a remainder of 0.5. In this case mark the first two days of your calendar as fertile for a girl, the day after those as fertile for both boy or girl and the final two days as fertile for a boy.</li>
<li>To determine the sperm lifespan, or to work out more accurately when ovulation will occur, <a href="http://www.lelanthran.com/conception/for-user.pdf">I have some tips that you can follow</a>.</li>
</ul>
<p>Is all of that too much work to get a baby of the sex you want? I expect it is, but good news is here in the form of an Android application I wrote called (unsurprisingly) <em>&#8220;The Conception Helper&#8221;</em>.</p>
<p>For the price of a happy meal ($3.99) you can simply purchase an application for your cellphone that will do all this for you (and do it more accurately as well). I&#8217;ve written the <a href="http://www.lelanthran.com/conception">Conception Helper </a>for Android phones and tablets. Simply use it as directed and it will display a calendar for you each month with the relevant days indicated on it.</p>
<p>Further, even if you do not start it at all (after setting your start of period), when the relevant days come along,  <em>it</em> will warn you. No need to trust your memory to look at it at all. See the <a href="http://www.lelanthran.com/conception">Conception Website</a>  for more details.</p>
<p>And, there is also no need to take my word for it &#8211; there is a <a href="http://www.lelanthran.com/conception/downloads/The%20Conception%20Helper-trial-final-v1.2-amz.apk">trial version</a> that will work for 30 days &#8211; after 30 days you&#8217;ll have to pony up the cash to buy a copy, or simply redownload it to your phone again.</p>
<p><a href="http://www.lelanthran.com/conception/downloads/The%20Conception%20Helper-trial-final-v1.2-amz.apk">Download it,</a> <em><strong>try it before you buy it</strong></em>, and if you are happy with it, then <em>and only then</em> spend your money on it. In the meantime, you can read the manual and see just how effective Conception can be.</p>
]]></content:encoded>
			<wfw:commentRss>http://lelanthran.com/deranged/?feed=rss2&#038;p=103</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Google with egg on their face?</title>
		<link>http://lelanthran.com/deranged/?p=86</link>
		<comments>http://lelanthran.com/deranged/?p=86#comments</comments>
		<pubDate>Mon, 11 Jul 2011 11:04:58 +0000</pubDate>
		<dc:creator>lelanthran</dc:creator>
				<category><![CDATA[Mobile development]]></category>
		<category><![CDATA[Software development]]></category>

		<guid isPermaLink="false">http://lelanthran.com/deranged/?p=86</guid>
		<description><![CDATA[The Google Android Developer Challenge/Africa is well underway. App submission deadline was 1-July-2011, semi-finalists will be announced 15-July-2011. Winners will be announced 12-September-2011. The point of this, presumably, is to encourage African developers to develop for Android. The biggest motivation &#8230; <a href="http://lelanthran.com/deranged/?p=86">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://code.google.com/android/adcafrica/">Google <em>Android Developer Challenge/Africa</em></a> is well underway. App submission deadline was <em>1-July-2011</em>, semi-finalists will be announced <em>15-July-2011</em>. Winners will be announced <em>12-September-2011</em>.
</p>
<p>The point of this, presumably, is to encourage African developers to develop for Android. The biggest motivation to do something is money, so participants are allowed to <a href="http://code.google.com/android/adcafrica/judging.html">enter paid apps into the challange</a> (under <strong>&#8220;Eligibility&#8221;</strong>). Sadly, it seems that the organisers of ADC spend very little time with the Google Checkout team, because Google checkout <a href="http://www.google.com/support/androidmarket/developer/bin/answer.py?&#038;&#038;answer=138294">does not support many of the countries</a> that are eligible for ADC/Africa, and there is no other way to get paid for apps in Google marketplace.</p>
<p>While I know that, sometimes, in large companies its a case of <em>&#8220;the right hand not knowing what the left hand is doing&#8221;</em>, in this case it seems even the left hand has only a very hazy notion as well. I&#8217;m working on porting my application to iPhone. Of course, the longer it takes for Google to get this working, the larger the number of ADC apps that get ported. It could be a case of Google simply giving app developers motivation to come up with a good concept that then goes to iPhone because the concept could not be sold for Android.</p>
<p>Heads up to Google: those devs that came up with a new, nifty and/or novel app will certainly not make it freely available for Android (not even as trial) if they can make it for money for iPhone. Making a trial version for Android simply means putting a good idea out there that some iPhone dev is going to implement on iPhone. To keep the first-mover advantage of a novel concept, the dev <em>has</em> to be the first one with the app. If it is so novel and nice, people would buy it from elsewhere.</p>
<p>Of course, its always possible that Apple also won&#8217;t allow devs from South Africa to sell applications either &#8211; with Apple you don&#8217;t get this information until after you pay their $99 fee, so if anyone knows if South African developers are allowed paid apps in Apples App Store, let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://lelanthran.com/deranged/?feed=rss2&#038;p=86</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamic arrays in C</title>
		<link>http://lelanthran.com/deranged/?p=68</link>
		<comments>http://lelanthran.com/deranged/?p=68#comments</comments>
		<pubDate>Mon, 14 Mar 2011 22:11:56 +0000</pubDate>
		<dc:creator>lelanthran</dc:creator>
				<category><![CDATA[Software development]]></category>

		<guid isPermaLink="false">http://lelanthran.com/deranged/?p=68</guid>
		<description><![CDATA[Dynamic arrays are remarkably easy to implement in plain C with two caveats: 1. No destructors, so the user of the array has to free the array elements one item at a time (not such a problem!) 2. No copy &#8230; <a href="http://lelanthran.com/deranged/?p=68">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>
Dynamic arrays are remarkably easy to implement in plain C with two caveats:<br />
1. No destructors, so the user of the array has to free the array elements one item at a time (not such a problem!)<br />
2. No copy constructors, so the user of the array has to ensure that structures with pointers are copied correctly.
</p>
<p>
I implemented the dynamic array by simply storing an array of void pointers, and only storing pointers to elements (a pointer to void can point to anything), hence this lets me have a dynamic array that can store elements of anything, even elements of different types (although this is a bad idea &#8211; unreadable code is not the goal!). You can <a href="http://www.lelanthran.com/downloads/rsarray-1.0.0.tgz">download the code that implements this as a library</a>.
</p>
<p>
The &#8220;array&#8221; is simply a struct like so:</p>
<pre name="code" class="c++" style="font:small;">
struct rsarray_t {
   size_t alloc_len;   /* Store the number of allocations */
   size_t used_len;   /* Store the of cells we have thus far used */
   void **data;        /* The actual array of void pointers */
};
</pre>
<p>There are no <tt>new_array</tt> or <tt>create_array</tt> functions that the user must call. All the user has to call to append a pointer onto the end of the array is <tt>rsarray_ins_tail(array, element)</tt>, where <em>element</em> is the new pointer to be appended to <em>array</em>, which is of type <tt>struct rsarray_t*</tt>.</p>
<p>If <tt>array</tt> is NULL, then an array is created and returned with <tt>element</tt> as the first and only element of the array (which is why there is no need for an &#8220;array_create&#8221; function). If <tt>array</tt> is not NULL, then the element is appended to the array using <tt>realloc</tt> if there is not enough space to store another element. Usage is dead-easy:</p>
<pre name="code" class="c++" style="font:small;">
struct rsarray_t *array;
int a =0, b = 1, c = 2;
array = rsarray_ins_tail (NULL, &amp;a);
array = rsarray_ins_tail (array, &amp;b);
array = rsarray_ins_tail (array, &amp;c);
/* array now contains pointers to a, b and c */
</pre>
<p>You may not always want to store many references to the variable, though. For example, at this point in the code we may need to modify the values of a, b and c and we don&#8217;t want those modifications to take effect on the values already stored in the array. To this end, I provide a function <tt>rsa_dup</tt>, which will make a duplicate of any object passed to it (with the size of the object specified by the user). The above code can then be written as:</p>
<pre name="code" class="c++" style="font:small;">
int a = 0, b = 1, c = 2;
array = rsarray_ins_tail (NULL, rsa_dup (&amp;a, sizeof a));
array = rsarray_ins_tail (array, rsa_dup (&amp;b, sizeof b));
array = rsarray_ins_tail (array, rsa_dup (&amp;c, sizeof c));
/* array now contains copies of a, b and c */
a = 42; /* Element 0 of array is still 0 */
/* Change the value of b */
*(int *)array-&gt;array[1] = 43;
</pre>
<p>The above is still a little dirty. The duplication of the variable in the insertion is bound to lead to problems, and the verbose semantics of accessing an element (seen in the last line) is simply unreadable. They can both be fixed with macros:</p>
<pre name="code" class="c++" style="font:small;">
/* unsafe macro, 'x' should not have side-effects */
#define RSA_INS_TAIL(a,x) \
   rsarray_ins_tail (a, rsa_dup (&amp;x, sizeof x))
#define RSA_INDEX(a,i,t)   \
   *(t *)a-&gt;data[i]
...
int a = 0, b = 1, c = 2;
array = RSA_INS_TAIL (NULL, a);
array = RSA_INS_TAIL (array, b);
array = RSA_INS_TAIL (array, c);
/* array now contains copies of a, b and c */
a = 42; /* Element 0 of array is still 0 */
/* Access the second element i.e. array[1] */
RSA_INDEX (array, 1, int) = 43;
</pre>
<p>All well and good, how do we iterate? Easy, again.</p>
<pre name="code" class="c++" style="font:small;">
/* unsafe macro, 'x' should not have side-effects */
#define RSA_INS_TAIL(a,x) \
   rsarray_ins_tail (a, rsa_dup (&amp;x, sizeof x))
#define RSA_INDEX(a,i,t)   \
   *(t *)a-&gt;data[i]
#define RSA_LENGTH(a)    \
   a-&gt;used_len
...
struct rsarray_t *array = NULL;
int i;
/* Insert ten elements into array */
for (i=0; i&lt;10; i++) {
   array = RSA_INS_TAIL (array, i);
}
/* Change all the values stored in the array */
for (i=0; i&lt;RSA_LENGTH; i++) {
    RSA_INDEX (array, i, int) = 42 + i;
}
</pre>
<p>Freeing is as simple as freeing each element (because you are certain that there are no other references to it &#8211; the memory was allocated when we appended elements using <tt>rsa_dup</tt>, so no other code should be sharing the pointer), so we can simply iterate over the array and free each of the <tt>void*</tt>. In fact, since iteration over the array is a common occurrence, we can just go ahead and create a function to do it for us, so that we never have to write the actual loop:</p>
<pre name="code" class="c++" style="font:small;">
void rsarray_iterate (rsarray_t *a, void (*f) (void *))
{
   size_t i;
   for (i=0; iused_len; i++) {
      f (a-&gt;data[i]);
   }
}
...
struct rsarray_t * array = NULL;
[... here we append to the array ...]
/* Now we free each element */
rsarray_iterate (array, free);
/* Free the array that held all the elements */
free (array-&gt;array);
/* Free the structure that held the array */
free (array);
</pre>
<p>Because this isn&#8217;t rare either, we can put the above 3 lines into a function  call (for example) <tt>rsarray_free ()</tt>. Of course, once you&#8217;ve got that, you can use the resizable array to implement a host of other data structures. You can <a href="http://www.lelanthran.com/downloads/rsarray-1.0.0.tgz">download the code that does this over here</a>.
</p>
<p>
It was written about 8 years ago, and I&#8217;ve been using it every since (dynamic arrays are more useful than you think!). I go a lot further in the download above, as the download is actually written as a library, and uses the dynamic array as a basis to implement growable arrays, stacks and queues. Check the download for example usage, further avenues to explore, for example creating a ring-buffer, or implementing copy-construction so that composite datatypes consisting of pointers are sensibly copied when an insertion is made. </tt></p>
]]></content:encoded>
			<wfw:commentRss>http://lelanthran.com/deranged/?feed=rss2&#038;p=68</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Blackjack basic strategy</title>
		<link>http://lelanthran.com/deranged/?p=55</link>
		<comments>http://lelanthran.com/deranged/?p=55#comments</comments>
		<pubDate>Wed, 09 Feb 2011 10:06:03 +0000</pubDate>
		<dc:creator>lelanthran</dc:creator>
				<category><![CDATA[Data analysis]]></category>
		<category><![CDATA[Research]]></category>
		<category><![CDATA[Useful software]]></category>
		<category><![CDATA[blackjack]]></category>
		<category><![CDATA[cards]]></category>
		<category><![CDATA[gambling]]></category>

		<guid isPermaLink="false">http://lelanthran.com/deranged/?p=55</guid>
		<description><![CDATA[Gambling Games There are two types of gambling games: Those that, even after the game has started, allows the player make decisions. Those that, after the game has started, doesn&#8217;t allow the player any further decisions. An example of the &#8230; <a href="http://lelanthran.com/deranged/?p=55">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h3>Gambling Games</h3>
<p>There are two types of gambling games:</p>
<ol>
<li>Those that, even after the game has started, allows the player make decisions.</li>
<li>Those that, after the game has started, doesn&#8217;t allow the player any further decisions.</li>
</ol>
<p>An example of the latter are slot machines &#8211; once you put your money in and hit the button, no further decisions from you can influence the outcome. Roulette is like this as well: once the dealer announces &#8220;no more bets&#8221;, you can only watch and hope that you will win.</p>
<p>An example of the former are games like poker, where even after you start the game, you can decide to draw more cards, bet more money or simply give up and lose whatever you already bet. Blackjack is also an example of a game that lets the player steer the outcome slightly &#8211; after you start the game, further actions taken by the player  can cause a win or loss.</p>
<h3>Enter Blackjack</h3>
<p>I&#8217;m not going to give a full treatment of the rules of blackjack (which, at any rate, differ from casino to casino) nor the theory behind it in a tiny blog post. I&#8217;m just going to point out that there are some actions that the player should take in certain conditions that provide the best possible chance that the player will win.</p>
<p>As an example, lets say the dealers face-up card is a five, and the players two cards are a pair of aces. In this situation, the best odds for the player are to split the aces. Or if the dealers face-up card is a nine while the player has an ace and an eight, then the best course of action would be for the player to stand.<sup>[1]</sup></p>
<p>The rules are simple (although they differ slightly based on what the different casinos allow), but they are horribly difficult to remember, especially when the test involves going to a casino and playing for money! Luckily, your blogging friend has some assistance in this regard. I&#8217;ve written a simple <a href="http://www.lelanthran.com/apps/bj_trainer/index.html" target="_blank">online blackjack training program</a>. You play blackjack, and it tells you whenever you make a choice that is sub-optimal.</p>
<p>Unlike other blackjack programs which even do your counting for you, this program tries to make you do all your own work, hence you should be prepared once you hit the casino for real.</p>
<p>Note that it is still slightly incomplete &#8211; I need to implement &#8220;splitting&#8221; &#8211; but the advice is given, and the game maintains a bankroll for the player, and the player can hit/stand/double/surrender. As usual, let me know if this is useful to you; it&#8217;s hardly worth my time to implement the &#8220;splitting&#8221; if no one is using this program.</p>
<p><sup>[1]</sup>Note that I&#8217;m ignoring the issue of insurance. Insurance is a suckers bet, and should be only taken if you have been keeping accurate count of the cards going past. But if you&#8217;ve been keeping accurate count of the cards going past, you won&#8217;t need the insurance anyway. So don&#8217;t take it. Ever.</p>
]]></content:encoded>
			<wfw:commentRss>http://lelanthran.com/deranged/?feed=rss2&#038;p=55</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The death penalty and its effect on crime.</title>
		<link>http://lelanthran.com/deranged/?p=37</link>
		<comments>http://lelanthran.com/deranged/?p=37#comments</comments>
		<pubDate>Tue, 25 Jan 2011 23:21:21 +0000</pubDate>
		<dc:creator>lelanthran</dc:creator>
				<category><![CDATA[Data analysis]]></category>
		<category><![CDATA[Research]]></category>
		<category><![CDATA[death penalty /1]]></category>

		<guid isPermaLink="false">http://lelanthran.com/deranged/?p=37</guid>
		<description><![CDATA[Well, I got interested in this little question some time back. Whenever the headlines are filled with a gruesome enough crime, the newspapers are inundated (in their &#8220;letters&#8221; section) with calls to &#8220;Bring Back The Death Penalty&#8221;. Of course, the &#8230; <a href="http://lelanthran.com/deranged/?p=37">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Well, I got interested in this little question some time back. Whenever the headlines are filled with a gruesome enough crime, the newspapers are inundated (in their &#8220;letters&#8221; section) with calls to <strong>&#8220;Bring Back The Death Penalty&#8221;</strong>. Of course, the purpose of the justice system in most countries, in the event that a criminal has been proved to be guilty beyond reasonable doubt, is not to exact revenge on the criminal on behalf of the victims, but to ensure that the crime is punished, and (pay attention, this is the important bit) to <em>rehabilitate the criminal</em>.</p>
<p>Hence, in South Africa, our prisons are run by the department of <strong>Correctional</strong> Services (note the emphasis!), and not by the department of Revenge Meted Out Services. Most other countries follow something similar &#8211; the idea behind law and justice is to make society safer for everyone, and not to simply punish criminals, nor to use the system as a means of revenge. This brings to the fore the question <em>&#8220;Is capital punishment a significant enough deterrent to capital crimes?&#8221;</em>. In other words, does having and handing out the death penalty frighten criminals into not becoming criminals?</p>
<p>There are basically two sides to this (I&#8217;m only going to mention both, not argue them):</p>
<ol>
<li>The death penalty does reduce crime.</li>
<li>The death penalty has no effect on crime.</li>
</ol>
<p>A number of arguments in support of either side eventually makes it way around the papers (via the &#8220;letters to the editor&#8221; section), with the most visible ones being:</p>
<ol>
<li>Criminals <strong>are</strong> afraid to commit crime if they think they will be killed if caught, hence crime rates drop.</li>
<li>Criminals think they won&#8217;t get caught (which is why they commit in the first place), hence have no fear of the death penalty.</li>
<li>A person who is terminated by the state would not get another opportunity to kill again.</li>
<li>A death penalty may actually raise the number of actual murders, as then criminals will have to ensure that all witnesses (for example a rape victim) are killed. With no death penalty a witness or two may survive.</li>
<li>Criminals are more likely to snitch on their accomplices when caught if there is a possibility that their sentence would be reduced from &#8220;death&#8221; to &#8220;life imprisonment&#8221;.</li>
</ol>
<p>Now I&#8217;m pretty certain that murderers don&#8217;t typically complete a course in statistics, so I decided to look it up for myself. Luckily, there are some countries that have and routinely hand out the death penalty, and some countries that don&#8217;t. So if there was a relationship between handing out the death penalty and crime rates (whether a positive or a negative one), we should be able to tell.<sup>[1]</sup></p>
<p>In other words, the list of the  safest countries should contain more death-penalty countries in the list, than a list of most dangerous countries if the death penalty were to make the society safer. The corollary (that the list of countries that hand out the death penalty should contain more countries that are safer) is a topic for a new blog post sometime next week.</p>
<p>So, we know what to expect: that the list of safest countries should contain less death-penalty-countries, and that the list of most dangerous countries should contain fewer death-penalty-countries. Lets actually check the both lists and see.</p>
<p><em>Note that I got the data on <a href="http://en.wikipedia.org/wiki/List_of_countries_by_intentional_homicide_rate">homicide rates from here</a>, and the list of countries that hand out <a href="http://www.infoplease.com/ipa/A0777460.html">death-penalty sentences from over here</a></em></p>
<p>Lets start by looking at top ten the countries with the highest rates of crime. Lets check how many of them have the death penalty.</p>
<table border="”1?" cellspacing="”0?" cellpadding="”0?" width="”390?">
<tbody>
<tr>
<td>Country</td>
<td>Murder Rate</td>
<td>Death-Penalty</td>
<td>No Death-Penalty</td>
</tr>
<tr>
<td>El Salvador</td>
<td>71</td>
<td></td>
<td>X</td>
</tr>
<tr>
<td>Honduras</td>
<td>67</td>
<td></td>
<td>X</td>
</tr>
<tr>
<td>Jamaica</td>
<td>58</td>
<td>X</td>
<td></td>
</tr>
<tr>
<td>Guatemala</td>
<td>52</td>
<td>X</td>
<td></td>
</tr>
<tr>
<td>Venezuela</td>
<td>49</td>
<td></td>
<td>X</td>
</tr>
<tr>
<td>Trinidad and Tobago</td>
<td>43</td>
<td>X</td>
<td></td>
</tr>
<tr>
<td>Colombia</td>
<td>35</td>
<td></td>
<td>X</td>
</tr>
<tr>
<td>South Africa</td>
<td>34</td>
<td></td>
<td>X</td>
</tr>
<tr>
<td>Belize</td>
<td>33.4</td>
<td>X</td>
<td></td>
</tr>
<tr>
<td>Brazil</td>
<td>25.2</td>
<td></td>
<td>X</td>
</tr>
<tr>
<td>Totals</td>
<td></td>
<td>4</td>
<td>6</td>
</tr>
</tbody>
</table>
<p>It seems, of the most dangerous countries, that four of them hand out the death penalty, and six of them don&#8217;t.</p>
<p>How about the top ten safest countries (i.e. those with the least crime)? How many of them have the death penalty?</p>
<table border="”1?" cellspacing="”0?" cellpadding="”0?" width="”390?">
<tbody>
<tr>
<td>Norway (excluding attempts)</td>
<td>0.6</td>
<td></td>
<td>X</td>
</tr>
<tr>
<td>Oman</td>
<td>0.59</td>
<td>X</td>
<td></td>
</tr>
<tr>
<td>Lebanon</td>
<td>0.57</td>
<td>X</td>
<td></td>
</tr>
<tr>
<td>Morocco</td>
<td>0.53</td>
<td></td>
<td>X</td>
</tr>
<tr>
<td>Brunei</td>
<td>0.5</td>
<td></td>
<td>X</td>
</tr>
<tr>
<td>Hong Kong</td>
<td>0.49</td>
<td></td>
<td>X</td>
</tr>
<tr>
<td>Japan</td>
<td>0.44</td>
<td>X</td>
<td></td>
</tr>
<tr>
<td>Singapore</td>
<td>0.38</td>
<td>X</td>
<td></td>
</tr>
<tr>
<td>Iceland (excluding attempts)</td>
<td>0.31</td>
<td></td>
<td>X</td>
</tr>
<tr>
<td>Liechtenstein (excluding &#8230;</td>
<td>0</td>
<td></td>
<td>X</td>
</tr>
<tr>
<td>Totals</td>
<td></td>
<td>4</td>
<td>6</td>
</tr>
</tbody>
</table>
<p>Hmm &#8230; it seems that this weeks  question is answered &#8211; <em>the death penalty makes no difference to the top ten most dangerous or safest countries</em>.</p>
<p>But is this actually truly reflective of reality for those countries that are not in the extremes? Perhaps we should rather check <em>all</em> the countries to see, on average, which are safer to live in &#8211; the death-penalty-countries or the non-death-penalty countries.</p>
<p>Check back next week for the answer to the question <em>&#8220;On average, do the countries with more crime hand out the death penalty? Is there a relationship between the crime rate and capital punishment?&#8221;</em></p>
<p>(To Be Continued &#8230;)</p>
<p>Footnotes<br />
[1] As the death penalty is only handed out for actual murder, I use the word &#8220;crime&#8221; to mean &#8220;homicide&#8221;. Not even the most ardent death penalty supporter would expect the death penalty to be handed out for shoplifting, hence there is no need to look at shoplifting statistics to see if they correlate with death penalty.</p>
]]></content:encoded>
			<wfw:commentRss>http://lelanthran.com/deranged/?feed=rss2&#038;p=37</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Downloading videos via torrents</title>
		<link>http://lelanthran.com/deranged/?p=20</link>
		<comments>http://lelanthran.com/deranged/?p=20#comments</comments>
		<pubDate>Fri, 21 Jan 2011 14:06:31 +0000</pubDate>
		<dc:creator>lelanthran</dc:creator>
				<category><![CDATA[Data analysis]]></category>
		<category><![CDATA[Research]]></category>

		<guid isPermaLink="false">http://lelanthran.com/deranged/?p=20</guid>
		<description><![CDATA[How to spot torrents of fake videos. <a href="http://lelanthran.com/deranged/?p=20">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Flelanthran.com%2Fderanged%2F%3Fp%3D20&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden;  width:450px; height:65px"></iframe>
<p><a href="http://www.lelanthran.com/downloads/torrent.pdf">See the preliminary data gathered</a>.</p>
<p>Many of the torrents being uploaded to popular sites are actually fake. Very many of the comments by the users of such sites express anger and dismay that there are people out there who seed fake  torrents (ironic, since these same users are infringing copyright by downloading the torrent).</p>
<p>For the most part, as far as I can tell when surfing the torrent search sites (purely in the spirit of scientific enquiry, you  understand), the <em>fakes</em> are simply files that can be read with only a single video player, and the fake videos display  nothing but a message informing the viewer where the video player can be purchased. In other words, the torrents are spam, but you can&#8217;t tell that until you&#8217;ve exhausted your entire bandwidth downloading this fake file.</p>
<p>As there is a financial incentive for these companies to keep their torrents highly visible, it&#8217;s obvious that they are the ones seeding the fakes, hence you see the latest Hollywood movie for download with approximately 13000 seeds and a few thousand leeches, you assume that the torrent must be legit because:</p>
<ol>
<li> Surely there cannot be 13000 people are are seeding a fake     file, and,</li>
<li> Even if 13000 people are silly enough to do this, surely the    few thousand leeches must mean that the torrent is legit.</li>
</ol>
<p>Sadly that is not the case &#8211; a botnet can easily exceed 13000 computers, and the spammers behind this obviously know that they need to leech off the seeders or else the downloaders will get suspicious. Hence the spammers will both seed and leech at the same time. If this is the case, then they obviously will prefer to leech as little as  possible to save what they can of their bandwidth to allow their victims to download.</p>
<p>Since I figured that they would have a set percentage of their botnet as leeches, I gathered a little data from isohunt &#8211; see the above-mentioned pdf file for the actual data. My conclusions?</p>
<ol>
<li>With the exception of a single torrent, all of the negatively       rated torrents (i.e. the fakes) had leeches that were not more        than 28% of the seeders.</li>
<li>With the exception of a single torrent, all of the positively       rated torrents (i.e. legit torrents) had leeches that were not       less than 30% of the seeders.</li>
</ol>
<p>The outliers are statistically expected &#8211; some of the  fakes will have actual people downloading (taking the percentage of leeches higher) while some of the legit torrents will have more people downloading than seeding anyway (taking the percentage down).</p>
<p>So, in order to determine a torrents legitimacy for seeds of 10k or more, you can rely on the percentage of leeches as being roughly  indicative of it&#8217;s legitimacy. In other words, any torrent which has a ratio of seeders:leechers that is around  10:3 is probably a fake.</p>
<p>I also note that the sample size (98 torrents ordered by number of seeds) is a little small, while the sampling (all from the Video/Movie category) is not random at all to draw firm conclusions from. The only <em>really</em> safe conclusion one should draw from this result is that it indicates that further  research might be useful.<br />
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Flelanthran.com%2Fderanged%2F%3Fp%3D20&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden;  width:450px; height:65px width:450px; height:65px"></iframe></p>
]]></content:encoded>
			<wfw:commentRss>http://lelanthran.com/deranged/?feed=rss2&#038;p=20</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

