# [R], 32 bytes (Or 25 bytes in R>=4.1 using ‘\’ instead of ‘function’) <!-- language-all: lang-r --> function(x)max(sample(c(x,x),2)) [Try it online!][TIO-lm3eehnq] [R]: https://www.r-project.org/ [TIO-lm3eehnq]: https://tio.run/##K/qfZvs/rTQvuSQzP0@jQjM3sUKjODG3ICdVI1mjQqdCU8dIU/N/ma2hlaEBV0liElC8OLGgIKdSAyQCBjpw7ZmaaRplmkD1AA —— # [R], 29 bytes (Or 22 bytes in R>=4.1 using ‘\’ instead of ‘function’) <!-- language-all: lang-r --> function(x)sample(rep(x,x),1) [Try it online!][TIO-lm3aaf3g] [R]: https://www.r-project.org/ [TIO-lm3aaf3g]: https://tio.run/##K/qfZvs/rTQvuSQzP0@jQrM4MbcgJ1WjKLVAo0KnQlPHUPN/ma2hlaEBV0liElCiOLGgIKdSAyQCBjpwvZmaaRplmpqa/wE "R – Try It Online" Only valid for integer input. —— # [R], 25 bytes (Or 18 bytes in R>=4.1 using ‘\’ instead of ‘function’) <!-- language-all: lang-r --> function(x)sample(x,1,,x) [Try it online!][TIO-lm39gyvx] [R]: https://www.r-project.org/ [TIO-lm39gyvx]: https://tio.run/##K/qfZvs/rTQvuSQzP0@jQrM4MbcgJ1WjQsdQR6dC83@ZraGVoQFXSWISULA4saAgp1IDJAIGOnB9mZppGmWampr/AQ "R – Try It Online" The 4th argument of R’s ‘sample’ function is ‘prob’=“a vector of probability weights for obtaining the elements of the vector being sampled”. This solution may unfortunately fail if the input vector has only 1 element (due to a ‘convenience’ feature of ‘sample’ which changes its behaviour in this case…). Fixing this brings it up to 33 bytes: <!-- language-all: lang-r --> function(x)sample(y<-c(0,x),1,,y)