diff --git a/captcha/random.go b/captcha/random.go new file mode 100644 index 0000000..4a98798 --- /dev/null +++ b/captcha/random.go @@ -0,0 +1,13 @@ +package captcha + +import ( + "math/rand" +) + +func NewRandomCaptcha() Captcha { + availables := []func() Captcha{ + NewBannerCaptcha, + NewMathCaptcha, + } + return availables[rand.Intn(len(availables))]() // nolint:gosec +} diff --git a/captcha/random_test.go b/captcha/random_test.go new file mode 100644 index 0000000..6803414 --- /dev/null +++ b/captcha/random_test.go @@ -0,0 +1,30 @@ +package captcha_test + +import ( + "fmt" + "math/rand" + + "moul.io/captcha/captcha" +) + +func ExampleNewRandomCaptcha() { + rand.Seed(42) + + // first captcha + c := captcha.NewRandomCaptcha() + question, _ := c.Question() + fmt.Println(question) + + // second captcha + c = captcha.NewRandomCaptcha() + question, _ = c.Question() + fmt.Println(question) + + // Output: + // 3 + 4 + // _ _ + // _ __ | |_ | |_ _ _ ___ + // | '_ \| _|| _|| || |/ -_) + // | .__/ \__| \__| \_,_|\___| + // |_| +}