1Data::Random(3) User Contributed Perl Documentation Data::Random(3)
2
3
4
6 Data::Random - Perl module to generate random data
7
9 use Data::Random qw(:all);
10
11 my @random_words = rand_words( size => 10 );
12
13 my @random_chars = rand_chars( set => 'all', min => 5, max => 8 );
14 my $string = rand_chars( set => 'all', min => 5, max => 8 );
15
16 my @random_set = rand_set( set => \@set, size => 5 );
17
18 my $random_enum = rand_enum( set => \@set );
19 my $random_enum = rand_enum( \@set ); # shortcut
20
21 my $random_date = rand_date();
22
23 my $random_time = rand_time();
24
25 my $random_datetime = rand_datetime();
26
27 open(my $file, ">", "rand_image.png") or die $!;
28 binmode($file);
29 print $file rand_image( bgcolor => [0, 0, 0] );
30 close($file);
31
33 A module used to generate random data. Useful mostly for test
34 programs.
35
37 rand_words()
38 This returns a list of random words given a wordlist. See below for
39 possible parameters.
40
41 • wordlist - the path to the wordlist file. A lot of systems have
42 one at /usr/dict/words. You can also optionally supply a
43 Data::Random::WordList object to keep a persistent wordlist. The
44 default is the wordlist distributed with this module.
45
46 • min - the minimum number of words to return. The default is 1.
47
48 • max - the maximum number of words to return. The default is 1.
49
50 • size - the number of words to return. The default is 1. If you
51 supply a value for 'size', then 'min' and 'max' aren't paid
52 attention to.
53
54 • shuffle - whether or not the words should be randomly shuffled.
55 Set this to 0 if you don't want the words shuffled. The default is
56 1. Random::Data::WordList returns words in the order that they're
57 viewed in the word list file, so shuffling will make sure that the
58 results are a little more random.
59
60 rand_chars()
61 When called in a list context this returns a list of random characters
62 given a set of characters. In a scalar context it returns a string of
63 random characters. See below for possible parameters.
64
65 • set - the set of characters to be used. This value can be either a
66 reference to an array of strings, or one of the following:
67
68 alpha - alphabetic characters: a-z, A-Z
69 upperalpha - upper case alphabetic characters: A-Z
70 loweralpha - lower case alphabetic characters: a-z
71 numeric - numeric characters: 0-9
72 alphanumeric - alphanumeric characters: a-z, A-Z, 0-9
73 char - non-alphanumeric characters: # ~ ! @ $ % ^ & * ( ) _ + = - { } | : " < > ? / . ' ; ] [ \ `
74 misc - same as 'char'
75 all - all of the above
76
77 • min - the minimum number of characters to return. The default is
78 0.
79
80 • max - the maximum number of characters to return. The default is
81 the size of the set.
82
83 • size - the number of characters to return. The default is 1. If
84 you supply a value for 'size', then 'min' and 'max' aren't paid
85 attention to.
86
87 • shuffle - whether or not the characters should be randomly
88 shuffled. Set this to 0 if you want the characters to stay in the
89 order received. The default is 1.
90
91 rand_set()
92 This returns a random set of elements given an initial set. See below
93 for possible parameters.
94
95 • set - the set of strings to be used. This should be a reference to
96 an array of strings.
97
98 • min - the minimum number of strings to return. The default is 0.
99
100 • max - the maximum number of strings to return. The default is the
101 size of the set.
102
103 • size - the number of strings to return. The default is 1. If you
104 supply a value for 'size', then 'min' and 'max' aren't paid
105 attention to.
106
107 • shuffle - whether or not the strings should be randomly shuffled.
108 Set this to 0 if you want the strings to stay in the order
109 received. The default is 1.
110
111 rand_enum()
112 This returns a random element given an initial set. See below for
113 possible parameters.
114
115 • set - the set of strings to be used. This should be a reference to
116 an array of strings. The "set" key will be assumed if the array
117 reference is passed as the only argument.
118
119 rand_date()
120 This returns a random date in the form "YYYY-MM-DD". 2-digit years are
121 not currently supported. Efforts are made to make sure you're returned
122 a truly valid date--ie, you'll never be returned the date February
123 31st. See the options below to find out how to control the date range.
124 Here are a few examples:
125
126 # returns a date somewhere in between the current date, and one year from the current date
127 $date = rand_date();
128
129 # returns a date somewhere in between September 21, 1978 and September 21, 1979
130 $date = rand_date( min => '1978-9-21' );
131
132 # returns a date somewhere in between September 21, 1978 and the current date
133 $date = rand_date( min => '1978-9-21', max => 'now' );
134
135 # returns a date somewhere in between the current date and September 21, 2008
136 $date = rand_date( min => 'now', max => '2008-9-21' );
137
138 See below for possible parameters.
139
140 • min - the minimum date to be returned. It should be in the form
141 "YYYY-MM-DD" or you can alternatively use the string "now" to
142 represent the current date. The default is the current date;
143
144 • max - the maximum date to be returned. It should be in the form
145 "YYYY-MM-DD" or you can alternatively use the string "now" to
146 represent the current date. The default is one year from the
147 minimum date;
148
149 rand_time()
150 This returns a random time in the form "HH:MM:SS". 24 hour times are
151 supported. See the options below to find out how to control the time
152 range. Here are a few examples:
153
154 # returns a random 24-hr time (between 00:00:00 and 23:59:59)
155 $time = rand_time();
156
157 # returns a time somewhere in between 04:00:00 and the end of the day
158 $time = rand_time( min => '4:0:0' );
159
160 # returns a time somewhere in between 8:00:00 and the current time (if it's after 8:00)
161 $time = rand_time( min => '12:00:00', max => 'now' );
162
163 # returns a date somewhere in between the current time and the end of the day
164 $time = rand_time( min => 'now' );
165
166 See below for possible parameters.
167
168 • min - the minimum time to be returned. It should be in the form
169 "HH:MM:SS" or you can alternatively use the string "now" to
170 represent the current time. The default is 00:00:00;
171
172 • max - the maximum time to be returned. It should be in the form
173 "HH:MM:SS" or you can alternatively use the string "now" to
174 represent the current time. The default is 23:59:59;
175
176 rand_datetime()
177 This returns a random date and time in the form "YYYY-MM-DD HH:MM:SS".
178 See the options below to find out how to control the date/time range.
179 Here are a few examples:
180
181 # returns a date somewhere in between the current date/time, and one year from the current date/time
182 $datetime = rand_datetime();
183
184 # returns a date somewhere in between 4:00 September 21, 1978 and 4:00 September 21, 1979
185 $datetime = rand_datetime( min => '1978-9-21 4:0:0' );
186
187 # returns a date somewhere in between 4:00 September 21, 1978 and the current date
188 $datetime = rand_datetime( min => '1978-9-21 4:0:0', max => 'now' );
189
190 # returns a date somewhere in between the current date/time and the end of the day September 21, 2008
191 $datetime = rand_datetime( min => 'now', max => '2008-9-21 23:59:59' );
192
193 See below for possible parameters.
194
195 • min - the minimum date/time to be returned. It should be in the
196 form "YYYY-MM-DD HH:MM:SS" or you can alternatively use the string
197 "now" to represent the current date/time. The default is the
198 current date/time;
199
200 • max - the maximum date/time to be returned. It should be in the
201 form "YYYY-MM-DD HH:MM:SS" or you can alternatively use the string
202 "now" to represent the current date/time. The default is one year
203 from the minimum date/time;
204
205 rand_image()
206 This returns a random image. Currently only PNG images are supported.
207 See below for possible parameters.
208
209 • minwidth - the minimum width of the image. The default is 1.
210
211 • maxwidth - the maximum width of the image. The default is 100.
212
213 • width - the width of the image. If you supply a value for 'width',
214 then 'minwidth' and 'maxwidth' aren't paid attention to.
215
216 • minheight - the minimum height of the image. The default is 1.
217
218 • maxheight - the maximum height of the image. The default is 100.
219
220 • height - the height of the image. If you supply a value for
221 'width', then 'minwidth' and 'maxwidth' aren't paid attention to.
222
223 • minpixels - the minimum number of random pixels to display on the
224 image. The default is 0.
225
226 • maxpixels - the maximum number of random pixels to display on the
227 image. The default is width * height.
228
229 • pixels - the number of random pixels to display on the image. If
230 you supply a value for 'pixels', then 'minpixels' and 'maxpixels'
231 aren't paid attention to.
232
233 • bgcolor - the background color of the image. The value must be a
234 reference to an RGB array where each element is an integer between
235 0 and 255 (eg. [ 55, 120, 255 ]).
236
237 • fgcolor - the foreground color of the image. The value must be a
238 reference to an RGB array where each element is an integer between
239 0 and 255 (eg. [ 55, 120, 255 ]).
240
242 0.12
243
245 Originally written by: Adekunle Olonoh
246
247 Currently maintained by: Buddy Burden (barefoot@cpan.org), starting
248 with version 0.06
249
251 Hiroki Chalfant
252 David Sarno
253 Michiel Beijen
254
256 Copyright (c) 2000-2011 Adekunle Olonoh. Copyright (c) 2011-2015 Buddy
257 Burden. All rights reserved. This program is free software; you can
258 redistribute it and/or modify it under the same terms as Perl itself.
259
261 Data::Random::WordList
262
263
264
265perl v5.36.0 2022-07-22 Data::Random(3)