summaryrefslogtreecommitdiff
path: root/lib/rand.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rand.pm')
-rw-r--r--lib/rand.pm20
1 files changed, 20 insertions, 0 deletions
diff --git a/lib/rand.pm b/lib/rand.pm
new file mode 100644
index 0000000..bf42e7d
--- /dev/null
+++ b/lib/rand.pm
@@ -0,0 +1,20 @@
+#!/usr/bin/perl
+
+sub get_random_line
+ {
+ my ($file) = @_;
+ my $line;
+
+ # inscrutable random line algorithm -- from the camel book
+ # $. is the number of the last line accessed
+ open FORTUNES, "$BUCKY_FORTUNES/$file" or error("Could not access fortune $file");
+ srand;
+ rand($.) < 1 && ($line = $_) while <FORTUNES>;
+ close FORTUNES;
+
+ chomp $line;
+ return $line;
+ }
+
+1;
+