The attached patch file improves the randomness of
rand_image and adds the capability to select a random
image from a directory without configuring every file
in the directory.
For the first change, the line:
my $pic = $files[rand @files];
is changed to:
my $pic = $files[int(rand @files) + 1];
This improves the randomness in my testing.
Secondly I added:
will randomly display all the
to 0, the program will only
qw (" section below.
file in the directory is an
checking at ALL to confirm this
a '.'.
my $use_allfiles = 1;
to the configuration section (really only adding a
single variable), and the following code immediately
before the random file selection:
if ( $use_allfiles ) {
undef @files;
opendir( DIR, $basedir )
or die "Can't opendir $basedir: $! \n";
while ( defined( my $file = readdir( DIR ) ) ) {
push @files, $file unless ( $file =~ /^./ );
}
}
Hope these are useful to you
Patch file
Logged In: YES
user_id=313586
Thanks for this.
Could you redo your changes against the 1.10 version from
the CVS as this patch fails to apply. If for some reason
you can't do that I have no objection to doing it manually
but I am a little busy at the moment so it might not go in
quite as soon.
Logged In: YES
user_id=57537
Certainly can update my patch to your CVS version. I'd like
to page through your new code (since the patch didn't apply)
first, and I should probably split the patch into two for the fix
(for increased randomness) and the enhancement (for the
display of directory contents). I will try to get to it in the next
couple of days, otherwise after I get back from holiday at the
end of August.
Logged In: YES
user_id=34146
my $pic = $files[rand @files];
my $pic = $files[int(rand @files) + 1];
As far as I can see, see only difference that this patch
would make would be be to break the program.
The addition of the int call has no effect as an array index
is automatically converted to an integer anyway.
The "+ 1" has the effect of pushing the reuqested index off
the end of the array - thereby giving an "uninitialised
value" error under "use warnings".
A longer explaination follows:
@files in a scalar context gives the number of elements in
the array @files. Let's assume this is 10.
rand 10 gives a number between 0 and just less than 10. It
will never return 10.
int(rand 10) will therefore give an integer between 0 and 9.
Adding one to this gives an integer between 1 and 10.
In a 10 element Perl array, the valid indexes are 0 to 9.
Your expression therefore a) will never return the first
element (index 0) and b) will try to return an element with
index 10 which doesn't exist.
Can you perhaps give some more explaination of the way you
designed and tested this patch?
Dave...
Logged In: YES
user_id=57537
my $pic = $files[rand @files];
my $pic = $files[int(rand @files) + 1];
There are days when I have flashes of stupidity. davorg's
point regarding the index is correct.
On receiving his note, I built a test script using the two
approaches (after removing the inappropriate +1). The
array contained 10 elements. Running 100,000
iterations, both approaches have a mean of 10,000 as
one would hope and a standard deviation of around 89.
The differences are not significant.
In production, we have found that my $pic = $files[rand
@files]; does not give an even distribution of results,
but that my $pic = $files[int(rand @files)]; does.
My next step is to set up a test case on the production
platform. I will let you know.
I'll definately do a separate patch file for my proposed
enhancement.