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.
- 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.
- 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.
- As usual, this is code-golf. So shortest code wins.
xshould the program possibly handle? \$\endgroup\$
floattype in most languages should be enough. \$\endgroup\$
floor(input + randomFloat0To1())could have a small probability output 9 for input 8 due to floating point errors. This is allowed in this question. But you would not want this happen in your real word codes. \$\endgroup\$