[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Random remailing script had. Help.
Xenon refers to the random choice script I sent him, and asks:
>> # choose random remailers
>> $first = rand($#remailers+1);
>> $second = rand($#remailers);
>> $second++ if $second >= $first;
> 1) Why not just $second = rand ($#remailers+1), instead of the two line
> $second routine? (And why did I have to add the +1...).
If there are N remailers, then $#remailers will be N-1. (It's the value
of the last index into the array, but the array starts at zero.)
rand($#remailers+1) is a floating point number in the range [0,N)
(including 0, but not including N). Hey, we're missing some int()
operations here; it should be like this:
>> # choose random remailers
>> $first = int(rand($#remailers+1));
>> $second = int(rand($#remailers));
>> $second++ if $second >= $first;
Now, $first is an integer in the range [0,N-1], which is correct for
indexing into the array of available remailers. When it comes to choosing
$second, we do not want to choose the same value as $first; for example,
if N is 5 then we want to choose $first from the set {0,1,2,3,4}, and if
we happen to choose $first=2 then we want to choose $second from the set
{0,1,3,4}. The two-line calculation of $second will do that.
> 2) How do I output the variables as csh environmental variables that stick
> around after the perl script has executed? I usually use 'setenv' but perl
> didn't like that.
You will have to have csh parse the output of the perl script. For
example, have the perl script print some csh-compatible "setenv" commands,
with something like
print "setenv A$cycle $remailers[$first]\n";
print "setenv B$cycle $remailers[$second]\n";
and have the csh script execute the perl script and parse its output
using something like this:
eval `perl perl-script`
BTW, don't ever write csh scripts. See Tom Christiansen's periodic FAQ
posting in comp.unix.shell.
> I may have screwed it up, as Alan originally had no +1 in the $first line,
> and had -1 in the next line, but it never outputed "Six" then.
That was a bug, which you fixed.
--apb (Alan Barrett)