diff --git a/faker.go b/faker.go index 64ee61b..7434489 100644 --- a/faker.go +++ b/faker.go @@ -84,6 +84,11 @@ func (f Faker) RandomStringElement(s []string) string { return s[i] } +func (f Faker) RandomIntElement(a []int) int { + i := f.IntBetween(0, len(a)-1) + return a[i] +} + func (f Faker) ShuffleString(s string) string { orig := strings.Split(s, "") dest := make([]string, len(orig)) diff --git a/faker_test.go b/faker_test.go index 367fdd1..16824a0 100644 --- a/faker_test.go +++ b/faker_test.go @@ -84,6 +84,19 @@ func TestRandomLetter(t *testing.T) { Expect(t, 1, len(value)) } +func TestRandomIntElement(t *testing.T) { + f := New() + elements := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} + element := f.RandomIntElement(elements) + found := false + for _, i := range elements { + if i == element { + found = true + } + } + Expect(t, true, found) +} + func TestShuffleString(t *testing.T) { f := New() orig := "foo bar"