-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#42: Add Rand func to generate a pseudo-random integer between min an…
…d max based on unix-nano time seed
- Loading branch information
1 parent
855a10f
commit ce5567d
Showing
5 changed files
with
44 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/.github export-ignore | ||
/.gitattributes export-ignore | ||
/.gitignore export-ignore | ||
/CODE_OF_CONDUCT.md export-ignore | ||
/CONTRIBUTING.md export-ignore |
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,13 @@ | ||
package pgo | ||
|
||
import ( | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
// Rand returns a pseudo-random integer between min and max based on unix-nano time seed | ||
// !! for random numbers suitable for security-sensitive work, use the crypto/rand package instead | ||
func Rand(min, max int64) int64 { | ||
rand.Seed(time.Now().UnixNano()) | ||
return rand.Int63n(max - min + 1) + min | ||
} |
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,13 @@ | ||
package pgo_test | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"pgo" | ||
"testing" | ||
) | ||
|
||
func TestRand(t *testing.T) { | ||
r1 := pgo.Rand(1, 100) | ||
assert.GreaterOrEqual(t, r1, int64(1)) | ||
assert.LessOrEqual(t, r1, int64(100)) | ||
} |