-
Notifications
You must be signed in to change notification settings - Fork 1
/
5.1-randomized-algs.go
50 lines (42 loc) · 1.03 KB
/
5.1-randomized-algs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"fmt"
"math/rand"
)
func main() {
for i := 0; i < 100; i++ {
fmt.Println(random(1, 10))
}
}
// Exercises
// 5.1-2
func random(a, b int) int {
n := b - a + 1
if n == 1 {
return a
}
// User binary-search-like method if n is even
if n%2 == 0 {
if rand.Intn(2) == 0 {
return random(a, a+n/2-1)
} else {
return random(a+n/2, b)
}
} else {
sum := 0
for i := 1; i <= n; i++ {
sum += rand.Intn(2)
}
if sum == n {
return b
} else {
return random(a, b-1)
}
}
}
// TODO: 5.1-3
// Suppose that you want to output 0 with probability 1=2 and 1 with probability 1=2.
// At your disposal is a procedure BIASED-RANDOM, that outputs either 0 or 1.
// It outputs 1 with some probability p and 0 with probability 1-p, where 0 < p < 1, but you do not know what p is.
// Give an algorithm that uses BIASED-RANDOM as a subroutine, and returns an unbiased answer, returning 0 with probability 1/2
// and 1 with probability 1/2. What is the expected running time of your algorithm as a function of p?