# 用欧拉计划学Rust编程，第323题，随机整数按位或运算

### 第一步：

fn trial() -> u32 {
let mut x = 0_u32;
for i in 1.. {
x |= rand::random::<u32>();
if x == 0xFFFFFFFF {
return i;
}
}
0
}


3/4 * E(2) = 8/4
E(2) = 8/3

### 第四步：

7/8 * E(3) = 1/8 + 3/8 * (1+ E(1)) + 3/8 * (1 + E(2)) + 1/8
7/8 * E(3) = 1/8 + 3/8 * 3 + 3/8 * 11/3 + 1/8
E(3) = 22/7

### 第五步，现在可以总结出最一般的规律：

E(N) = 1/2^N + C(N,1)/2^N * (1+E(1)) + C(N,2)/2^N * (1+E(2)) + ... + C(N,N-1)/2^N * (1+E(N-1)) + 1/2^N (1+E(N))

(2N-1)/2N * E(N) = 1/2^N + C(N,1)/2^N * (1+E(1)) + C(N,2)/2^N * (1+E(2)) + ... + C(N,N-1)/2^N * (1+E(N-1)) + 1/2^N

E(N) = [1 + C(N,1) * (1+E(1)) + C(N,2) * (1+E(2)) + ... + C(N,N-1) * (1+E(N-1)) + 1] / (2^N-1)

fn comb(n: usize, r: usize) -> f64 {
let mut c = 1_f64;
for i in 0..r {
c *= ((n - i) as f64) / (r - i) as f64;
}
c
}


let mut e = vec![0.0, 2.0];
for n in 2..=32 {
let mut f = 1.0 as f64;
for r in 1..=n {
f += comb(n, r) * (1.0 + e[n - r]);
}
f = f / (2_f64.powf(n as f64) - 1.0);
e.push(f);
println!("E({}) = {:.10}", n, f);
}


----==== Email: slofslb (GTD) qq.com 请将(GTD)换成@ ====----

---- 魔方桥牌象棋、游戏人生...