R, 27 bytes
(Or 20 bytes in R>=4.1 using \
instead of function
)
function(x)-sample(-x,1,,x)
The 4th argument of R’s sample
function is prob
=“a vector of probability weights for obtaining the elements of the vector being sampled”.
Unfortunately, a “convenience” feature of sample
changes its behaviour when its first argument is a single positive number, so we need to negate it here to avoid this, and then re-negate the result.
——
R, 31 bytes
(Or 24 bytes in R>=4.1 using \
instead of function
)
function(x)max(-sample(-x,2,T))
Return the highest of two random picks.
——
R, 29 bytes
(Or 22 bytes in R>=4.1 using \
instead of function
)
function(x)sample(rep(x,x),1)
Only valid for integer input.