hw1 solution

This is the solution to the first homework.

The working script is available here.

# Create a variable to store how many random were generated at the end.
$howmany = 0;
 
while ($total < 100) {
   $random = int(rand(13));
   $howmany = $howmany + 1;
   $total = $total + $random;
   print " This time the special number is $random.";
   if ($random > 0 and $random % 3 == 0) {
     print " Lucky you!!!";
   }
   print "\n";
 
}
print " Finished: I choosed $howmany random numbers, the total is $total.\n";

A couple of remarks:

  • Some of you did not understood that the condition to be tested (can the random number generated be divided by three?) has to be tested at any cycle, not just at the end.
  • There are many ways to do the same thing. If you want to know if another possibility is correct… TRY!!!
  • It’s up to you if to consider 0 a lucky number or not. For me, it’s not! 🙂
  • This script is and example of creating a variable ($howmany) to keep track of something that is not predictable at the begin.
Tagged ,

Leave a Reply

Your email address will not be published.