Debian Codegraph @

random

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