in reply to random number generation problem

The problem is that you forgot to declare '$newvar' as a local variable, so each time through the loop the result of the previous assignment is still there.

Switch it to my($newvar), or reset it each time.

Not having local variables also means that $minlength is incremented by ('+=') a random number each time. If you want to pick a number from 5 to 15, you are a little off track.

As a creative suggestion, instead of doing all that work, why not just this?
my(@vchars) =(a..z,A..Z,0..9); sub rwg { return join ('', map { $vchars[int(rand($#vchars+1))] } 5 .. 5+int(rand(10))); }
Change 5 to $minlength, 10 to ($maxlength-$minlength) if so desired.

Additionally, remember that rand() picks a number up to the maximum, which in this case is $#vchars, so the last letter will probably not get picked unless you +1 it.

Replies are listed 'Best First'.
Re: Re: random number generation problem
by cghost23 (Novice) on Apr 27, 2001 at 01:39 UTC
    thanks a lot for the code, but
    return join ('', map { $vchars[int(rand($#vchars+1))] } 5 .. 5+int(rand(10)));
    seems to also return strings less than 5 chars longs
      Would you believe it if I said that it should read:
      1 .. 5+int(rand(10)))
      Since we aren't counting from five to some number in the range of 5-15, but from 1!

      Thanks to cghost23 and Chmrr for the tip.