This repository has been archived by the owner on May 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
chance.lua
1803 lines (1622 loc) · 51 KB
/
chance.lua
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[--
Chance: A Library for Generating Random Data
@module chance
@author Eric James Michael Ritz
@copyright 2015 Plutono Inc
@license GNU General Public License
@release <a href="https://github.com/ejmr/chance.lua">Project Home</a>
--]]--
--- The table representing the entire module.
--
-- @local
local chance = {}
--- Core
--
-- These functions provide a random number generator atop which the
-- rest of the library is built, and metadata about the library.
--
-- @section Core
chance.core = {}
--- The library version number.
--
-- This table contains four keys: MAJOR, MINOR, PATCH, and LABEL. The
-- first three are numbers, and the last is a potentially empty
-- string. Calling <code>tostring()</code> on the table will produce
-- an easy-to-read version number such as "1.0.2-rc1", "2.0.0", etc.
--
-- The version number follows <a href="http://semver.org">Semantic
-- Versioning</a>.
--
-- @field VERSION
chance.VERSION = setmetatable(
{
["MAJOR"] = 0,
["MINOR"] = 5,
["PATCH"] = 0,
["LABEL"] = "-pre-release",
},
{
__tostring = function ()
return string.format(
"%i.%i.%i%s",
chance.VERSION["MAJOR"],
chance.VERSION["MINOR"],
chance.VERSION["PATCH"],
chance.VERSION["LABEL"])
end
})
--- Make a shallow copy of a table.
--
-- @local
-- @param array
-- @treturn table A copy of <code>array</code>
local function makeShallowCopy(array)
local copy = {}
for _,value in ipairs(array) do
table.insert(copy, value)
end
return copy
end
--- Creates a string by calling a generator repeatedly.
--
-- @local
-- @tparam func generator
-- @tparam int|{int,int} count
-- @tparam[opt] string separator
-- @treturn string
local function makeStringFrom(generator, count, separator)
local amount = count
if type(count) == "table" then
amount = chance.core.random(count[1], count[2])
end
return table.concat(chance.misc.n(generator, amount), separator or "")
end
--- Seeds the random number generator.
--
-- This function accepts one parameter: a seed, which it uses to seed
-- the random number generator. The seed must be a number, and
-- providing the same seed must result in @{chance.core.random}
-- producing the same sequence of results. Beyond that there are no
-- restrictions on the implementation of how the seed is used or the
-- underlying random number generation algorithm to be used.
--
-- @tparam Number seed
-- @treturn nil
function chance.core.seed(seed)
math.randomseed(seed)
end
--- Returns a random number.
--
-- This is the primary function used throughout the library for
-- generating random numbers. Any algorithm for generating random
-- numbers may be used, so long as the implementation of this function
-- adheres to the following restrictions:
--
-- 1. When called with no arguments the function must return a number
-- in the range of [0, 1).
--
-- 2. When called with one argument, a number 'm', the function must
-- return a number in the range of [1, m].
--
-- 3. When called with two arguments, numbers 'm' and 'n', the
-- function must return a number in the range of [m, n]. If 'n' is
-- less than or equal to 'm' then the function simply returns 'm'.
--
-- Note that this is the same behavior as <code>math.random()</code>
-- from Lua's standard library.
--
-- @see chance.core.seed
-- @usage chance.core.random() == 0.8273
-- @usage chance.core.random(10) == 7
-- @usage chance.core.random(8, 12) == 8
--
-- @param[opt] m
-- @param[opt] n
-- @treturn Number
function chance.core.random(m, n)
if m ~= nil then
if n ~= nil then
if n <= m then
return m
else
return math.random(m, n)
end
else
return math.random(m)
end
end
return math.random()
end
--- Sets of data which some functions choose from.
--
-- Many functions select random data from a predefined source. For
-- example, @{chance.time.month} randomly picks a name from an
-- existing list of names. This table contains all of those types of
-- predefined sets of data. Developers can modify or add new sets of
-- data by using the @{chance.core.set} function.
--
-- The keys for this table must strings, which name the data set.
--
-- The values must either be arrays (which can contain any types of
-- values), or a single function. If the value is a function then the
-- library treats it as a generator for that data set, i.e. the
-- library will invoke that function expecting it to return the
-- appropriate type of random data. The function will receive no
-- arguments.
--
-- @see chance.core.set
-- @see chance.core.fromSet
-- @field dataSets
chance.core.dataSets = {}
--- Define or modify a set of data.
--
-- This function creates a new set of data or replaces an existing
-- one. The key parameter must be a string naming the data set. The
-- data parameter must be either a table of data, which can be of any
-- type, or must be a function. If it is a function then the library
-- treats it as a generator and will invoke that function with no
-- arguments whenever random data is requested from that set.
--
-- @see chance.core.fromSet
-- @see chance.core.dataSets
--
-- @tparam string key
-- @tparam table|function data
-- @treturn nil
function chance.core.set(key, data)
chance.core.dataSets[key] = data
end
--- Add data to an existing data set.
--
-- See the documentation on @{chance.core.set} for details on the
-- <code>key</code> parameter. The <code>data</code> must be a table
-- of values which the function will add to the existing data set.
-- <strong>This does not work for data sets that have generator
-- functions for their values.</strong>
--
-- @see chance.core.set
-- @see chance.core.dataSets
--
-- @tparam string key
-- @tparam table data
-- @treturn nil
function chance.core.appendSet(key, data)
for _,value in ipairs(data) do
table.insert(chance.core.dataSets[key], value)
end
end
--- Select random data from an existing data set.
--
-- See the documentation on @{chance.core.set} for details on the
-- restrictions and semantics of the <code>key</code> parameter.
--
-- @see chance.core.set
-- @see chance.core.dataSets
--
-- @tparam string key
-- @return Random data of potentially any type, or nil if there is no
-- data set for the given <code>key</code>
function chance.core.fromSet(key)
local data = chance.core.dataSets[key]
if data == nil then return nil end
if type(data) == "function" then
return data()
else
return chance.helpers.pick(data)
end
end
--- Basic
--
-- These are functions that generate simple types of data such as
-- booleans and numbers.
--
-- @section Basic
chance.basic = {}
--- Returns a random boolean.
--
-- If given no arguments the function has a 50/50 chance of returning
-- true or false. However, an optional table argument can specify the
-- probability of returning true, expressing the probability as a
-- percentage by using an integer in the range [1, 100].
--
-- @usage fifty_fifty = chance.basic.bool()
-- @usage ten_percent_true = chance.basic.bool { probability = 10 }
--
-- @param[opt] flags
-- @treturn true|false
function chance.basic.bool(flags)
local result = chance.core.random(100)
if flags then
return result <= flags["probability"]
else
return result <= 50
end
end
--- Returns a random floating-point number.
--
-- The number will be in the range of zero (inclusive) to one
-- (exclusive), like random number generating functions in many
-- programming languages.
--
-- @treturn number
function chance.basic.float()
return chance.core.random()
end
--- Returns a random integer.
--
-- By default the function returns an integer between smallest and the
-- largest integers Lua allows on the given platform. An optional
-- table can provide inclusive "min" and "max" limits, which have the
-- default values -2^16 and 2^16, respectively.
--
-- @usage x = chance.basic.integer()
-- @usage y = chance.basic.integer { max = 50 }
-- @usage z = chance.basic.integer { min = 1, max = 20 }
--
-- @param[opt] flags
-- @treturn int
function chance.basic.integer(flags)
local min, max = -2^16, 2^16
if flags then
if flags["min"] then min = flags["min"] end
if flags["max"] then max = flags["max"] end
end
return chance.core.random(min, max)
end
--- Returns a random natural number.
--
-- By default the function returns a number between zero and positive
-- inifinity. But it accepts an optional table of flags which can
-- define inclusive "min" and "max" ranges for the result. Minimum
-- values less than zero are rounded up to zero.
--
-- @see chance.basic.integer
--
-- @param[opt] flags
-- @treturn int
function chance.basic.natural(flags)
if flags then
if (flags["min"] == nil) or (flags["min"] and flags["min"] < 0) then
flags["min"] = 0
end
return chance.basic.integer(flags)
end
return chance.basic.integer { min = 0 }
end
-- These groups are preset "pools" for chance.basic.character().
local character_groups = {}
character_groups["lower"] = "abcdefghijklmnopqrstuvwxyz"
character_groups["upper"] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
character_groups["digit"] = "0123456789"
character_groups["hex"] = "0123456789abcdef"
character_groups["letter"] = character_groups["lower"] .. character_groups["upper"]
character_groups["all"] = character_groups["letter"] .. character_groups["digit"]
--- Returns a random character.
--
-- This functions returns a random character which will be either a
-- digit or a letter, lower-case and upper-case.
--
-- The function accepts a table of optional flags. The "pool" flag
-- can be a string from which the function will select a character.
-- Or one can use the "group" flag to receive a random character from
-- a specific group of characters. The acceptable group names are
-- "lower", "upper", "letter" (case-insensitive), "digit", and "all"
-- (the default). If the function receives both "pool" and "group"
-- then "pool" takes precedence and "group" is ignored.
--
-- @usage anything = chance.basic.character()
-- @usage anything = chance.basic.character { group = "all" }
-- @usage vowel = chance.basic.character { pool = "aeiou" }
-- @usage capital = chance.basic.character { group = "upper" }
--
-- @param[opt] flags
-- @treturn string
function chance.basic.character(flags)
local pool = character_groups["all"]
if flags then
if flags["pool"] then
pool = flags["pool"]
elseif flags["group"] then
pool = character_groups[flags["group"]]
end
end
local index = chance.core.random(pool:len())
return pool:sub(index, index)
end
--- Returns a random string.
--
-- This function will return a string of random characters, with a
-- random length of five to twenty characters. The optional flags
-- table can set "length" explicitly. It also accepts a "group" flag
-- which determines what kind of characters appear in the string.
--
-- @see chance.basic.character
--
-- @usage chance.basic.string() == "c0Ab3le8"
-- @usage chance.basic.string { length = 3 } == "NIN"
-- @usage chance.basic.string { group = "digit" } = "8374933749"
--
-- @param[opt] flags
-- @treturn string
function chance.basic.string(flags)
local length = chance.core.random(5, 20)
local group = "all"
local result = ""
if flags then
if flags["length"] then
length = flags["length"]
end
if flags["group"] then
group = flags["group"]
end
end
local count = 1
while count <= length do
result = result .. chance.basic.character { group = group }
count = count + 1
end
return result
end
--- Text
--
-- These are functions for generating random text.
--
-- @section Text
chance.text = {}
--- Data used to build random syllables.
--
-- @see chance.text.syllable
-- @local
-- @field syllables
-- @table chance.core.dataSets
chance.core.set("syllables", {
["consonants"] = {
"b",
"c",
"ch",
"d",
"f",
"g",
"gh",
"h",
"j",
"k",
"l",
"m",
"n",
"p",
"qu",
"r",
"s",
"sh",
"t",
"th",
"y",
"w",
"z",
},
["vowels"] = {
"a",
"e",
"i",
"o",
"u",
"ea",
"ee",
"ao",
"oo",
"ou",
}})
--- Returns a random syllable.
--
-- This functions returns a randomly generated syllable that will be
-- between two to six characters in length. It uses the
-- <code>syllables</code> data set, which contains a collection of
-- consonants and vowels used to create the syllable. Each syllable
-- will contain between two to six characters.
--
-- @usage chance.text.syllable() == "peep"
-- @see chance.text.word
--
-- @treturn string
function chance.text.syllable()
local initial = chance.helpers.pick(chance.core.dataSets["syllables"]["consonants"])
local vowel = chance.helpers.pick(chance.core.dataSets["syllables"]["vowels"])
local ending = chance.helpers.pick(chance.core.dataSets["syllables"]["consonants"])
local syllable = initial .. vowel
-- Fifty percent of the time we add an additional consonant sound
-- to the end of the syllable.
if chance.basic.bool() == true then
syllable = syllable .. ending
end
return syllable
end
--- Returns a random word.
--
-- The word, by default, will contain one to three syllables.
-- However, the optional flag <code>syllables</code> can specify
-- exactly how many syllables to use in the word. Note that
-- "syllable" in this context means anything which
-- @{chance.text.syllable} will return.
--
-- @usage chance.text.word() == "beepbop"
-- @usage chance.text.word { syllables = 4 } == "thadoobgerlu"
-- @see chance.text.syllable
--
-- @param[opt] flags
-- @treturn string
function chance.text.word(flags)
local syllableCount = chance.core.random(1, 3)
local word = ""
if flags and flags["syllables"] then
syllableCount = flags["syllables"]
end
if syllableCount < 1 then return word end
while syllableCount > 0 do
syllableCount = syllableCount - 1
word = word .. chance.text.syllable()
end
return word
end
--- Generates a random sentence of words via @{chance.text.word}.
--
-- This function returns a sentence of random words, between twelve to
-- eighteen words by default. The optional <code>words</code> flag
-- allows controling exactly how many words appear in the sentence.
-- The first word in the sentence will be capitalized and the sentence
-- will end with a period.
--
-- @usage chance.text.sentence { words = 3 } == "Hob the rag."
-- @see chance.text.word
--
-- @param[opt] flags
-- @treturn string
function chance.text.sentence(flags)
local words
local wordCount = chance.core.random(12, 18)
if flags and flags["words"] then
wordCount = flags["words"]
end
words = chance.misc.n(chance.text.word, wordCount)
words[1] = string.gsub(words[1], "^%l", string.upper)
table.insert(words, ".")
return table.concat(words, " ")
end
--- Generates a random paragraph via @{chance.text.sentence}.
--
-- This function returns a paragraph of random sentences, created by
-- calling @{chance.text.sentence}. By default the paragraph will
-- contain three to seven sentences. However, the optional integer
-- flag <code>sentences</code> controls exactly how many sentences to
-- create for the paragraph.
--
-- @see chance.text.sentence
--
-- @param[opt] flags
-- @treturn string
function chance.text.paragraph(flags)
local count = chance.core.random(3, 7)
if flags and flags["sentences"] then
count = flags["sentences"]
end
return makeStringFrom(chance.text.sentence, count)
end
--- Person
--
-- These are functions for generating random data about people.
--
-- @section Person
chance.person = {}
--- Generates a random United States Social Security Number.
--
-- This function generates a random United States Social Security
-- Number and returns it as a string, <code>AAA-GG-SSSS</code>, where
-- the digits represent the Area, Group, and Serial numbers,
-- respectively. The function will not return all zeros for any part
-- of the number, nor will the Area ever be '666' or '900-999', per
-- the standards on Social Security Numbers.
--
-- @usage chance.person.ssn() == "343-74-0571"
--
-- @treturn string
function chance.person.ssn()
local area, group, serial
while true do
area = chance.basic.string { length = 3, group = "digit" }
if not area:match("000") and not area:match("9%d%d") then
break
end
end
while true do
group = chance.basic.string { length = 2, group = "digit" }
if not group:match("00") then
break
end
end
while true do
serial = chance.basic.string { length = 4, group = "digit" }
if not serial:match("0000") then
break
end
end
return string.format("%s-%s-%s", area, group, serial)
end
--- The possible genders returned by @{chance.person.gender}.
--
-- This is a table of strings which the @{chance.person.gender}
-- function will randomly choose from when called. Developers can
-- modify the domain of @{chance.person.gender} by changing this table
-- to include or remove possible values as needed for their purposes.
-- The default values are based on common gender identities in modern
-- socities as opposed to gender based on medical qualification
-- (e.g. chromosones) or sexual orientation.
--
-- @see chance.person.gender
-- @local
-- @field genders
-- @table chance.core.dataSets
chance.core.set("genders", {
"Male",
"Female",
"Third", -- https://en.m.wikipedia.org/wiki/Third_gender
})
--- Returns a random gender as a string.
--
-- One can classify gender in a number of ways. The most traditional
-- form is the binary division of 'male' and 'female'; if the function
-- is given the optional flag <code>binary = true</code> then it will
-- return either <code>"Male"</code> or <code>"Female"</code>.
--
-- By default, however, the function will return a string from the
-- <code>genders</code> data set.
--
-- @usage chance.person.gender() == "Female"
-- @usage chance.person.gender { binary = true } == "Male"
--
-- @see chance.core.dataSets
--
-- @param[opt] flags
-- @treturn string
function chance.person.gender(flags)
if flags and flags["binary"] == true then
return chance.helpers.pick { "Male", "Female" }
end
return chance.core.fromSet("genders")
end
--- Possible words returned by @{chance.person.prefix}
--
-- @see chance.person.prefix
-- @local
-- @field prefixes
-- @table chance.core.dataSets
chance.core.set("prefixes", {
["short"] = {
"Mr.",
"Ms.",
"Mrs.",
"Doc.",
"Prof.",
"Rev.",
"Hon.",
},
["long"] = {
"Mister",
"Miss",
"Doctor",
"Professor",
"Reverend",
"Honorable",
}})
--- Returns a random prefix for a name.
--
-- This function will return a random prefix for a name, e.g. "Mr."
-- or "Prof.", short prefixes by default. The function accepts an
-- optional table of flags, and if the flag <code>type</code> equals
-- <code>"long"</code> then the function returns prefixes such as
-- "Mister" and "Professor". The function uses the
-- <code>prefixes</code> data set.
--
-- @usage chance.person.prefix() == "Mrs."
-- @usage chance.person.prefix { type = "long" } == "Doctor"
--
-- @param[opt] flags
-- @treturn string
function chance.person.prefix(flags)
local prefixType = "short"
if flags and flags["type"] then
prefixType = string.lower(flags["type"])
end
return chance.helpers.pick(chance.core.dataSets["prefixes"][prefixType])
end
--- Possible words returned by @{chance.person.suffix}
--
-- @see chance.person.suffix
-- @local
-- @field suffixes
-- @table chance.core.dataSets
chance.core.set("suffixes", {
["short"] = {
"Ph.D.",
"Esq.",
"Jr.",
"Sr.",
"M.D.",
"J.D.",
},
["long"] = {
"Doctor of Philosophy",
"Esquire",
"Junior",
"Senior",
"Medical Doctor",
"Juris Doctor",
},
}
)
--- Returns a random suffix for names.
--
-- This function will return a random suffix for a name, e.g. "Jr."
-- or "M.D.", short prefixes by default. The function accepts an
-- optional table of flags, and if the flag <code>type</code> equals
-- <code>"long"</code> then the function returns prefixes such as
-- "Junior" and "Juris Doctor". The function uses the
-- <code>suffixes</code> data set.
--
-- @usage chance.person.suffix() == "Sr."
-- @usage chance.person.suffix { type = "long" } == "Senior"
--
-- @param[opt] flags
-- @treturn string
function chance.person.suffix(flags)
local suffixType = "short"
if flags and flags["type"] then
suffixType = string.lower(flags["type"])
end
return chance.helpers.pick(chance.core.dataSets["suffixes"][suffixType])
end
--- Ranges for various types of ages.
--
-- @see chance.person.age
-- @local
-- @field ages
-- @table chance.core.dataSets
chance.core.set("ages", {
["child"] = {1, 12},
["teen"] = {13, 19},
["adult"] = {18, 65},
["senior"] = {65, 100},
})
--- Returns a random age for a person.
--
-- By default this function return an integer in the range of one and
-- one-hundred twenty. It accepts an optional <code>type</code> flag
-- which must be one of the following strings, which limit the range
-- of the generated age:
--
-- <ol>
-- <li><code>"child" = [1, 12]</code></li>
-- <li><code>"teen" = [13, 19]</code></li>
-- <li><code>"adult" = [18, 65]</code></li>
-- <li><code>"senior" = [65, 100]</code></li>
-- </ol>
--
-- These ranges are defined in the <code>ages</code> data set, meaning
-- one can use @{chance.core.set} and @{chance.core.appendSet} to
-- redefine the ranges for types and/or add new types.
--
-- @usage chance.person.age() == 33
-- @usage chance.person.age { type = "teen" } == 17
-- @usage chance.person.age { type = "adult" } == 40
--
-- @param[opt] flags
-- @treturn int
function chance.person.age(flags)
if flags and flags["type"] then
local group = chance.core.dataSets["ages"][flags["type"]]
return chance.core.random(group[1], group[2])
end
return chance.core.random(1, 120)
end
--- Time
--
-- These are functions for generating random times.
--
-- @section Time
chance.time = {}
--- Returns a random hour.
--
-- By default this will return an hour in the range of one to twelve.
-- However, if the optional flag <code>twentyfour</code> is true then
-- the result will be in the range of one to twenty-four.
--
-- @usage chance.time.hour() == 3
-- @usage chance.time.hour { twentyfour = true } == 15
--
-- @param[opt] flags
-- @treturn number
function chance.time.hour(flags)
if flags and flags["twentyfour"] == true then
return chance.core.random(1, 24)
else
return chance.core.random(1, 12)
end
end
--- Returns a random minute.
--
-- This will return a number in the range of zero to fifty-nine.
--
-- @treturn number
function chance.time.minute()
return chance.core.random(0, 59)
end
--- Returns a random second.
--
-- This will return a number in the range of zero to fifty-nine.
--
-- @treturn number
function chance.time.second()
return chance.core.random(0, 59)
end
--- Returns a random millisecond.
--
-- This returns a number in the range of zero to nine-hundred ninety
-- nine.
--
-- @treturn number
function chance.time.millisecond()
return chance.core.random(0, 999)
end
--- Returns a random year.
--
-- By default this function returns a number representing a year in
-- the range of the current year and a century later. For example,
-- calling <code>chance.time.year()</code> in the year 2015 will
-- return a number between 2015 and 2115.
--
-- The function accepts an optional table of flags which can have
-- <code>min</code> and <code>max</code> properties to restrict the
-- range of the output. If only <code>min</code> is provided then the
-- maximum range is one century ahead of the minimum, for example
-- <code>chance.time.year { min = 1750 }</code> returns a year between
-- 1750 and 1850. If only <code>max</code> is provided then the
-- minimum is the current year.
--
-- @usage chance.time.year() == 2074
-- @usage chance.time.year { min = 1800 } == 1884
-- @usage chance.time.year { max = 2300 } == 2203
-- @usage chance.time.year { min = 1990, max = 2000 } == 1995
--
-- @param[opt] flags
-- @treturn number
function chance.time.year(flags)
local current_year = os.date("*t")["year"]
local minimum = current_year
local maximum = current_year + 100
if flags then
if flags["min"] and flags["max"] then
minimum = flags["min"]
maximum = flags["max"]
elseif flags["min"] then
minimum = flags["min"]
maximum = minimum + 100
elseif flags["max"] then
maximum = flags["max"]
end
end
return chance.core.random(minimum, maximum)
end
--- Names of months.
--
-- @see chance.time.month
-- @local
-- @field months
-- @table chance.core.dataSets
chance.core.set("months", {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
})
--- Returns the name of a random month.
--
-- This function chooses the name of a month from the
-- <code>months</code> data set.
--
-- @treturn string
function chance.time.month()
return chance.core.fromSet("months")
end
--- Names of days of the week.
--
-- @see chance.time.day
-- @local
-- @field days
-- @table chance.core.dataSets
chance.core.set("days", {
["weekdays"] = {
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
},
["weekends"] = {
"Saturday",
"Sunday",
}})
--- Returns a random day of the week.
--
-- By default this function will return the name of a day of the week,
-- chosen from the <code>days</code> data set. The function accepts
-- an optional table of flags which control the possible days it
-- returns. The optional boolean flags <code>weekdays</code> and
-- <code>weekends</code> will restrict the output to those types of
-- days.
--
-- @usage chance.time.day() == "Monday"
-- @usage chance.time.day { weekends = true } == "Sunday"
-- @usage chance.time.day { weekends = false } == "Thursday"
--
-- @param[opt] flags
-- @treturn string
function chance.time.day(flags)
local category = "all"
local days = {}
if flags then
if flags["weekdays"] == false
or flags["weekends"] == true then
category = "weekends"
elseif flags["weekends"] == false
or flags["weekdays"] == true then
category = "weekdays"
end
end
if category == "weekdays" or category == "weekends" then
days = makeShallowCopy(chance.core.dataSets["days"][category])
elseif category == "all" then
for _,set in pairs(chance.core.dataSets["days"]) do
for _,day in ipairs(set) do
table.insert(days, day)
end
end
end
return chance.helpers.pick(days)
end
--- Returns a random Unix timestamp.
--
-- This function returns a random number between zero and the current
-- time as a Unix timestamp, i.e. the number of seconds since January
-- 1st 1970.
--
-- <strong>This function may not correctly determine the current time
-- on non-POSIX systems.</strong>
--
-- @treturn number
function chance.time.timestamp()
return chance.core.random(0, os.time())
end
--- Returns 'am' or 'pm' for use with times.
--
-- @treturn string <code>"am"</code> or <code>"pm"</code>