13

4

# Perl，37个42个字符

``````(\$d/=2)+=rand>.5for%!;print\$d/2<pop|0
``````

``````\$p=<>;do{\$p*=2;\$p-=(\$-=\$p)}while\$--(.5<rand);print\$-
``````

1

3

## Python，81个字符

``````import random
print(sum(random.randint(0,1)*2**-i for i in range(9))<input()*2)+0
``````

Matt

3

# Mathematica 165

``````d = RealDigits; r = RandomInteger;
f@n_ := If[(c = Cases[Transpose@{a = Join[ConstantArray[0, Abs[d[n, 2][[2]]]], d[n, 2][[1]]],
RandomInteger[1, {Length@a}]}, {x_, x_}]) == {}, r, c[[1, 1]]]
``````

``````f[.53]
``````

1个

``````Table[Count[Table[f[.53], {10^4}], 1]/10^4 // N, {50}]
Mean[%]
``````

{0.5292、0.5256、0.5307、0.5266、0.5245、0.5212、0.5316、0.5345、0.5297、0.5334、0.5306、0.5288、0.528、0.5379、0.5293、0.5263、0.539、0.5322、0.5195、0.5208、0.5382、0.543、0.5336、0.5305、0.5303 ，0.5297、0.5318、0.5243、0.5281、0.5361、0.5349、0.5308、0.5265、0.5309、0.5233、0.5345、0.5316、0.5376、0.5264、0.5269、0.5295、0.523、0.5294、0.5326、0.5316、0.5334、0.5165、0.5296、0.5266、0.5293 }

0.529798

.53的基数2表示为

.10000111101011100001010001111010111000010100011110110

1

``````import System.Random
g p|p>1=print 1|p<0=print 0|1>0=randomIO>>=g.(p*2-).f
f x|x=1|1>0=0.0
``````

0

# Wolfram语言（Mathematica），42个字节

``````RandomInteger[]/.⌈1-2#⌉:>#0@Mod[2#,1]&
``````

• 如果输入概率`p`小于1/2，则当coinflip变为0时，返回0。否则，递归于`2p`;。假设正确，则获得1的总概率为`2p`或的一半`p`
• 如果输入概率`p`大于1/2，则当coinflip出现1时，返回1。否则，递归`2p-1`; 假设正确，则获得0的总概率为`1-(2p-1)`或的一半`1-p`