-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add pseudo random number generator
- Loading branch information
Showing
4 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} |