zentara has asked for the wisdom of the Perl Monks concerning the following question:

UPDATE: Sorry, I didn't word my original question clearly, I was looking for a function to return 1 or -1 randomly. My code below returns an even 50-50 distribution. I was looking for an even distribution, but random at any one time.

Hi, I recently had a need to run an loop and each time thru the loop I needed to alternatively add or subtract a variable. So I came up with:

#!/usr/bin/perl @hits = (-1,1); for(1..10){ print toggle(),"\n"; } sub toggle{ push (@hits,shift(@hits)); return $hits[0]; }
I have a feeling that there is some "golfed way" of doing this in a more simple fashion. I thought about taking the output of rand, and make the number 1 if > .5 and -1 if less than .5. Anyone have ideas? Thanks.

Replies are listed 'Best First'.
Re: random negative toggle function
by !1 (Hermit) on Nov 15, 2003 at 19:37 UTC

    As another way to do it:

    perl -le'print +(-1,1)[rand 2]' # or as a sub sub toggle { (-1,1)[rand 2] }

    Of course, that's the short and simple way. I hope this helps you with your problem.

Re: random negative toggle function
by Limbic~Region (Chancellor) on Nov 15, 2003 at 19:14 UTC
    zentara,
    I am replying after you made your update:
    #!/usr/bin/perl -w use strict; print toggle(), $/ for 1 .. 10; sub toggle { int rand 2 ? 1 : -1; }
    Cheers - L~R
Re: random negative toggle function
by jeffa (Bishop) on Nov 15, 2003 at 21:26 UTC
    How about Abigail-II's Tie::FlipFlop?
    use Tie::FlipFlop; tie my $flipflop => Tie::FlipFlop => -1,1; print $flipflop for 1..10;
    These days, i like to search the CPAN before i add YAUM (yet another utility method) to my YAOASL (yet another obscure and specialized library). The CPAN has become my utility library.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: negative toggle function
by CombatSquirrel (Hermit) on Nov 15, 2003 at 18:06 UTC
    How about this:
    my $toggle = -1; for (1..10) { print $toggle *= -1, $/; }

    CombatSquirrel.
    Entropy is the tendency of everything going to hell.
Re: negative toggle function
by EdwardG (Vicar) on Nov 15, 2003 at 18:43 UTC

    For a solution perhaps more sophisticated that the classic negate-$x-each-iteration perhaps it would be illuminating to look at Easy binary toggle at fixed intervals, or perhaps Flipper, and of course, merlyn has probably written an article about this :)

    IMHO the simplest is:

    d:\>perl -e "print +($a += 1) %= 2 for 1..10" 1010101010
Re: random negative toggle function
by EdwardG (Vicar) on Nov 15, 2003 at 19:06 UTC

    Random 1 or 0 is even easier

    d:\>perl -e "print int rand()+.5 for 1..10" 1110110000
Re: random negative toggle function
by zentara (Cardinal) on Nov 16, 2003 at 22:50 UTC
    Thanks for the good answers. In case you were wondering, I was trying to simulate random motion, like deviation from a straight line trajectory. I needed the -1 instead of a 0 because it would plug directly into the motion equation.