2018-11-10 18:19:00 +01:00
<?xml version="1.0" encoding="utf-8"?>
2019-03-06 10:42:59 +01:00
<?xml-stylesheet type="text/xsl" href="../assets/xml/rss.xsl" media="all"?> <rss version= "2.0" xmlns:dc= "http://purl.org/dc/elements/1.1/" xmlns:atom= "http://www.w3.org/2005/Atom" > <channel > <title > Caught in the Net (Posts about random)</title> <link > francescomecca.eu</link> <description > </description> <atom:link href= "francescomecca.eu/categories/random.xml" rel= "self" type= "application/rss+xml" > </atom:link> <language > en</language> <copyright > Contents © 2019 < a href="mailto:francescomecca.eu"> Francesco Mecca< /a> </copyright> <lastBuildDate > Wed, 06 Mar 2019 09:27:55 GMT</lastBuildDate> <generator > Nikola (getnikola.com)</generator> <docs > http://blogs.law.harvard.edu/tech/rss</docs> <item > <title > The Buridan's donkey in python</title> <link > francescomecca.eu/blog/2016/4/2/buridan_donkey/</link> <dc:creator > Francesco Mecca</dc:creator> <description > < div> < p> During the final weeks of my exam session I started reading a bit about python 3 using an excellent book: < a href="http://www.diveintopython.net/"> Dive into Python< /a> .
2018-11-10 18:19:00 +01:00
When I noted that python uses the < a href="https://en.wikipedia.org/wiki/Mersenne_Twister"> Mersenne Twister PRNG< /a> as well I decided to write another version of my < a href="http://francescomecca.eu/index.php/archives/207"> Buridan's donkey program< /a> .< /p>
< p> .. code:: python< /p>
< pre class="code literal-block"> < span> < /span> < span class="s s-Atom"> import< /span> < span class="s s-Atom"> random< /span> < span class="p"> ,< /span> < span class="s s-Atom"> sys< /span>
< span class="s s-Atom"> if< /span> < span class="k"> __< /span> < span class="s s-Atom"> name__< /span> < span class="o"> ==< /span> < span class="s s-Atom"> '__main__':< /span>
< span class="s s-Atom"> args< /span> < span class="o"> =< /span> < span class="nf"> list< /span> < span class="p"> ()< /span>
< span class="s s-Atom"> if< /span> < span class="o"> not< /span> < span class="s s-Atom"> sys< /span> < span class="p"> .< /span> < span class="s s-Atom"> stdin< /span> < span class="p"> .< /span> < span class="nf"> isatty< /span> < span class="p"> ()< /span> < span class="s s-Atom"> :< /span>
< span class="s s-Atom"> for< /span> < span class="s s-Atom"> line< /span> < span class="s s-Atom"> in< /span> < span class="s s-Atom"> sys< /span> < span class="p"> .< /span> < span class="nn"> stdin< /span> < span class="p"> :< /span>
< span class="s s-Atom"> if< /span> < span class="s s-Atom"> line< /span> < span class="p"> [< /span> < span class="o"> -< /span> < span class="mi"> 1< /span> < span class="p"> ]< /span> < span class="o"> is< /span> < span class="s s-Atom"> '\n':< /span>
< span class="s s-Atom"> line< /span> < span class="o"> =< /span> < span class="s s-Atom"> line< /span> < span class="p"> [:-< /span> < span class="mi"> 1< /span> < span class="p"> ]< /span>
< span class="s s-Atom"> args< /span> < span class="p"> .< /span> < span class="nf"> append< /span> < span class="p"> (< /span> < span class="s s-Atom"> line< /span> < span class="p"> )< /span>
< span class="nn"> else< /span> < span class="p"> :< /span>
< span class="s s-Atom"> args< /span> < span class="o"> =< /span> < span class="s s-Atom"> sys< /span> < span class="p"> .< /span> < span class="s s-Atom"> argv< /span> < span class="p"> [< /span> < span class="mi"> 1< /span> < span class="s s-Atom"> :< /span> < span class="p"> ]< /span>
< span class="s s-Atom"> argRange< /span> < span class="o"> =< /span> < span class="nf"> len< /span> < span class="p"> (< /span> < span class="s s-Atom"> args< /span> < span class="p"> )< /span>
< span class="s s-Atom"> for< /span> < span class="s s-Atom"> i< /span> < span class="s s-Atom"> in< /span> < span class="nf"> range< /span> < span class="p"> (< /span> < span class="s s-Atom"> argRange< /span> < span class="p"> )< /span> < span class="s s-Atom"> :< /span>
< span class="nf"> print< /span> < span class="p"> (< /span> < span class="nf"> str< /span> < span class="p"> (< /span> < span class="s s-Atom"> i< /span> < span class="o"> +< /span> < span class="mi"> 1< /span> < span class="p"> )< /span> < span class="o"> +< /span> < span class="s s-Atom"> '.'< /span> < span class="p"> ,< /span> < span class="s s-Atom"> args< /span> < span class="p"> .< /span> < span class="nf"> pop< /span> < span class="p"> (< /span> < span class="s s-Atom"> random< /span> < span class="p"> .< /span> < span class="nf"> randrange< /span> < span class="p"> (< /span> < span class="mi"> 0< /span> < span class="p"> ,< /span> < span class="nf"> len< /span> < span class="p"> (< /span> < span class="s s-Atom"> args< /span> < span class="p"> ))))< /span>
< /pre>
< p> This script works in a different way than the one in c++.
Rather than shuffling a list made by the entries in the arguments, it pops randomly one entry from the list till the list is empty.< /p>
< p> Not satisfied enough, I wrote also a telegram bot using the < a href="https://github.com/eternnoir/pyTelegramBotAPI"> telebot library< /a> that works as the script above but inside the telegram app.
The bot can be added to your contact list by simply searching for < a href="http://telegram.me/duridan_donkey_bot"> @duridan_donkey_bot< /a> (yes, a typo!)< /p>
< p> All the code is opensource and can be found on my github page.< /p>
< p> Francesco Mecca< /p> < /div> </description> <category > buridan donkey</category> <category > mersenne twister</category> <category > PesceWanda</category> <category > python</category> <category > random</category> <guid > francescomecca.eu/blog/2016/4/2/buridan_donkey/</guid> <pubDate > Sat, 02 Apr 2016 00:00:00 GMT</pubDate> </item> </channel> </rss>