Debian Codegraph @

random

raw ◀ ▶
Source reconstructed at patch state 2 of 5 — not the installed (fully patched) source.
5 Debian patches touch this file
1 #!/usr/bin/perl -w
2
3 # numrandom: Print out a random number.
4 #
5 # Copyright (C) 2002-2004 Suso Banderas
6
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation; either version 2
10 # of the License, or (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #
21 # You may contact the author at <[email protected]>.
22
23 #######################
24 # VARIABLES AND SETUP #
25 #######################
26
27 use Getopt::Std;
28 use strict;
29 use vars qw/ %opts $verbose /;
30
31 my ($expression, @random_array, $number);
32
33 getopts('hdV', \%opts);
34
35 if ($opts{'h'}) {
36 &help();
37 exit(0);
38 }
39
40 if ($opts{'d'}) {
41 $verbose = 3;
42 print STDERR "Debug mode\n";
43 } elsif ($opts{'V'}) {
44 $verbose = 2;
45 print STDERR "Verbose mode\n";
46 } elsif ($opts{'q'}) {
47 $verbose = 0; # Nothing except the final answer
48 } else {
49 $verbose = 1; # Normal warnings and such.
50 }
51
52 my $range_expression = shift || "/1..100/"; # Give a default expression
53 # if none are specified.
54 $range_expression =~ s/^\///;
55 $range_expression =~ s/\/$//;
56 my @range_expressions = split(/,/, $range_expression);
57
58
59 ################
60 # MAIN PROGRAM #
61 ################
62
63 # This part is almost exactly the same as the numrange program, except
64 # for the last part of course.
65
66 my $number_exp = "-?[0-9]*\\.?[0-9]+";
67
68 foreach $expression (@range_expressions) {
69 print "expression: $expression\n" if ($verbose >= 3);
70 if ($expression =~ /^($number_exp)$/) {
71 push @random_array, $expression;
72 } elsif ($expression =~ /^($number_exp)\.\.($number_exp)$/) {
73 print "1: $1\n2: $2\n" if ($verbose >= 3);
74 if ($1 > $2) {
75 my $number = $1;
76 while ($number >= $2) {
77 push (@random_array, $number);
78 $number--;
79 }
80 } else {
81 my $number;
82 foreach $number ($1..$2) {
83 push (@random_array, $number);
84 }
85 }
86 } elsif ($expression =~ /^($number_exp)\.\.($number_exp)i($number_exp)$/) {
87 print "1: $1\n2: $2\n3: $3\n" if ($verbose >= 3);
88 if ($1 > $2) {
89 my $number = $1;
90 while ($number >= $2) {
91 $number = eval($number); # This fixes a stupid computer math problem.
92 push (@random_array, $number);
93 $number = $number - $3;
94 }
95 } else {
96 my $number = $1;
97 while ($number <= $2) {
98 $number = eval($number); # This fixes a stupid computer math problem.
99 push (@random_array, $number);
100 $number = $number + $3;
101 }
102 }
103 } else {
104 die "Invalid expression: $expression\n";
105 print STDERR "Invalid expression: $expression\n" if ($verbose >= 1);
106 }
107 }
108
109 my $random_array_max = @random_array;
110 my $rand_value = int(rand($random_array_max));
111 my $random_number = $random_array[$rand_value];
112
113 print "$random_number\n";
114
115 exit(0);
116
117 ###############
118 # SUBROUTINES #
119 ###############
120
121 sub help {
122 print <<"EOF";
123 -------------------------------------
124 numrandom : Print out a random number.
125 -------------------------------------
126 Usage:
127
128 numrandom [options] /[expression]/
129
130 Options:
131 -d Debug. For developers only.
132 -h Help: You're looking at it.
133 -V Increase verbosity.
134
135 Expressions:
136
137 /1..100/ Random number from 1 to 100.
138 /1..50,60..75/ Random number from multiple ranges.
139 /0..100i2/ Random even number from 0 to 100.
140 /1..100i2/ Odd number from 1 to 100.
141 /0..100i3/ Random factor of 3 from 0 to 100.
142 /1.1..4.5i0.1/ Decimal range.
143
144 EOF
145 }
146
147 sub process_filehandle {
148 my $filehandle = shift;
149 my $number_array_ref = shift;
150
151 while (<$filehandle>) {
152 if (m/^\s*(\-?[0-9]*\.?[0-9]+)/) {
153 print STDERR "found number: $1\n" if ($verbose >= 3);
154 push(@$number_array_ref, $1);
155 }
156 }
157 return 1;
158 }
159
160
161 # Lay down some of that perl pod action.
162 =pod
163
164 =head1 NAME
165
166 numrandom - Print out a random number.
167
168 =head1 SYNOPSIS
169
170 B<numrandom> [-dhV] /[expression]/
171
172 =head1 DESCRIPTION
173
174 B<numrandom>
175 will print out a random number determined by the expression that you give. The
176 syntax and program is nearly identical to the B<numrange> program, except that B<numrandom>
177 picks a number at random from the range expression. If no expression is specified,
178 B<numrandom> will print out a random integer between 1 and 100.
179
180 =head1 OPTIONS
181
182 -h Help: You're looking at it.
183 -V Increase verbosity.
184 -d Debug mode. For developers
185
186 =head1 EXAMPLES
187
188 Random number from 1 to 10.
189 $ numrandom /1..10/
190 7
191
192 From 1 to 10 or from 15 to 20.
193 $ numrandom /1..10,15..20/
194 16
195
196 An even number from 0 to 10
197 $ numrandom /0..10i2/
198 4
199
200 An odd number. Notice the starting number in the range.
201 $ numrandom /1..10i2/
202 9
203
204 A factor of 3 between 99 and 120.
205 $ numrandom /99..120i3/
206 111
207
208 A decimal number.
209 $ numrandom /1.1..2.5i0.1/
210 1.8
211
212 A negative random number.
213 $ numrandom /0.0..-2.0i0.3/
214 -0.8
215
216 =head1 BUGS
217
218 B<numrandom> is slow when dealing with large ranges to randomly
219 find a number from. This is because it creates a list of all
220 potential numbers before picking one. So it can be memory intensive
221 for large ranges.
222
223 =head1 SEE ALSO
224
225 numaverage(1), numbound(1), numinterval(1), numnormalize(1), numgrep(1), numprocess(1), numsum(1), numrange(1), numround(1)
226
227 =head1 COPYRIGHT
228
229 numrandom is part of the num-utils package, which is copyrighted by
230 Suso Banderas and released under the GPL license. Please read
231 the COPYING and LICENSE files that came with the num-utils package
232
233 Developers can read the GOALS file and contact me about providing
234 submitions or help for the project.
235
236 =head1 MORE INFO
237
238 More info on numrandom can be found at:
239
240 =over 1
241
242 =item B<http://suso.suso.org/programs/num-utils/>
243
244 =back
245
246 =cut
247