CGI/Perl Guide | Learning Center | Forums | Advertise | Login Site Search: in Perl Guide PerlGuru Forums Learning Ctr

Home: Perl Programming Help: Intermediate:
Infectious Random Walk

Lynn
Novice

Sep 15, 2012, 6:47 PM

Post #1 of 2 (2380 views)
 Infectious Random Walk Can't Post
I am modifying the Random Walk model to include a certain number of targets. I have a 20x20 grid (x and y axis), it now must have n=3 targets randomly distributed in the grid. I must show the probability of the random walker hitting this target within 20 moves. I have two for loops. the first one for creating the random walk works but to show that it may hit a randomly distributed target does not. I need help with that part. Thanks

 Code
`while(\$count > 0) {     \$xpos = 0;     \$ypos = 0;      #Inner loop to perform each step of a random walk                                                                                    for (\$i = \$NO_OF_STEPS; \$i > 0; \$i--)     {         \$random_num = rand();         #Storing xpos, ypos into designated arrays                                                                                           \$xposArr[\$i] = \$xpos;         push(@yposArr, \$ypos);         if(\$random_num < 0.25) {             \$xpos--;         } elsif (\$random_num < 0.5) {             \$xpos++;         } elsif (\$random_num < 0.75) {             \$ypos--;         } else {             \$ypos++;         }     }      #Inner loop when walker hits a target                                                                                            for (\$i = \$NO_OF_STEPS; \$i > 0; \$i--)     {         \$random_num = rand();         \$xposArr[\$i] = \$xpos;         push(@yposArr, \$ypos);         if(\$random_num == \$n)         {             print "xpos \$xpos\t ypos \$ypos\t Hit by target\n";         }     }`

Laurent_R
Veteran / Moderator

Sep 16, 2012, 1:12 AM

Post #2 of 2 (2379 views)
 Re: [Lynn] Infectious Random Walk [In reply to] Can't Post
In the second inner loop, you are comparing \$random_num with \$n. But \$n isn't defined anywhere in the code you posted.

Even if it was defined, I can't see how this comparison would yield you the desired result. You are just comparing a random number with \$n, even assuming \$n is defined somewhere else, the probability of an exact match is very very low, close to 0 (on my system, rand returns a number with 16 decimal digits, the probability of an exact match is of the order of 1/1,000,000,000,000,000,; if you run this program 100 times second, the chances are that you will most probably not get a hit in 100,000 years). Actually, I don't even understand what you are trying to do with this second inner loop.

The first inner loop also has flaws: you are not checking that your moves don't get you out of the grid. You should have minimal and maximal values for \$xpos and \$ypos and decide what to do when using the random number would lead you to walk outside of the grid.

Final point: you should "use strict;" and "use warnings". This would give you a number of diagnostics on your code and declare most on your variables in limited lexical scope (with my).

 Announcements     PerlGuru Announcements Perl Programming Help     Frequently Asked Questions     Beginner     Intermediate     Advanced     Regular Expressions     mod_perl     DBI     Win32 Programming Help Fun With Perl     Perl Quizzes - Learn Perl the Fun Way     Perl Golf     Perl Poetry Need a Custom or Prewritten Perl Program?     I need a program that...     I Need a Programmer for Freelance Work     Throw Down The Gauntlet General Discussions     General Questions     Feedback     Tutorial/Article Suggestions for The Learning Cent     Internet Security Other Programming Languages     Javascript     PHP

 Search this forum this category all forums for All words Any words Whole Phrase (options) Powered by Gossamer Forum v.1.2.0