Skip to content

Commit

Permalink
fix: Typo Closes #4.
Browse files Browse the repository at this point in the history
  • Loading branch information
imusmanmalik committed May 10, 2023
1 parent 84489c2 commit e773748
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 50 deletions.
15 changes: 8 additions & 7 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
go-version: 1.20

- name: Release
id: release_output
id: semrel
uses: go-semantic-release/action@v1
with:
changelog-generator-opt: "emojis=true"
Expand All @@ -35,11 +35,12 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- run: |
echo "Version: ${{ steps.release_output.outputs.version }}"
echo "Major Version: ${{ steps.release_output.outputs.version_major }}"
echo "Minor Version: ${{ steps.release_output.outputs.version_minor }}"
echo "Patch Version: ${{ steps.release_output.outputs.version_patch }}"
echo "Changelog: ${{ steps.release_output.outputs.changelog }}"
echo "Version: ${{ steps.semrel.outputs.version }}"
echo "Major Version: ${{ steps.semrel.outputs.version_major }}"
echo "Minor Version: ${{ steps.semrel.outputs.version_minor }}"
echo "Patch Version: ${{ steps.semrel.outputs.version_patch }}"
echo "Changelog: ${{ steps.semrel.outputs.changelog }}"
- name: Publish to pkg.go.dev
run: GOPROXY=proxy.golang.org go list -m github.com/imusmanmalik/randomizer@${{ steps.release_output.outputs.version }}
if: steps.semrel.outputs.version != ''
run: GOPROXY=proxy.golang.org go list -m github.com/imusmanmalik/randomizer@${{ steps.semrel.outputs.version }}
35 changes: 21 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,26 @@ import (
)

func main() {
// Generate a random integer between 0 and 100
n := yourpackage.RandomInt(100)
fmt.Println(n)

// Generate a random byte slice with 16 bytes
b := yourpackage.RandomBytes(16)
fmt.Printf("%x\n", b)

// Generate a random string with 10 characters
s := yourpackage.RandomString(10)
fmt.Println(s)
// Generate a random integer between 1 and 100
randomInt, err := randomizer.RandomInt(1, 100)
if err != nil {
panic(err)
}
fmt.Println(randomInt)

// Generate a random slice of bytes with length 16
randomBytes, err := randomizer.RandomBytes(16)
if err != nil {
panic(err)
}
fmt.Println(randomBytes)

// Generate a random string with length 32
randomString, err := randomizer.RandomString(32)
if err != nil {
panic(err)
}
fmt.Println(randomString)
}
```

Expand All @@ -42,6 +51,7 @@ func main() {
```shell
go test
```

## Contributing

Contributions are welcome! If you find a bug or have an idea for a new feature, please open an issue or submit a pull request on GitHub.
Expand All @@ -53,6 +63,3 @@ This library is licensed under Apache 2.0 License. See the [LICENSE file](https:
## Acknowledgments

This library was inspired by the math/rand package in the Go standard library, and the github.com/Pallinder/go-randomdata library.



48 changes: 31 additions & 17 deletions randomizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,44 @@ import (
"math/big"
)

// Generator is a struct that holds the random generator's configuration.
type Generator struct {
}
// RandomInt generates a cryptographically secure random integer between min and max (inclusive)
func RandomInt(min, max int64) (int64, error) {
// Create a new big.Int to hold the range of possible values
rangeSize := big.NewInt(max - min + 1)

// Generate a cryptographically secure random value within the range
randomValue, err := rand.Int(rand.Reader, rangeSize)
if err != nil {
return 0, err
}

// NewGenerator returns a new instance of the random generator.
func NewGenerator() *Generator {
return &Generator{}
// Convert the random value to an int64 and add the minimum value to shift the range
return randomValue.Int64() + min, nil
}

// Intn returns a cryptographically secure random integer between 0 and n.
func (g *Generator) Intn(n int) (int, error) {
max := big.NewInt(int64(n))
r, err := rand.Int(rand.Reader, max)
// RandomBytes generates a slice of cryptographically secure random bytes with a given length
func RandomBytes(length int) ([]byte, error) {
// Create a slice to hold the random bytes
randomBytes := make([]byte, length)

// Generate the random bytes using the crypto/rand package
_, err := rand.Read(randomBytes)
if err != nil {
return 0, err
return nil, err
}
return int(r.Int64()), nil

return randomBytes, nil
}

// Float64 returns a cryptographically secure random float64 between 0 and 1.
func (g *Generator) Float64() (float64, error) {
r, err := rand.Int(rand.Reader, big.NewInt(1e17))
// RandomString generates a cryptographically secure random string with a given length
func RandomString(length int) (string, error) {
// Generate a slice of random bytes with the given length
randomBytes, err := RandomBytes(length)
if err != nil {
return 0, err
return "", err
}
return float64(r.Int64()) / 1e17, nil

randomString := string(randomBytes)

return randomString, nil
}
41 changes: 29 additions & 12 deletions randomizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,41 @@ import (
"testing"
)

func TestGenerator(t *testing.T) {
g := NewGenerator()
func TestGenerateRandomInt(t *testing.T) {
// Generate a random integer between 1 and 100
randomInt, err := RandomInt(1, 100)
if err != nil {
t.Errorf("Error generating random integer: %v", err)
}

// Check that the generated integer is within the range
if randomInt < 1 || randomInt > 100 {
t.Errorf("Generated integer %v is not within the specified range", randomInt)
}
}

// Test Intn function.
n, err := g.Intn(100)
func TestGenerateRandomBytes(t *testing.T) {
// Generate a random byte slice with length 16
randomBytes, err := RandomBytes(16)
if err != nil {
t.Fatalf("Intn returned error: %s", err)
t.Errorf("Error generating random bytes: %v", err)
}
if n < 0 || n >= 100 {
t.Fatalf("Intn returned invalid value: %d", n)

// Check that the length of the generated byte slice is correct
if len(randomBytes) != 16 {
t.Errorf("Generated byte slice has incorrect length: expected 16, got %v", len(randomBytes))
}
}

// Test Float64 function.
f, err := g.Float64()
func TestGenerateRandomString(t *testing.T) {
// Generate a random string with length 32
randomString, err := RandomString(32)
if err != nil {
t.Fatalf("Float64 returned error: %s", err)
t.Errorf("Error generating random string: %v", err)
}
if f < 0.0 || f >= 1.0 {
t.Fatalf("Float64 returned invalid value: %f", f)

// Check that the length of the generated string is correct
if len(randomString) != 32 {
t.Errorf("Generated string has incorrect length: expected 32, got %v", len(randomString))
}
}

0 comments on commit e773748

Please sign in to comment.