Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: Opening random files then reading random lines from file.

by fame (Initiate)
on Apr 27, 2012 at 19:07 UTC ( #967701=note: print w/ replies, xml ) Need Help??


in reply to Re: Opening random files then reading random lines from file.
in thread Opening random files then reading random lines from file.

yeah most of my question has been answered. but i got it down now.. thanks all for the responses.. its greatly appreciated..

I knew how to get a random line read in a file but didnt quite understand how to get a random file opened and do what i needed until i started playing around with it and took advice of others and got a copy of perl cookbook..the following is what i came up with for what I need to be done

#!/usr/bin/perl my @names = qw(x.txt l.txt m.txt); my $random_file = $names[rand @names]; if($random_file eq "l.txt") { open FILE, "l.txt" or die "Error Opening: Cannot open file!"; @emails = <FILE>; close FILE; $email = $emails[int(rand($#emails +1))]; chomp($email); print "$email\n"; }elsif($random_file eq "x.txt"){ open FILE, "x.txt" or die "Error Opening: Cannot open file!"; @emails = <FILE>; close FILE; $email = $emails[int(rand($#emails +1))]; chomp($email); print "$email\n"; }elsif($random_file eq "m.txt"){ open FILE, "m.txt" or die "Error Opening: Cannot open file!"; @emails = <FILE>; close FILE; $email = $emails[int(rand($#emails +1))]; chomp($email); print "$email\n"; }

any feedback on this would also be appreciated... any suggestions or tips to better the way i am doing this? any and all information is always appreciated :) thanks again


Comment on Re^2: Opening random files then reading random lines from file.
Download Code
Re^3: Opening random files then reading random lines from file.
by Anonymous Monk on Sep 11, 2012 at 15:08 UTC

    I read elsewhere (sorry but I forgot where) that there's one potential technical problem with this algorithm:

    If your rand(n) function isn't strictly uniform, neither will your random line selection, and this problem tends to get worse if the total number of lines increases.

    A naively implemented rand(n) is often non-uniform because your RNG outputs integers between 0 and RMAX, where RMAX is usually a power of 2, or a large prime. The quick-n-dirty way to turn this into a random number between 0 and n, is to calculate its remainder modulo n. This is only truly uniform if n exactly divides RMAX, otherwise it will be almost uniform (given that RMAX is big), which is good enough for many purposes, but not if you're doing many calls and the correctness of your algorithm depends on the uniformity of your rand(n). The proper way to do it, btw, is to take the modulo in most cases, but re-roll if the RNG output is larger than n * floor(RMAX / n).

    I am not sure how the rand(n) function in Perl works internally, hopefully it's been corrected over the years, but for other languages it's something to be on the lookout for. The only one I'm certain of is that Python's got the correct algo, because I remember reading a bug report about their random range function.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://967701]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (20)
As of 2013-06-19 17:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How many continents have you visited?









    Results (661 votes), past polls