{"id":11117,"date":"2015-06-23T18:27:49","date_gmt":"2015-06-23T18:27:49","guid":{"rendered":"http:\/\/www.oracletutoring.ca\/blog\/?p=11117"},"modified":"2015-06-23T18:27:49","modified_gmt":"2015-06-23T18:27:49","slug":"perl-random-walk-simulation-code-explanation","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/perl-random-walk-simulation-code-explanation\/","title":{"rendered":"Perl random walk simulation:  code explanation"},"content":{"rendered":"<h1>The tutor continues with some explanation of last post&#8217;s Perl random walk simulation.<\/h1>\n<p>Below is the Perl code from last post, this time with some comments (recall that # denotes a comment in Perl):<br \/>\n<span style=\"font-family:monospace;color:brown\"><br \/>\n#!\/usr\/bin\/perl <span style=\"color:orange\"> # needed for Linux (Unix)<\/span><br \/>\nprint &#8220;Welcome to the random walk simulation.\\n\\n&#8221;;<br \/>\nprint &#8220;Enter the number of steps you&#8217;d like, please.\\n\\n&#8221;;<br \/>\n$steps=&lt;STDIN&gt;;<span style=\"color:orange\"># &lt;STDIN&gt; fetches user input<\/span><br \/>\nchomp $steps;<span style=\"color:orange\"># chomps the newline from the input<\/span><br \/>\n$pos=0;<span style=\"color:orange\"># walker&#8217;s position starts as 0<\/span><br \/>\nfor($i=0;$i<$steps;$i++){<br \/>\n$rn=int(rand(100)); <span style=\"color:orange\"># Perl random number function<\/span><br \/>\nif($rn % 2==0){<span style=\"color:orange\"># % is Perl&#8217;s modulus operator<\/span><br \/>\n$pos=$pos-1;<br \/>\n}<span style=\"color:green\"># if the random number is even, walker steps left<\/span><br \/>\nelse{<br \/>\n$pos++; <span style=\"color:orange\"># adds 1 to the $pos value<\/span><br \/>\n}<span style=\"color:green\"># otherwise, walker steps right<\/span><br \/>\n}<br \/>\nif($pos<0){\n$dir=\"left\";\n$pos*=-1;<span style=\"color:orange\"># means $pos=$pos*-1;<\/span><br \/>\n}<br \/>\nelse{<br \/>\n$dir=&#8221;right&#8221;;<br \/>\n}<br \/>\nprint &#8220;The position, after $steps steps, is $pos units $dir\\n\\n&#8221;;<br \/>\n<\/span><\/p>\n<p>This random walk is a great springboard to summer posts about probability.  Of course, we never need a reason to return to Perl:)<\/p>\n<p>Source:<\/p>\n<p>McGrath, Mike.  <u>Perl in easy steps<\/u>.  Southam:  Computer Step, 2004.<\/p>\n<p>Jack of <a href=\"https:\/\/www.oracletutoring.ca\">Oracle Tutoring by Jack and Diane,<\/a> Campbell River, BC.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The tutor continues with some explanation of last post&#8217;s Perl random walk simulation. Below is the Perl code from last post, this time with some comments (recall that # denotes a comment in Perl): #!\/usr\/bin\/perl # needed for Linux (Unix) &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/perl-random-walk-simulation-code-explanation\/\"> <span class=\"screen-reader-text\">Perl random walk simulation:  code explanation<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105,3],"tags":[238,173,889,718,887],"class_list":["post-11117","post","type-post","status-publish","format-standard","hentry","category-computer-science","category-math","tag-perl-comments","tag-perl-programming","tag-perl-random-walk-simulation","tag-probability","tag-random-walk"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/11117","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/comments?post=11117"}],"version-history":[{"count":22,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/11117\/revisions"}],"predecessor-version":[{"id":11139,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/11117\/revisions\/11139"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=11117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=11117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=11117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}