{"id":2366,"date":"2008-07-26T10:33:32","date_gmt":"2008-07-26T08:33:32","guid":{"rendered":"http:\/\/vasil.ludost.net\/blog\/?p=2366"},"modified":"2008-07-26T10:33:32","modified_gmt":"2008-07-26T08:33:32","slug":"2008-07-26-day-of-the-sysadmin-hangover","status":"publish","type":"post","link":"https:\/\/vasil.ludost.net\/blog\/?p=2366","title":{"rendered":"2008-07-26 day of the sysadmin hangover"},"content":{"rendered":"<p>Last night while somewhat drunk around a discussion with Guninski (I owe him a beer) I wrote some test proggy&#8230;<\/p>\n<p>The question was as follows &#8211; if we have two sets with 2^16 elements, and each element can be from 0 do 2^32, what&#8217;s the chance for the intersection of both sets not to be the empty set. Because the quantity of alcohol wasn&#8217;t good for the math skills (and chervarium just swore at us and declined to calculate it), this is what I wrote to get some stats:<br \/>\n(to be noted that this is the first time I manage to use qsort() :) )<\/p>\n<p><code><br \/>\n#include &lt;stdio.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;sys\/types.h&gt;<br \/>\n#include &lt;sys\/fcntl.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<\/p>\n<p>#define LEN 65536<\/p>\n<p>#define NTEST 100<\/p>\n<p>static int compare(const void *a, const void *b)<br \/>\n{<br \/>\n        if (*((int *) a) &gt; *((int *) b)) return 1;<br \/>\n        if (*((int *) a) &lt; *((int *) b)) return -1;<br \/>\n        return 0;<br \/>\n}<\/p>\n<p>int do_whatever()<br \/>\n{<br \/>\n\tint s0[LEN],s1[LEN];<\/p>\n<p>        int i=0,j=0;<\/p>\n<p>        int rndsrc;<br \/>\n        rndsrc=open(\"\/dev\/urandom\",O_RDONLY);<br \/>\n        read(rndsrc,s0,(sizeof(int)*LEN));<br \/>\n        read(rndsrc,s1,(sizeof(int)*LEN));<br \/>\n        close(rndsrc);<\/p>\n<p>        qsort (s0,LEN,sizeof(int),compare);<br \/>\n        qsort (s1,LEN,sizeof(int),compare);<\/p>\n<p>\twhile ( (s0[i]!=s1[j]) && (i&lt;LEN) && (j&lt;LEN))<br \/>\n\t{<br \/>\n\t\tif (s0[i]&lt;s1[j]) { if (!i&lt;LEN) i++; }<br \/>\n\t\t\telse {if (!j&lt;LEN) j++;};<\/p>\n<p>\t}<br \/>\n\tif (s0[i]==s1[j]) return 1;<br \/>\n        return 0;<\/p>\n<p>}<\/p>\n<p>int main()<br \/>\n{<br \/>\n\tint i,num=0;<\/p>\n<p>\tfor (i=0;i&lt;NTEST;i++)<br \/>\n\t\tnum+=do_whatever();<br \/>\n\tprintf (\"tests %d true %d false %d\\n\",NTEST,num,NTEST-num);<br \/>\n\treturn 0;<br \/>\n}<br \/>\n<\/code><\/p>\n<p>Guninski was saying that the chances incline to 1, and seems to be right &#8211; according to this it&#8217;s more that 1:2, which is inclining to 1 :)<\/p>\n<p>And damn, Shopov, how could you buy me two beers after I&#8217;ve drunk so much. Half of the morning I wanted to be dead :).<\/p>\n<p>Whomever remembers more of the celebration &#8211; feel free to write.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last night while somewhat drunk around a discussion with Guninski (I owe him a beer) I wrote some test proggy&#8230; The question was as follows &#8211; if we have two sets with 2^16 elements, and each element can be from 0 do 2^32, what&#8217;s the chance for the intersection of both sets not to be [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2366","post","type-post","status-publish","format-standard","hentry","category-general"],"_links":{"self":[{"href":"https:\/\/vasil.ludost.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2366","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vasil.ludost.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vasil.ludost.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vasil.ludost.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vasil.ludost.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2366"}],"version-history":[{"count":0,"href":"https:\/\/vasil.ludost.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2366\/revisions"}],"wp:attachment":[{"href":"https:\/\/vasil.ludost.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vasil.ludost.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vasil.ludost.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}