-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathrandom_element.go
46 lines (37 loc) · 1.22 KB
/
random_element.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
package faker
// RandomElement returns a fake random element from a given list of elements
func RandomElement[T any](f Faker, elements ...T) T {
i := f.IntBetween(0, len(elements)-1)
return elements[i]
}
// RandomElementWeighted takes faker instance and a list of elements in the form of map[weight]element,
// it then selects one of the elements randomly and returns it,
//
// Elements with higher weight have more chance to be returned.
func RandomElementWeighted[T any](f Faker, elements map[int]T) T {
arrayOfElements := make([]T, 0)
for weight, value := range elements {
// TODO: there is an accepted proposal for generic slices.Repeat function in Go 1.23
for i := 0; i < weight; i++ {
arrayOfElements = append(arrayOfElements, value)
}
}
i := f.IntBetween(0, len(arrayOfElements)-1)
return arrayOfElements[i]
}
func RandomMapKey[K comparable, V any](f Faker, m map[K]V) K {
keys := make([]K, 0, len(m))
for k := range m {
keys = append(keys, k)
}
i := f.IntBetween(0, len(keys)-1)
return keys[i]
}
func RandomMapValue[K comparable, V any](f Faker, m map[K]V) V {
values := make([]V, 0, len(m))
for k := range m {
values = append(values, m[k])
}
i := f.IntBetween(0, len(values)-1)
return values[i]
}