From dd53cdd22c09a0872f607ad15cc116389549a6e2 Mon Sep 17 00:00:00 2001 From: Energy Star Date: Mon, 25 Oct 2021 12:10:37 -0700 Subject: [PATCH] v0.0.3 --- CHANGELOG.md | 27 +++++++++++++-------------- example_test.go | 15 +++++++++++++++ randomness.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d56ac7c..78a0be9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,21 +7,31 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Roadmap -## [0.0.2] - 2021-10-01 +## [0.0.3] - 2021-10-25 +### Added +- Added the ability to generate X amount of random numbers. +## [0.0.2] - 2021-10-01 ### Added - More tests. ### Changed - - Improved validation for `min` and `max`. - Fixed `Generate` panic in case of `rand.Int` failure. ## [0.0.1] - 2021-09-24 +### Added +- [x] Ability to create custom errors +- [x] Ability to create custom errors with code +- [x] Ability to create custom errors with status code +- [x] Ability to create custom errors with message +- [x] Ability to create custom errors wrapping an error +- [x] Ability to create static (pre-created) custom errors +- [x] Ability to create dynamic (in-line) custom errors +- [x] Ability to print a custom error with a dynamic, and custom message ### Checklist - - [x] CI Pipeline: - [x] Lint - [x] Tests @@ -42,14 +52,3 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [x] Real testing - Examples: - [x] Example's test file - -### Added - -- [x] Ability to create custom errors -- [x] Ability to create custom errors with code -- [x] Ability to create custom errors with status code -- [x] Ability to create custom errors with message -- [x] Ability to create custom errors wrapping an error -- [x] Ability to create static (pre-created) custom errors -- [x] Ability to create dynamic (in-line) custom errors -- [x] Ability to print a custom error with a dynamic, and custom message diff --git a/example_test.go b/example_test.go index 0fa531a..3ea5938 100644 --- a/example_test.go +++ b/example_test.go @@ -150,3 +150,18 @@ func ExampleNew_mustGenerate() { // output: // false } + +// Demonstrates how to generate N amount of random numbers. +func ExampleNew_randomNumbers() { + r, err := New(18000, 50000, 100, true) + if err != nil { + log.Fatalln(err) + } + + numbers := r.MustGenerateMany(10) + + fmt.Println(len(numbers) == 10) + + // output: + // true +} diff --git a/randomness.go b/randomness.go index cf4e0b7..7a7ce7b 100644 --- a/randomness.go +++ b/randomness.go @@ -42,6 +42,7 @@ type Randomness struct { memory []int64 } +// Generate returns a random number. func (r *Randomness) Generate() (int64, error) { // calculate the max we will be using bg := big.NewInt(int64(r.Max - r.Min + 1)) @@ -94,6 +95,37 @@ func (r *Randomness) MustGenerate() int64 { return n } +// GenerateMany returns an slice of `n` numbers. +func (r *Randomness) GenerateMany(n int) ([]int64, error) { + numbers := []int64{} + + for i := 0; i < n; i++ { + n, err := r.Generate() + if err != nil { + return nil, err + } + + numbers = append(numbers, n) + } + + return numbers, nil +} + +// MustGenerateMany is like `GenerateMany`, but will panic in case of any error. +func (r *Randomness) MustGenerateMany(n int) []int64 { + numbers, err := r.GenerateMany(n) + if err != nil { + log.Panicln(err) + } + + return numbers +} + +////// +// Factory +////// + +// New is the Randomness factory. func New(min, max, maxRetry int, collisionFree bool) (*Randomness, error) { if min < 1 { return nil, ErrInvalidMin