[EMAIL PROTECTED] wrote:
I know it "shouldn't" make a difference, but we all know that things that
shouldn't, do. I wasn't seeding it explicitly, was letting perl handle
it.
Unless it's a bug, my theory is that mod_perl is keeping the old seed from
the last compile. Mod_perl does not recompile or even reinitialize the
script between calls. I've used this effect to maintain data within the
cgi
script.
The code is trvial.
my $fn = substr rand(), 2;
my $x = 0;
while (-e "$tempdir\\$fn.bat") {
print "<!-- batch file $tempdir\\$fn.bat already exists -->\n";
$fn = substr rand(), 2;
print "cannot find unique temp bat file" and die "cannot find unique
temp bat file" if $x > 10;
$x++;
}
The change I made was to put srand() above rand(). It's build 811. But
really, that's all beside the point. The core problem is rand() repeating
it's output.
Just for fun I did a little test.
for (1 .. 10000) {
my $fn = rand();
++$dup and next if exists $num{$fn};
$num{$fn} = 1;
}
print scalar keys %num, " unique numbers. $dup duplicates produced\n";
#########
8623 unique numbers. 1377 duplicates produced
That's really interesting. So rand() is pretty useless.
random <> unique.
If you want unique numbers also put your routine above in
your program and discard duplicates.
A couple of points
1. Random is not the same as unique (as already pointed out), flipping a
coin generates random numbers but not unique numbers.
2. A random number generator can generate duplicates and still be random
(of course it could also be repeating). Indeed, a random number
generator should, in general, generate duplicates. It is a special case
of random number generation where duplicates are not allowed.
3. If you are using a random number generator for a critical app then
you need to understand the number domain it is working in and have an
estimate of the number of unique values in the list. On my creaky old
win98 machine running perl v5.8.2 it appears there are about 32k unique
values that rand() returns. Note that rand returns a 15 digit number and
you might think there are 10^15 unique values but there are not.
#tries #unique
10000 8596
50000 25654
80000 29876
100000 31175
200000 32691
You can see that the number of uniques found is approaching 32K
asymptotically.
4. On my machine rand() without srand() produces a new list of numbers
every time.
5. Using a random number generator to generate unique files names seems
kind of odd. Why not use the date and time plus -e file operator and a
little character string magic (auto-increment a character string with
++) to generate the file names?
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs