[Question]
  [
Input a decimal number and round it to an integer, randomly rounding up or down with a probability based on its fractional part, so the expected value of the output equals to the input value.
If input \$x\$ is an integer, the program should output it as is. If \$x\$ is not an integer, the program has a \$x-\left\lfloor x \right\rfloor\$ probability to output \$\left\lceil x \right\rceil\$, and a \$\lceil x\rceil-x\$ probability to output \$\left\lfloor x \right\rfloor\$.
In the above formula, \$\left\lfloor i\right\rfloor\$ means rounding \$i\$ down to the nearest integer; \$\left\lceil i\right\rceil\$ means rounding \$i\$ up to the nearest integer.
## Examples
* For input 2.4, it has 60% probability to output 2, and 40% probability to output 3.
* For input 3.9, it has 10% probability to output 3, and 90% probability to output 4.
* For input 0.5, it has 50% probability to output 0, and 50% probability to output 1.
* For input -5.25, it has 75% probability to output -5, and 25% probability to output -6.
* For input 8, it has 100% probability to output 8, and 0% probability to output 7 or 9.
## Rules
* To make the challenge easier, reasonable errors in probability are allowed.
	+ For any input \$-100<x<100\$, the probability error should be less than \$0.01\%\$.
* You may assume your language's built-in random number generator is perfectly balanced.
* You may assume the input / output value fits your languages decimal number types, as long as [this does not trivialize the challenge](https://codegolf.meta.stackexchange.com/a/8245/44718).
* As usual, this is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"). So shortest code wins.
  
[Answer]
# [APL(Dyalog Unicode)](https://dyalog.com), 6 bytes [SBCS](https://github.com/abrudz/SBCS)
```
⌊?⍤≡+⊢
```
[Try it on APLgolf!](https://razetime.github.io/APLgolf/?h=AwA&c=e9TTZf@od8mjzoXaj7oWAQA&f=e9Q7VyGoNE8hNTE5QyE5sThVwU@hJDM3tVghMS9FoTgjv1yhJCNVITm/NK9EIT8Noq4otbg0p4TLT@FR2wQFQwMg4ApJLS4Bc6urH/XuUtB41LnoUe9WzdpHPTvSDq3we9S7BcithSgz0jPhUleHsI31LOFsAz1TOPvQelM9IwTXAgA&i=AwA&r=tryapl&l=apl-dyalog&m=train&n=f)
Add a random number in \$(0,1)\$ and floor.
[Answer]
# [R](https://www.r-project.org), 20 bytes
```
\(n)(n+runif(1))%/%1
```
[Attempt This Online!](https://ato.pxeger.com/run?1=ddHBasMgGMBxdt1TfFACShMb09gkY3uTXcw2WyGoJHros_SSQvsGe5Cd-zSLCCM7fB704N-fiJfrON8UvBb34FXRfr8TQ4nZjsFoRTil2S7jaevx9HOEN1DBfHhtzdKBG61jXvbDF0nzJJ0bzoS_8DKO_C_WVC0HlvF8JBWr6SYH7eEkJziUWXR62etB-zN4CzZ4FzxUOUjzCTUa7Nmi7Vm30jgeJ61DgzpqJRMrTaBxmTQ84FErBKvWXiOwvBAJrPDiEMX231vR29ukofsN2BE6lj52ntP6Cw)
[Answer]
# [MathGolf](https://github.com/maxbergmark/mathgolf/blob/master/math_golf.txt), ~~4~~ 3 [bytes](https://github.com/maxbergmark/mathgolf/blob/master/code_page.py)
```
ƒ-ü
```
Port of [*@ovs*' APL answer](https://codegolf.stackexchange.com/a/244212/52210).  
-1 byte thanks to [*@nextwayup*'s 05AB1E answer](https://codegolf.stackexchange.com/a/244218/52210) by using \$ceil(input-random)\$ instead of \$floor(input+random)\$.  
Make sure to upvote both of them as well!
[Try it online.](https://tio.run/##y00syUjPz0n7///YJN3De/7/N9Iz4TLWs@Qy0DPl0jXVMzLlsuDSNTQw4AJiAA)
**Explanation:**
```
ƒ    # Random float in the range [0,1]
 -   # Subtract this from the (implicit) input-float
  ü  # Ceil it to an integer
     # (after which the entire stack is output implicitly)
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), ~~12~~ ~~11~~ 9 bytes
*-2 bytes thanks to Kevin Cruijssen*
```
4°LΩ4°/-î
```
[Try it online!](https://tio.run/##yy9OTMpM/f/f5NAGn3MrgaS@7uF1//8b6ZkAAA "05AB1E – Try It Online")
Does 05AB1E really not have a function for random(0,1)? Anyway, here's an answer.
Explanation:
```
4°L             # create list of length 10000 (Ždt = 10000)
   Ω            # choose an integer from the list
    4°/        # divide it by 10000 to get a random decimal number
       -       # subtract the random number from the input
        î      # round up
```
[Answer]
# [Lua](https://www.lua.org), 36 29 bytes
*-7 thanks to @Sisyphus*
```
print((...+math.random())//1)
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m704pzRxwYKlpSVpuhZ7C4oy80o0NPT09LRzE0sy9IoS81LyczU0NfX1DTUhaqBKl0ab6pnGQjkA) Adds a random float between 0 and 1 to the input and then floors.
[![execute](https://i.stack.imgur.com/arAzi.gif)](https://i.stack.imgur.com/arAzi.gif)
[Attempt This Online! (36 byte version)](https://ato.pxeger.com/run?1=m704pzRxwYKlpSVpuhY3VQqKMvNKNHITSzL00nLy84s09PT0tMHcosS8lPxcDU1NTYhaqJaV0aYgoGcaCxUAAA)
*insert furious clicking of the execute button*
[Answer]
# x86 machine code (32-bit), ~~18~~ 9 bytes
```
0F C7 F1 39 D1 83 D0 00 C3
```
Saved **9** bytes. Thanks to Peter Cordes.
### Disassembly
```
0:  0f c7 f1                rdrand ecx
3:  39 d1                   cmp    ecx,edx
5:  83 d0 00                adc    eax,0x0
8:  c3                      ret
```
### Explanation
The function has the same behavior as `randRound` in the following C code.
```
#include <stdint.h>
static uint32_t rand_u32() {
    uint32_t r;
    __asm__ ("rdrand\t%0" : "=r"(r));
    return r;
}
__attribute__((regparm(3)))
int32_t randRound(uint32_t hi, uint32_t lo) {
    return hi + (rand_u32() < lo);
}
```
`rdrand` reads the hardware random number generator. The Intel manual states the validity of its result should be checked through the carry flag, but according to Peter Cordes, `rdrand` never fails on Ivy Bridge, so this code is always valid on Ivy Bridge, and mostly valid on other machines.
The single statement in `randRound` is all of the algorithm, but I have to explain how the data is represented.
Each fractional number is encoded in 64 bits. The high 32 bits is the integral part and the low 32 bits is the fractional part. The following list shows how each hex value is decoded to an actual value.
```
0000 0000 0000 0000 -> 0
7fff ffff 0000 0000 -> 0x7fff'ffff
8000 0000 0000 0000 -> -0x8000'0000
ffff ffff 0000 0000 -> -1
0000 0001 0000 0001 -> 1 + 1 / 0x1'0000'0000
ffff ffff 0000 0001 -> -1 + 1 / 0x1'0000'0000
ffff ffff ffff ffff -> -1 + 0xffff'ffff / 0x1'0000'0000
```
Such representation is common in fixed point arithmetic because the basic operations are very cheap. Here are some examples using 16/16-bit split, unlike the 32/32-bit split for the entry.
```
typedef uint32_t t_fix;
t_fix fix_add(t_fix x, t_fix y) {
    return x + y;
}
t_fix fix_sub(t_fix x, t_fix y) {
    return x - y;
}
t_fix fix_mul(t_fix x, t_fix y) {
    return (uint64_t)x * y >> 16;
}
```
[Answer]
# [Python 3](https://docs.python.org/3/), 44 bytes
```
lambda i:(i+random())//1
from random import*
```
[Try it online!](https://tio.run/##Jcg9CoQwEAbQ3lN8ZaIx/oMKe5NtIhIcMJkwpPH0cWGrBy89@eI4Ff/5ltuF43SgXVEjLp4clNZdN1ReOOA/oJBYcl08CwgUMdrZYLKbQW8Xg3ax4491r4AkFLPyirQuLw "Python 3 – Try It Online")
-4 bytes by porting [ovs's APL answer](https://codegolf.stackexchange.com/a/244212/87681)
[Answer]
# [Ruby](https://www.ruby-lang.org/), 18 bytes
```
->n{(n+rand).to_i}
```
[Try it online!](https://tio.run/##KypNqvyfZvtf1y6vWiNPuygxL0VTryQ/PrP2v6GBgV5JZm5qcXVNXk2BApAL5esl55fmlVSnRedpGegZGMba2hrW1v4HAA "Ruby – Try It Online")
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 7 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
+Ø%X÷¤Ḟ
```
A monadic Link that accepts a number and yields an integer.
Note: given an integer there is a one in \$2^{32}\$ chance of outputting an integer one greater than the input, this is within the \$0.01\%\$ specified.
**[Try it online!](https://tio.run/##y0rNyan8/1/78AzViMPbDy15uGPe////dU31jEwB "Jelly – Try It Online")**
### How?
```
+Ø%X÷¤Ḟ - Link: number, N
     ¤  - nilad followed by link(s) as a nilad:
 Ø%     -   4294967296
   X    -   random integer from [1..4294967296]
    ÷   -   divide by 4294967296
+       - N add that
      Ḟ - floor
```
[Answer]
# [PARI/GP](https://pari.math.u-bordeaux.fr), ~~32~~ 24 bytes
```
f(a)=(a+random()/2^31)\1
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m728ILEoMz69YMGCpaUlaboWO9I0EjVtNRK1ixLzUvJzNTT1jeKMDTVjDCHSN10LijLzSjTSNIz0TDQ1uWA8Yz1LJJ6BnikST9dUzwiZb6GpCTEMZicA)
-8, cheers to @alephalpha for the explanation on PARI/GP
[Answer]
# TI-Basic, 4 [bytes](https://codegolf.meta.stackexchange.com/a/4764/98541)
```
int(Ans+rand
```
the input is `Ans` (last value entered)
[![enter image description here](https://i.stack.imgur.com/sjCQ1m.jpg)](https://i.stack.imgur.com/sjCQ1m.jpg)
[Answer]
# [Perl 5](https://www.perl.org/) `-p`, 22 bytes
```
$_=int$_+($_/abs)*rand
```
[Try it online!](https://tio.run/##K0gtyjH9/18l3jYzr0QlXltDJV4/MalYU6soMS/l/39dUz0j03/5BSWZ@XnF/3V9TfUMDA3@6xYAAA "Perl 5 – Try It Online")
[Answer]
# [Vyxal 2.4.1](https://github.com/Vyxal/Vyxal/releases/tag/v2.4.1), 4 bytes
```
∆Ṙ+⌊
```
[Try it Online!](https://lyxal.pythonanywhere.com?flags=&code=%E2%88%86%E1%B9%98%2B%E2%8C%8A&inputs=3.2&header=&footer=)
When rewriting Vyxal, we forgot `∆Ṙ`. It's not a feature, [it's a bug](https://github.com/Vyxal/Vyxal/issues/802).
Same as most other answers, adds a random float and floors.
This uses python's `random.random` which is a random 32-bit float in \$[0, 1)\$.
[Answer]
# [Julia 1.0](http://julialang.org/), 17 bytes
```
!x=ceil(x-rand())
```
[Try it online!](https://tio.run/##yyrNyUw0rPj/X7HCNjk1M0ejQrcoMS9FQ1Pzf1p@kUKFQmaeQrSpnpGOgqmehY6CrrmeoY6CoQEQG@mZxnIpAAFIXSZInaGVoQFYBAQKijLzSnLyNBQrNMFiqXkpXMjixaW5QDmw5niYZoN4AwMDTX0YA0W9JhfQhP8A "Julia 1.0 – Try It Online")
the same as other answers here
[Answer]
# [Math++](https://esolangs.org/wiki/Math%2B%2B), 23 bytes
```
?>a
_a+!_($rand/(a-_a))
```
[Answer]
# [Retina](https://github.com/m-ender/retina/wiki/The-Language), 88 bytes
```
$
.
\.\d*
$&0000
(\.....).*
$1*_,10000*
L$@`(?<=(-?)(\d+)(_*),_+)((?!\3))?
$1$.($2*_$#4*
```
[Try it online!](https://tio.run/##FYqxCsJAEAX79xfiKrtrsuQuOVBQzs7GT1i4CElhYyH@/3mZZmCY7/p7f16hHvgxV4LBzRcFHYcG2G1DrJWgpQtbVTzpPnO@3rjPwr6chItKV5o573wUyW0nY4paaD9prdEmjHbBYAl9sphw/gM "Retina – Try It Online") Link includes test cases. Accurate to `0.01%` as required Explanation:
```
$
.
```
Append a decimal point in case the input is an integer.
```
\.\d*
$&0000
```
Ensure that there are at least 4 digits after the decimal point.
```
(\.....).*
$1*_,10000*
```
Multiply the decimal part of the input by `10,000` and convert it to unary, and separately add another `10,000` in unary.
```
L$@`(?<=(-?)(\d+)(_*),_+)((?!\3))?
```
Randomly match one `_` of the `10,000`, then test whether the random value was less than the decimal fraction.
```
$1$.($2*_$#4*
```
Keep the sign and increment the magnitude if it was.
[Answer]
# [PowerShell](https://github.com/TryItOnline/TioSetup/wiki/Powershell), 39 bytes
```
[Math]::Floor($args[0]+(random -ma 1.))
```
[Try it online!](https://tio.run/##bZExb8IwEIXn@FdYUaiSooSkW0EMVSukDu2AyoQY3MZAJMd2HVsMlN@e@hwDSYsXW3ffu/d0luJAVbOnjLXRdn5s129E7zfT6YIJoeKIqF2zzjfjWBFeihqnNcFFliTtCUXc1HiOH7LHGYqUMLyk5UraynnEM61YxXcxgMmVeREH3qO8kWe0NaR6JZdEV8JSroFT3BcnkyLL74s8v/BQPStsPR2OAUxowj5oo5uOcNrSqubwMBKuIst63M/oiIJIURDcRVsMOVBQbW0iKKb0e5gJWzxwI8fjGQpOmLKG3sRX0sNGdig6IRS@cmk0fjf1J1UYztRZhijsYi8Jx/7YzjWnBZbdXOwW6wEIAlq3B9ztBvqgHa5sZLGnL20IG2KxmzHpWcHKk1HP0P72JZGRf@2ge7HzX/HPzEOx1d@0WpCKWaduB94q7oGpFaYuaRK2vw "PowerShell – Try It Online")
[Answer]
# [Haskell](https://www.haskell.org/), 65 bytes
```
import System.Random
f x=do n<-randomIO::IO Float;print$floor$n+x
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/PzO3IL@oRCG4srgkNVcvKDEvJT@XK02hwjYlXyHPRrcILODpb2Xl6a/glpOfWGJdUJSZV6KSlpOfX6SSp13xPzcxMw@omkshTcEUTOiRSxlCKEsQZQYkNAx0gXKapLP@AwA "Haskell – Try It Online")
[Answer]
# [C (gcc)](https://gcc.gnu.org/), ~~53~~ 44 bytes
```
i;f(float n){i=n+((n>0)-.5)*rand()/(5<<30);}
```
[Try it online!](https://tio.run/##TY7RqsIwDIbvfYogCIlra9EzONLpi3i8GNsqBe1kLSiOvbq1duNob9L8@fiSip@qKgSjNOpzW3qw1JudzRDtXhIXOS270tZIK8yLYiNJDcFYD5fSWCToZxCfS4g3lwYlkUrZaPON8@5whB30a/HDYCO2DKTIGfBcrGP5HSa87QDfYmPr5h55qaZvAc48mlZjctFq6pZjqyDLEkdJM97ztT@axhsSdFT/8/cuF6fRS5/02sVc43yhge9hUf/ZOQPPwE3MMBvCs9Ln8uQCv70A "C (gcc) – Try It Online")
*Saved a whopping 9 bytes thanks to [att](https://codegolf.stackexchange.com/users/81203/att)!!!*
[Answer]
# R, 26/31 bytes
For anything that has *probability* in it there **has** to be at least one attempt in R.
Here we have a function in 31 bytes:
```
f=function(x)floor(x+runif(1))
```
and the same thing taking stdin input in 26 bytes:
```
floor(scan(n=1)+runif(1))
```
[Answer]
# [Desmos](https://desmos.com/calculator), 21 bytes
```
f(n)=ceil(n-random())
```
Press the randomize button at the top left (The icon next to the plus sign) to run the code again.
The simulation code will run the randomization automatically and keep track of the frequency and distribution (more details in the graph).
[Try It On Desmos!](https://www.desmos.com/calculator/yu38g3zxmh)
[Try It On Desmos! - Simulation](https://www.desmos.com/calculator/vtb6hkcq9m)
[Answer]
# [J](https://www.jsoftware.com/#/), ~~9~~ 7 bytes
-2 thanks to user ovs' comment:
```
<.@+?@0
```
Original:
```
(<.@+?)&0
```
Port of [@ovs' APL answer](https://codegolf.stackexchange.com/a/244212/18872)
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 12 bytes
```
≧Ｘ²φＩ⌊⁺∕‽φφＮ
```
[Try it online!](https://tio.run/##FcqxCoMwEADQvV@R8QJpB3FzKi2FDi3BP7hq1IOYk0uin3/FN79hQRkYo@oHt3vONKee5qWA5yOIM40zk@0uXigVeGAu8IrMAj7WDE/aaQzQYxp5hcme15l32mr51vUXBOypU21urV73@Ac "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
≧Ｘ²φ
```
Square the predefined value for `1,000`, so that the error will be less than `0.01%` as desired. (Unfortunately without this the error would be `0.1%`.)
```
Ｉ⌊⁺∕‽φφＮ
```
Divide a random integer between `0` and `1,000,000` by `1,000,000`, add it to the input, and floor the result.
[Answer]
# [Python 3](https://docs.python.org/3/), 57 bytes
```
lambda i:math.floor(i+random.random())
import random,math
```
[Try it online!](https://tio.run/##JcoxCoAwDEDR3VNkbDEGUQsqeBOXihQDtimhi6eviNOHx89PuSSNNWx7vX08Tg@8Rl8uCreIGm7Vp1Mi/THWNhyzaIEf8HtrEAUGTjDQhCMt2JPDztHgcF6zciomGLa2vg "Python 3 – Try It Online")
[Answer]
# Swift, 67 bytes
```
import Foundation
let f={(a:Double)in floor(a + .random(in:0..<1))}
```
[Try it online!](https://tio.run/##HcpBCsJADEDR/ZwiywRhqNJV0Z14DRlpA4FpUmJKF6VnH4t/@fjfTTj61mRezANetupYQkxTnQL4sWMZnrZ@6kSiwNXMscAFshcdbUbRocv5fiU6GpvDG87tLx3sCc4WFw1kvOWeKB3tBw)
[Answer]
# [JavaScript (V8)](https://v8.dev/), ~~45~~ 29 bytes
```
n=>Math.ceil(n-Math.random())
```
[Try it online!](https://tio.run/##VZDBTsMwEETv/oo9gGSTxriFSkUhvSFOCIlrFCnGODSVsZHt9pL628MmTStxmtFo94129/Iog/Ldb8yPm6EtB1tu32TccaU7Q20@eS/tl/uhjA1EORsiRN9JE6CEpRCiIKQiACv@uEB54E@jCL4eJV/z1WQ2pOat8y9S7aiFcgs9hmeYcgcbkdWnAjMcAmp0hA4jUaA8z3Xos4xNi5dVjzMttayYMwRVvsaQXv3pBIJBBstxJs2lzmhu3DdtPnQ4mBim1pvepmZCvX/utYpcWyzW4cxiPDgfKbteQSu/AFWz8Zh/TMjh1UXE@QSdRVVwf/nY3fixdAuunZOGYWNiwx8 "JavaScript (V8) – Try It Online")
I am an idiot, this is a much better way of doing it.
Here is my old solution:
```
n=>Math.random()>(n>0?n:-n)%1?~~n:n>0?-~n:~-n
```
[Try it online!](https://tio.run/##VZBBb4MwDIXv/AofVikZJQvtKnVl0NO00zRp1woJlsFKxZwppL1Q@OvMobTSTs95sr/n@JCf8kaZ6tcGp/VQxgPGyVtu98Lk@KV/GE8YJnKLmwD5LNz2PW7cOyDtAxw8pbGxYE2V1w3EEEopI8/beQAL8TgnWYonJ1KsnAQrsRiLtZeKUpuXXO0ZQpxAS@YFpvQRLbHaLiKPmoDVhYWKLBmRPE9xVPs@Hwevo4Z6SoY8mjwC7UxKJrvV5zNIDj6ErqebQnVdiFp/s@yjaI61bcbUuxa7bES9fx4KZUWBFFw0Fxa/rc92Zg4q5e4X/2AQwKu2xDEdVEiq4OF6qnt3qm4GupycjFNUx4c/ "JavaScript (V8) – Try It Online")
A bit convoluted to make it work for negatives, but essentially it checks whether the result from `Math.random()` is greater than the absolute value of the input. If it is, return the input rounded towards zero, otherwise away from zero.
]