1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
#! /p/bin/perl -w
# Print n pairs of floating point values.
# Each value is in the range [0,1).
# Usage: rand n
# Copyright (C) 1997 Free Software Foundation, Inc.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
# Use only the basename from the path to this executable in error messages.
($program_name = $0) =~ s|.*/||;
require 'newgetopt.pl';
$MAXINT = 0x7fffffff;
undef $opt_help;
undef $opt_verbose;
undef $opt_integer;
&usage if (&NGetOpt(('seed=i', 'range=s', 'help', 'items-per-line=i',
'format=s', 'integer', 'verbose')) == 0);
&usage if (defined ($opt_help));
&usage if (scalar (@ARGV) != 1 || $ARGV[0] !~ /^[0-9]+$/);
$n = $ARGV[0];
if (!defined ($opt_seed))
{
$opt_seed = time;
print STDERR "seed= $opt_seed\n" if (defined ($opt_verbose));
}
srand ($opt_seed);
# FIXME: make sure this number is positive.
$opt_items_per_line = 1 if (!defined ($opt_items_per_line));
if (defined ($opt_integer))
{
$opt_format = "%d" if (!defined ($opt_format));
if (defined ($opt_range))
{
# FIXME: allow FP endpoints even though --integer specified?
if ($opt_range =~ /^([0-9]+),([0-9]+)$/)
{
$lo = $1;
$hi = $2;
}
else
{
print STDERR ("bad argument `$opt_range' to --range option\n");
exit 2;
}
}
else
{
$lo = 0;
$hi = $MAXINT;
}
# Increase by one because we'll have to truncate to get integers.
$hi += 1;
}
else
{
$opt_format = "%.15g" if (!defined ($opt_format));
if (defined ($opt_range))
{
if ($opt_range =~ /^(-?[0-9.]+),(-?[0-9.]+)$/)
{
$lo = $1;
$hi = $2;
}
else
{
print STDERR ("bad argument `$opt_range' to --range option\n");
exit 2;
}
}
else
{
$lo = 0.0;
$hi = 1.0;
}
}
# FIXME: warn if lo > hi.
$range = $hi - $lo;
if ($opt_items_per_line == 1)
{
for ($i=0; $i < $n; $i++)
{
printf $opt_format, $lo + rand($range);
print "\n";
}
}
else
{
$separator = ' ';
for ($i=0; $i < $n; $i++)
{
for ($j=0; $j < $opt_items_per_line; $j++)
{
printf $opt_format, $lo + rand($range);
print $j == $opt_items_per_line - 1 ? "\n" : $separator;
}
}
}
exit 0;
sub usage
{
print STDERR <<EOF;
Usage: $program_name [OPTIONS] n
--help
--seed=n
--integer
--items-per-line=n
--range=i,j e.g. 3,9 or 1.5,33.9
--format='%3d'
--verbose (report the seed if it gets default value)
EOF
exit 2;
}
|