-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add: random element and weighted random element. (#170)
* add: random element and weighted random element. * fix: time format in ISO8601.
- Loading branch information
Showing
4 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
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] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package faker | ||
|
||
import "testing" | ||
|
||
func TestRandomElement(t *testing.T) { | ||
f := New() | ||
m := []string{"str1", "str2"} | ||
randomStr := RandomElement(f, m...) | ||
Expect(t, true, randomStr == "str1" || randomStr == "str2") | ||
} | ||
|
||
func TestRandomElementWeighted(t *testing.T) { | ||
f := New() | ||
m := map[int]string{ | ||
0: "zeroChance", | ||
1: "someChance", | ||
5: "moreChance", | ||
} | ||
|
||
for i := 0; i < 5; i++ { | ||
got := RandomElementWeighted(f, m) | ||
Expect(t, true, got == "someChance" || got == "moreChance") | ||
Expect(t, true, got != "zeroChance") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters