Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
v0.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
thalesfsp committed Oct 25, 2021
1 parent c057fcd commit dd53cdd
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 14 deletions.
27 changes: 13 additions & 14 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
15 changes: 15 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
32 changes: 32 additions & 0 deletions randomness.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit dd53cdd

Please sign in to comment.