Skip to content

Commit

Permalink
#42: Add Rand func to generate a pseudo-random integer between min an…
Browse files Browse the repository at this point in the history
…d max based on unix-nano time seed
  • Loading branch information
arthurkushman committed Jan 19, 2020
1 parent 855a10f commit ce5567d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .gitattributes
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Go library for PHP community with convenient functions
* [Network](#user-content-network)
* [IP2Long/Long2Ip](#user-content-ip2longlong2ip)
* [GetMxrr](#user-content-getmxrr)
* [Math](#user-content-math)
* [Rand](#user-content-rand)

#### Installation

Expand Down Expand Up @@ -310,3 +312,10 @@ ip := pgo.Long2ip(2956665461) // "176.59.34.117"
```go
isMx, mxs, _ := pgo.GetMxrr("google.com") // e.g.: true, n
```

### Math

#### Rand
```go
rand := pgo.Rand(1, 100)
```
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module pgo

go 1.13

require github.com/stretchr/testify v1.4.0
require (
github.com/djherbis/atime v1.0.0
github.com/stretchr/testify v1.4.0
)
13 changes: 13 additions & 0 deletions math.go
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
}
13 changes: 13 additions & 0 deletions math_test.go
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))
}

0 comments on commit ce5567d

Please sign in to comment.