`let prizes = ["頭獎", "二獎", "三獎", "四獎", "五獎"];`

```let prize;

let i = random_integer::random_i32(0, 99);
if i >= 0 && i <= 1 {
prize = prizes[0];
} else if i >= 2 && i <= 6 {
prize = prizes[1];
} else if i >= 7 && i <= 16 {
prize = prizes[2];
} else if i >= 17 && i <= 49 {
prize = prizes[3];
} else {
prize = prizes[4];
}

println!("{prize}");```

### Random Pick

「Random Pick」是筆者開發的套件，可以從一個或是多個陣列切片中，依權重隨機取得一個或是多個元素。

Crates.io

Cargo.toml

random-pick = "*"

#### 使用方法

`random_pick`這個crate，提供了`pick_from_slice`函數，可以從一個陣列中，依權重隨機取得一個元素。如本文開頭提到的例子，可以使用「Random Pick」套件改寫如下：

```fn main() {
let prizes = ["頭獎", "二獎", "三獎", "四獎", "五獎"];

let weights = [2, 5, 10, 33, 50];

let prize = random_pick::pick_from_slice(&prizes, &weights).unwrap();

println!("{prize}");
}```

```fn main() {
let prizes = ["頭獎", "二獎", "三獎", "四獎", "五獎"];

let weights = [2, 5, 10, 33, 50];

for prize in random_pick::pick_multiple_from_slice(&prizes, &weights, 5){
println!("{prize}");
}
}```

```fn main() {
let prizes = ["頭獎", "二獎", "三獎"];

let extra_prizes = ["加開四獎", "加開五獎"];

let weights = [2, 5, 10, 33, 50];

let prize = random_pick::pick_from_multiple_slices(&[&prizes, &extra_prizes], &weights).unwrap();

println!("{prize}");
}```

```fn main() {
let prizes = ["頭獎", "二獎", "三獎"];

let extra_prizes = ["加開四獎", "加開五獎"];

let weights = [2, 5, 10, 33, 50];

for prize in random_pick::pick_multiple_from_multiple_slices(&[&prizes, &extra_prizes], &weights, 5) {
println!("{prize}");
}
}```

```fn main() {
let prizes = ["頭獎", "二獎", "三獎", "四獎"];

let weights = [20, 80];

let prize = random_pick::pick_from_slice(&prizes, &weights).unwrap();

println!("{prize}");
}```