-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjar.go
More file actions
94 lines (84 loc) · 1.37 KB
/
Copy pathjar.go
File metadata and controls
94 lines (84 loc) · 1.37 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Imagine we have a jar with numbers from one to ten in it.
// We take one number after the other out of the jar and put
// them in a line from left to right on the table.
package main
import (
"bytes"
"crypto/rand"
"fmt"
"math/big"
"sort"
"strconv"
)
const (
runs = 10
max = runs
)
func getRandomInt(max int) (num int64) {
num = 1
// this is from 0 to max-1
b, err := rand.Int(rand.Reader, big.NewInt(int64(max)))
if err != nil {
panic(err)
}
num += b.Int64()
return num
}
func main() {
kv := make(map[int]int64)
i := 1
for i <= runs {
switch i {
case 1:
// shortcut for first key
kv[i] = getRandomInt(max)
case runs:
// shortcut for last key
var j int64
for j = 1; j <= int64(runs); j++ {
found := false
for _, v := range kv {
if j == v {
found = true
break
}
}
if !found {
kv[i] = j
break
}
}
default:
for {
num := getRandomInt(max)
found := false
for _, v := range kv {
if num == v {
found = true
break
}
}
if !found {
kv[i] = num
break
}
}
}
i++
}
var keys []int
for k := range kv {
keys = append(keys, k)
}
sort.Ints(keys)
var buf bytes.Buffer
i = 1
for _, k := range keys {
buf.WriteString(strconv.FormatInt(kv[k], 10))
if i < len(keys) {
buf.WriteString(" ")
}
i++
}
fmt.Println(buf.String())
}