{"id":11098,"date":"2015-06-23T03:19:30","date_gmt":"2015-06-23T03:19:30","guid":{"rendered":"http:\/\/www.oracletutoring.ca\/blog\/?p=11098"},"modified":"2015-06-23T03:19:30","modified_gmt":"2015-06-23T03:19:30","slug":"perl-simulation-a-random-walk","status":"publish","type":"post","link":"https:\/\/www.oracletutoring.ca\/blog\/perl-simulation-a-random-walk\/","title":{"rendered":"Perl simulation:  a random walk"},"content":{"rendered":"<h1>The tutor returns to Perl programming with a random walk simulation.<\/h1>\n<p>A really simple conception of a random walk is that the walker starts at 0 on the number line, then makes consecutive steps, each of which can either be left or right.  The probability of left or right can be equal or different.  After a certain number of steps, the position of the walker is taken.<\/p>\n<p>Perl, with its rand() function, facilitates easy simulation of a random walk:<br \/>\n<span style=\"font-family:monospace;color:brown\"><br \/>\n#!\/usr\/bin\/perl<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;;<br \/>\nchomp $steps;<br \/>\n$pos=0;<br \/>\nfor($i=0;$i<$steps;$i++){<br \/>\n$rn=int(rand(100));<br \/>\nif($rn % 2==0){<br \/>\n$pos=$pos-1;<br \/>\n}<br \/>\nelse{<br \/>\n$pos++;<br \/>\n}<br \/>\n}<br \/>\nif($pos<0){\n$dir=\"left\";\n$pos*=-1;\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>Next post I&#8217;ll explain this program:)<\/p>\n<p>Sources:<\/p>\n<p>Ross, Sheldon M.  <u>Probability Models<\/u>, Fifth Ed.  San Diego:  Academic Press, Inc., 1993.<\/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 returns to Perl programming with a random walk simulation. A really simple conception of a random walk is that the walker starts at 0 on the number line, then makes consecutive steps, each of which can either be &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.oracletutoring.ca\/blog\/perl-simulation-a-random-walk\/\"> <span class=\"screen-reader-text\">Perl simulation:  a random walk<\/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":[886,173,888,887],"class_list":["post-11098","post","type-post","status-publish","format-standard","hentry","category-computer-science","category-math","tag-computer-simulation","tag-perl-programming","tag-perl-rand-function","tag-random-walk"],"_links":{"self":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/11098","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=11098"}],"version-history":[{"count":18,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/11098\/revisions"}],"predecessor-version":[{"id":11116,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/posts\/11098\/revisions\/11116"}],"wp:attachment":[{"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/media?parent=11098"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/categories?post=11098"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.oracletutoring.ca\/blog\/wp-json\/wp\/v2\/tags?post=11098"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}