good chemistry is complicated, and a little bit messy -LW |
|
PerlMonks |
Re: A script with a loop is running my computer Out of memoryby jethro (Monsignor) |
on Feb 02, 2011 at 21:53 UTC ( [id://885862]=note: print w/replies, xml ) | Need Help?? |
First two off topic optimizations: If you used an array for %generations you wouldn't need to sort the keys (just store generation x into arrays slot x). Also max(keys %generations)==$generations. Also you are summing up a lot of random values between 0 and 5 (distributed evenly). Basically the average number of offsprings per person you will get is 2.5, if you do this a lot. Change your last line to
to see this effect. It also shows how to calculate the next generation directly if you don't mind to get a clean statistical average instead of your calculated numbers By the way, if you want to try to not have an evenly distributed number of offspring (i.e. families with two kids should be more common than with five, at least in our century), you might define an array with a different distribution:
and in your loop use the random number to get one random value out of the array:
In Section
Seekers of Perl Wisdom
|
|