urandelement DSL function (#789)

This commit is contained in:
John Kerl 2021-12-21 23:15:14 -05:00 committed by GitHub
parent 99e4a23ac6
commit 1d3bc11b29
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 60 additions and 18 deletions

View file

@ -208,9 +208,9 @@ FUNCTION LIST
sha512 sin sinh sort splita splitax splitkv splitkvx splitnv splitnvx sqrt
ssub strftime strftime_local string strip strlen strptime strptime_local sub
substr substr0 substr1 system systime systimeint tan tanh tolower toupper
truncate typeof unflatten uptime urand urand32 urandint urandrange version !
!= !=~ % & && * ** + - . .* .+ .- ./ / // < << <= <=> == =~ > >= >> >>> ?: ??
??? ^ ^^ | || ~
truncate typeof unflatten uptime urand urand32 urandelement urandint
urandrange version ! != !=~ % & && * ** + - . .* .+ .- ./ / // < << <= <=> ==
=~ > >= >> >>> ?: ?? ??? ^ ^^ | || ~
COMMENTS-IN-DATA FLAGS
Miller lets you put comments in your data, such as
@ -2508,6 +2508,9 @@ FUNCTIONS FOR FILTER/PUT
urand32
(class=math #args=0) Integer uniformly distributed 0 and 2**32-1 inclusive.
urandelement
(class=math #args=1) Random sample from the first argument, which must be an non-empty array.
urandint
(class=math #args=2) Integer uniformly distributed between inclusive integer endpoints.

View file

@ -187,9 +187,9 @@ FUNCTION LIST
sha512 sin sinh sort splita splitax splitkv splitkvx splitnv splitnvx sqrt
ssub strftime strftime_local string strip strlen strptime strptime_local sub
substr substr0 substr1 system systime systimeint tan tanh tolower toupper
truncate typeof unflatten uptime urand urand32 urandint urandrange version !
!= !=~ % & && * ** + - . .* .+ .- ./ / // < << <= <=> == =~ > >= >> >>> ?: ??
??? ^ ^^ | || ~
truncate typeof unflatten uptime urand urand32 urandelement urandint
urandrange version ! != !=~ % & && * ** + - . .* .+ .- ./ / // < << <= <=> ==
=~ > >= >> >>> ?: ?? ??? ^ ^^ | || ~
COMMENTS-IN-DATA FLAGS
Miller lets you put comments in your data, such as
@ -2487,6 +2487,9 @@ FUNCTIONS FOR FILTER/PUT
urand32
(class=math #args=0) Integer uniformly distributed 0 and 2**32-1 inclusive.
urandelement
(class=math #args=1) Random sample from the first argument, which must be an non-empty array.
urandint
(class=math #args=2) Integer uniformly distributed between inclusive integer endpoints.

View file

@ -73,7 +73,7 @@ is 2. Unary operators such as `!` and `~` show argument-count of 1; the ternary
* [**Conversion functions**](#conversion-functions): [boolean](#boolean), [float](#float), [fmtnum](#fmtnum), [hexfmt](#hexfmt), [int](#int), [joink](#joink), [joinkv](#joinkv), [joinv](#joinv), [splita](#splita), [splitax](#splitax), [splitkv](#splitkv), [splitkvx](#splitkvx), [splitnv](#splitnv), [splitnvx](#splitnvx), [string](#string).
* [**Hashing functions**](#hashing-functions): [md5](#md5), [sha1](#sha1), [sha256](#sha256), [sha512](#sha512).
* [**Higher-order-functions functions**](#higher-order-functions-functions): [any](#any), [apply](#apply), [every](#every), [fold](#fold), [reduce](#reduce), [select](#select), [sort](#sort).
* [**Math functions**](#math-functions): [abs](#abs), [acos](#acos), [acosh](#acosh), [asin](#asin), [asinh](#asinh), [atan](#atan), [atan2](#atan2), [atanh](#atanh), [cbrt](#cbrt), [ceil](#ceil), [cos](#cos), [cosh](#cosh), [erf](#erf), [erfc](#erfc), [exp](#exp), [expm1](#expm1), [floor](#floor), [invqnorm](#invqnorm), [log](#log), [log10](#log10), [log1p](#log1p), [logifit](#logifit), [max](#max), [min](#min), [qnorm](#qnorm), [round](#round), [roundm](#roundm), [sgn](#sgn), [sin](#sin), [sinh](#sinh), [sqrt](#sqrt), [tan](#tan), [tanh](#tanh), [urand](#urand), [urand32](#urand32), [urandint](#urandint), [urandrange](#urandrange).
* [**Math functions**](#math-functions): [abs](#abs), [acos](#acos), [acosh](#acosh), [asin](#asin), [asinh](#asinh), [atan](#atan), [atan2](#atan2), [atanh](#atanh), [cbrt](#cbrt), [ceil](#ceil), [cos](#cos), [cosh](#cosh), [erf](#erf), [erfc](#erfc), [exp](#exp), [expm1](#expm1), [floor](#floor), [invqnorm](#invqnorm), [log](#log), [log10](#log10), [log1p](#log1p), [logifit](#logifit), [max](#max), [min](#min), [qnorm](#qnorm), [round](#round), [roundm](#roundm), [sgn](#sgn), [sin](#sin), [sinh](#sinh), [sqrt](#sqrt), [tan](#tan), [tanh](#tanh), [urand](#urand), [urand32](#urand32), [urandelement](#urandelement), [urandint](#urandint), [urandrange](#urandrange).
* [**String functions**](#string-functions): [capitalize](#capitalize), [clean_whitespace](#clean_whitespace), [collapse_whitespace](#collapse_whitespace), [gsub](#gsub), [lstrip](#lstrip), [regextract](#regextract), [regextract_or_else](#regextract_or_else), [rstrip](#rstrip), [ssub](#ssub), [strip](#strip), [strlen](#strlen), [sub](#sub), [substr](#substr), [substr0](#substr0), [substr1](#substr1), [tolower](#tolower), [toupper](#toupper), [truncate](#truncate), [\.](#dot).
* [**System functions**](#system-functions): [hostname](#hostname), [os](#os), [system](#system), [version](#version).
* [**Time functions**](#time-functions): [dhms2fsec](#dhms2fsec), [dhms2sec](#dhms2sec), [fsec2dhms](#fsec2dhms), [fsec2hms](#fsec2hms), [gmt2localtime](#gmt2localtime), [gmt2sec](#gmt2sec), [hms2fsec](#hms2fsec), [hms2sec](#hms2sec), [localtime2gmt](#localtime2gmt), [localtime2sec](#localtime2sec), [sec2dhms](#sec2dhms), [sec2gmt](#sec2gmt), [sec2gmtdate](#sec2gmtdate), [sec2hms](#sec2hms), [sec2localdate](#sec2localdate), [sec2localtime](#sec2localtime), [strftime](#strftime), [strftime_local](#strftime_local), [strptime](#strptime), [strptime_local](#strptime_local), [systime](#systime), [systimeint](#systimeint), [uptime](#uptime).
@ -879,6 +879,12 @@ urand32 (class=math #args=0) Integer uniformly distributed 0 and 2**32-1 inclus
</pre>
### urandelement
<pre class="pre-non-highlight-non-pair">
urandelement (class=math #args=1) Random sample from the first argument, which must be an non-empty array.
</pre>
### urandint
<pre class="pre-non-highlight-non-pair">
urandint (class=math #args=2) Integer uniformly distributed between inclusive integer endpoints.

View file

@ -71,3 +71,17 @@ func BIF_urandrange(input1, input2 *mlrval.Mlrval) *mlrval.Mlrval {
a + (b-a)*lib.RandFloat64(),
)
}
func BIF_urandelement(input1 *mlrval.Mlrval) *mlrval.Mlrval {
arrayval := input1.GetArray()
if arrayval == nil { // not an array
return mlrval.ERROR
}
if len(arrayval) == 0 {
return mlrval.ERROR
}
// lo is inclusive, hi is exclusive
index := lib.RandRange(0, len(arrayval))
return arrayval[index].Copy()
}

View file

@ -804,6 +804,13 @@ is normally distributed.`,
zaryFunc: bifs.BIF_urand32,
},
{
name: "urandelement",
class: FUNC_CLASS_MATH,
help: `Random sample from the first argument, which must be an non-empty array.`,
unaryFunc: bifs.BIF_urandelement,
},
// ----------------------------------------------------------------
// FUNC_CLASS_TIME

View file

@ -187,9 +187,9 @@ FUNCTION LIST
sha512 sin sinh sort splita splitax splitkv splitkvx splitnv splitnvx sqrt
ssub strftime strftime_local string strip strlen strptime strptime_local sub
substr substr0 substr1 system systime systimeint tan tanh tolower toupper
truncate typeof unflatten uptime urand urand32 urandint urandrange version !
!= !=~ % & && * ** + - . .* .+ .- ./ / // < << <= <=> == =~ > >= >> >>> ?: ??
??? ^ ^^ | || ~
truncate typeof unflatten uptime urand urand32 urandelement urandint
urandrange version ! != !=~ % & && * ** + - . .* .+ .- ./ / // < << <= <=> ==
=~ > >= >> >>> ?: ?? ??? ^ ^^ | || ~
COMMENTS-IN-DATA FLAGS
Miller lets you put comments in your data, such as
@ -2487,6 +2487,9 @@ FUNCTIONS FOR FILTER/PUT
urand32
(class=math #args=0) Integer uniformly distributed 0 and 2**32-1 inclusive.
urandelement
(class=math #args=1) Random sample from the first argument, which must be an non-empty array.
urandint
(class=math #args=2) Integer uniformly distributed between inclusive integer endpoints.

View file

@ -234,9 +234,9 @@ sec2gmt sec2gmtdate sec2hms sec2localdate sec2localtime select sgn sha1 sha256
sha512 sin sinh sort splita splitax splitkv splitkvx splitnv splitnvx sqrt
ssub strftime strftime_local string strip strlen strptime strptime_local sub
substr substr0 substr1 system systime systimeint tan tanh tolower toupper
truncate typeof unflatten uptime urand urand32 urandint urandrange version !
!= !=~ % & && * ** + - . .* .+ .- ./ / // < << <= <=> == =~ > >= >> >>> ?: ??
??? ^ ^^ | || ~
truncate typeof unflatten uptime urand urand32 urandelement urandint
urandrange version ! != !=~ % & && * ** + - . .* .+ .- ./ / // < << <= <=> ==
=~ > >= >> >>> ?: ?? ??? ^ ^^ | || ~
.fi
.if n \{\
.RE
@ -4004,6 +4004,15 @@ Int-valued example: '$n=floor(20+urand()*11)'.
.fi
.if n \{\
.RE
.SS "urandelement"
.if n \{\
.RS 0
.\}
.nf
(class=math #args=1) Random sample from the first argument, which must be an non-empty array.
.fi
.if n \{\
.RE
.SS "urandint"
.if n \{\
.RS 0

View file

@ -15,8 +15,8 @@ mlr \
@index = 1;
}
$color = @colors[urandint(1,length(@colors))];
$shape = @shapes[urandint(1,length(@shapes))];
$color = urandelement(@colors);
$shape = urandelement(@shapes);
$flag = (urand32() < 0.6) ? "true" : "false";
$index = @index;
$quantity=fmtnum(urandrange(50,100),"%.4f");

View file

@ -2,10 +2,7 @@
PUNCHDOWN LIST
* blockers:
- sort-hof check
- more linux perf checks
- mlr -O / abor!
> doc 07 int 08 float
- --ifs-regex & --ips-regex -- guessing is not safe as evidence by '.' and '|'
- big-picture item @ Rmd (csv memes; and beyond); also webdoc intro page
- function: randsel for arrays; use for example-csv-expander