/
misc.go
64 lines (58 loc) · 1.93 KB
/
misc.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2019 spaGO Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package rand
import (
"github.com/nlpodyssey/spago/pkg/utils"
"golang.org/x/exp/rand"
)
// ShuffleInPlace pseudo-randomizes the order of elements, modifying the
// given slice in-place.
func ShuffleInPlace(xs []int, generator *LockedRand) []int {
swap := func(i, j int) { xs[i], xs[j] = xs[j], xs[i] }
if generator != nil {
generator.Shuffle(len(xs), swap)
} else {
rand.Shuffle(len(xs), swap) // Warning: use global rand
}
return xs
}
// WeightedChoice performs a random generation of the indices based of the probability distribution itself.
// Please note that it uses the global random.
func WeightedChoice(dist []float64) int {
rnd := rand.Float64() // // Warning: use global rand
cumulativeProb := 0.0
for i, prob := range dist {
cumulativeProb += prob
if rnd < cumulativeProb {
return i
}
}
return 0
}
// GetUniqueRandomInt generates n mutually exclusive integers up to max, using the default random source.
// The callback checks whether a generated number can be accepted, or not.
func GetUniqueRandomInt(n, max int, valid func(r int) bool) []int {
a := make([]int, n)
for i := 0; i < n; i++ {
r := rand.Intn(max) // Warning: use global rand
for !valid(r) || utils.ContainsInt(a, r) {
r = rand.Intn(max) // Warning: use global rand
}
a[i] = r
}
return a
}
// GetUniqueRandomIndices select n mutually exclusive indices, using the global random.
// The callback checks whether an extracted index can be accepted, or not.
func GetUniqueRandomIndices(n int, indices []int, valid func(r int) bool) []int {
a := make([]int, n)
for i := 0; i < len(a); i++ {
r := ShuffleInPlace(indices, nil)[0] // Warning: use global rand
for !valid(r) || utils.ContainsInt(a, r) {
r = ShuffleInPlace(indices, nil)[0] // Warning: use global rand
}
a[i] = r
}
return a
}