in reply to Expecting a 100 solutions, getting just 1

It's hard to understand what you are trying to do. What is a "trial solution"? Is there some randomness involved?

If you want to store multiple somethings, you best use an array:

my @interesting_values; for (1...1000) { my $r = rand; if ($r > 0.5) { push @interesting_values, $r; } }

See also: perldata, perlintro.

This part of your code:

for ($i=0; $i<$i_max; $i++) { if ($z < $z_best) {

seems to be entirely useless, because you never set $z to anything, and there's no reason to iterate over that if-statement, since nothing in there is random or depends on $i.

Also please Use strict and warnings and declare your variables with my.

Replies are listed 'Best First'.
Re^2: Expecting a 100 solutions, getting just 1
by doozy (Initiate) on Apr 17, 2012 at 08:58 UTC
    There is randomness involved because 1 value of $A is a guess. The objective is to choose a pair of guesses A and B which minimize the error term. Also, will declaring the variables with my get around the scalar property of $? Thanks for the tip about the warnings.
      There is randomness involved

      Where? I don't see any calls to rand, or other sources of randomness in your code

      because 1 value of $A is a guess.

      A guess for what?

      The objective is to choose a pair of guesses A and B which minimize the error term.

      What's the error term? How is it calculated?

      Also, will declaring the variables with my get around the scalar property of $?

      No. You're not supposed to work around it, you are supposed to use variables starting with @ if you want multiple values in one variable.

        No. You're not supposed to work around it, you are supposed to use variables starting with @ if you want multiple values in one variable.

        Thanks for the clarification. I misused the term random in this case. The value of $A is a guess for a variable in a set of equations (which I didn't include here), uniformly distributed between 0 and A_max. The error term is "included" in the posted script as part of a comment.