Skip to content

Commit

Permalink
feat: Add pseudo random number generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Allaman committed May 19, 2024
1 parent 1caaa8e commit 3a76a76
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Commands:
lorem paragraphs Paragraphs
htpasswd Create a htpasswd string
reverse Reverse the input
rng Random number generator
split Split a string
tail Returns the last n lines
url encode Encode string to valid URL
Expand Down Expand Up @@ -92,6 +93,7 @@ However, some commands read from named arguments, for example:
```sh
gstring htpasswd -u bar -p test
gstring lorem sentences -c 20
gstring rng -s 1 -e 100 -c 10 -d ";"
```
## What about the name?
Expand Down
17 changes: 17 additions & 0 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type CLI struct {
} `cmd:"" help:"Generate Lorem Ipsum"`
HtPassWD HtPassWDCmd `cmd:"" name:"htpasswd" help:"Create a htpasswd string"`
Reverse reverseCmd `cmd:"" help:"Reverse the input"`
Rng rngCmd `cmd:"" help:"Random number generator"`
Split splitCmd `cmd:"" help:"Split a string"`
Tail tailCmd `cmd:"" help:"Returns the last n lines"`
URL struct {
Expand Down Expand Up @@ -350,3 +351,19 @@ func (c *tailCmd) Run(globals *Globals) error {
printOutput(tail(in, c.Num), globals.Trim)
return nil
}

type rngCmd struct {
Start int `default:"0" short:"s" help:"Min RNG"`
End int `default:"1" short:"e" help:"Max RNG"`
Count int `default:"1" short:"c" help:"Number of generated RNGs"`
Delimiter string `default:" " short:"d" help:"Output delimiter"`
}

func (c *rngCmd) Run(globals *Globals) error {
rngs, err := pseudoRandomGenerator(c.Start, c.End, c.Count, c.Delimiter)
if err != nil {
return err
}
printOutput(rngs, globals.Trim)
return nil
}
22 changes: 22 additions & 0 deletions rng.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"fmt"
"math/rand"
"strings"
)

func pseudoRandomGenerator(start, end, n int, delimiter string) (string, error) {
if start > end {
return "", fmt.Errorf("start should be less than or equal to end")
}
if n <= 0 {
return "", fmt.Errorf("number of random numbers to generate should be greater than zero")
}

rngs := make([]int, n)
for i := 0; i < n; i++ {
rngs[i] = rand.Intn(end-start+1) + start
}
return strings.Trim(strings.Join(strings.Fields(fmt.Sprint(rngs)), delimiter), "[]"), nil
}
51 changes: 51 additions & 0 deletions rng_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"strconv"
"strings"
"testing"
)

func TestPseudoRandomGenerator(t *testing.T) {
tests := []struct {
name string
start int
end int
n int
wantError bool
}{
{"ValidRange", 1, 10, 5, false},
{"StartGreaterThanEnd", 10, 1, 5, true},
{"NegativeCount", 1, 10, -5, true},
{"ZeroCount", 1, 10, 0, true},
{"SingleNumberRange", 5, 5, 5, false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := pseudoRandomGenerator(tt.start, tt.end, tt.n, " ")
if (err != nil) != tt.wantError {
t.Errorf("pseudoRandomGenerator() error = %v, wantError %v", err, tt.wantError)
return
}

if !tt.wantError {
// Split the result into individual numbers
numbers := strings.Split(got, " ")
if len(numbers) != tt.n {
t.Errorf("expected %d numbers, got %d", tt.n, len(numbers))
}

for _, numStr := range numbers {
num, err := strconv.Atoi(numStr)
if err != nil {
t.Errorf("failed to convert %s to int: %v", numStr, err)
}
if num < tt.start || num > tt.end {
t.Errorf("number %d out of range [%d, %d]", num, tt.start, tt.end)
}
}
}
})
}
}

0 comments on commit 3a76a76

Please sign in to comment.