Debian Codegraph @

random

raw ◀ ▶
Source reconstructed at patch state 0 of 5 — not the installed (fully patched) source.
5 Debian patches touch this file
1 #!/usr/bin/perl -w
2
3 # random: 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 range 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 print STDERR "Invalid expression: $expression\n" if ($verbose >= 1);
105 }
106 }
107
108 my $random_array_max = @random_array;
109 my $rand_value = int(rand($random_array_max));
110 my $random_number = $random_array[$rand_value];
111
112 print "$random_number\n";
113
114 exit(0);
115
116 ###############
117 # SUBROUTINES #
118 ###############
119
120 sub help {
121 print <<"EOF";
122 -------------------------------------
123 random : Print out a random number.
124 -------------------------------------
125 Usage:
126
127 random [options] /[expression]/
128
129 Options:
130 -d Debug. For developers only.
131 -h Help: You're looking at it.
132 -V Increase verbosity.
133
134 Expressions:
135
136 /1..100/ Random number from 1 to 100.
137 /1..50,60..75/ Random number from multiple ranges.
138 /0..100i2/ Random even number from 0 to 100.
139 /1..100i2/ Odd number from 1 to 100.
140 /0..100i3/ Random factor of 3 from 0 to 100.
141 /1.1..4.5i0.1/ Decimal range.
142
143 EOF
144 }
145
146 sub process_filehandle {
147 my $filehandle = shift;
148 my $number_array_ref = shift;
149
150 while (<$filehandle>) {
151 if (m/^\s*(\-?[0-9]*\.?[0-9]+)/) {
152 print STDERR "found number: $1\n" if ($verbose >= 3);
153 push(@$number_array_ref, $1);
154 }
155 }
156 return 1;
157 }
158
159
160 # Lay down some of that perl pod action.
161 =pod
162
163 =head1 NAME
164
165 random - Print out a random number.
166
167 =head1 SYNOPSIS
168
169 B<random> [-dhV] /[expression]/
170
171 =head1 DESCRIPTION
172
173 B<random> is a program that is part of the numeric utilities package. B<random>
174 will print out a random number determined by the expression that you give. The
175 syntax and program is nearly identical to the B<range> program, except that B<random>
176 picks a number at random from the range expression. If no expression is specified,
177 B<random> will print out a random integer between 1 and 100.
178
179 =head1 OPTIONS
180
181 -h Help: You're looking at it.
182 -V Increase verbosity.
183 -d Debug mode. For developers
184
185 =head1 EXAMPLES
186
187 Random number from 1 to 10.
188 $ random /1..10/
189 7
190
191 From 1 to 10 or from 15 to 20.
192 $ random /1..10,15..20/
193 16
194
195 An even number from 0 to 10
196 $ random /0..10i2/
197 4
198
199 An odd number. Notice the starting number in the range.
200 $ random /1..10i2/
201 9
202
203 A factor of 3 between 99 and 120.
204 $ random /99..120i3/
205 111
206
207 A decimal number.
208 $ random /1.1..2.5i0.1/
209 1.8
210
211 A negative random number.
212 $ random /0.0..-2.0i0.3/
213 -0.8
214
215 =head1 BUGS
216
217 B<random> is slow when dealing with large ranges to randomly
218 find a number from. This is because it creates a list of all
219 potential numbers before picking one. So it can be memory intensive
220 for large ranges.
221
222 =head1 SEE ALSO
223
224 average(1), bound(1), interval(1), normalize(1), numgrep(1), numprocess(1), numsum(1), range(1), round(1)
225
226 =head1 COPYRIGHT
227
228 random is part of the num-utils package, which is copyrighted by
229 Suso Banderas and released under the GPL license. Please read
230 the COPYING and LICENSE files that came with the num-utils package
231
232 Developers can read the GOALS file and contact me about providing
233 submitions or help for the project.
234
235 =head1 MORE INFO
236
237 More info on random can be found at:
238
239 =over 1
240
241 =item B<http://suso.suso.org/programs/num-utils/>
242
243 =back
244
245 =cut
246