Skip to content

Commit

Permalink
add number random as string
Browse files Browse the repository at this point in the history
  • Loading branch information
Erfan Momeni authored and thanhpk committed Oct 18, 2022
1 parent e66b069 commit d192e61
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ _testmain.go
*.prof

vendor/

.idea
12 changes: 12 additions & 0 deletions randstr.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,15 @@ func String(n int, letters ...string) string {
}
return bb.String()
}

// Number generates a random number string with length of n
func Number(n int) string {
var bb bytes.Buffer
bb.Grow(n)
l := uint32(10)
// on each loop, generate one random rune and append to output
for i := 0; i < n; i++ {
bb.WriteRune(defLetters[binary.BigEndian.Uint32(Bytes(4))%l])
}
return bb.String()
}
33 changes: 23 additions & 10 deletions randstr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ func ExampleHex() {
fmt.Println(token)
}
// Output:
// 67aab2d956bd7cc621af22cfb169cba8
// 226eeb52947edbf3e97d1e6669e212c2
// 5f3615e95d103d14ffb5b655aa0eec1e
// ff3ab4efbd74025b87b14b59422d304c
// a6705813c174ca73ed795ea0bab12726
// 67aab2d956bd7cc621af22cfb169cba8
// 226eeb52947edbf3e97d1e6669e212c2
// 5f3615e95d103d14ffb5b655aa0eec1e
// ff3ab4efbd74025b87b14b59422d304c
// a6705813c174ca73ed795ea0bab12726
}

func ExampleString() {
Expand All @@ -23,9 +23,22 @@ func ExampleString() {
fmt.Println(token)
}
// Output:
// 7EbxkrHc1l3Ahmyr
// I5XH2gc1EEHgbmGI
// GlCycMpsxGkn9cDQ
// U2OfBDQoak0z8FwV
// kDX1m81u14YwEiCY
// 7EbxkrHc1l3Ahmyr
// I5XH2gc1EEHgbmGI
// GlCycMpsxGkn9cDQ
// U2OfBDQoak0z8FwV
// kDX1m81u14YwEiCY
}

func ExampleNumber() {
for i := 0; i < 5; i++ {
token := Number(16)
fmt.Println(token)
}
// Output:
//1232392418047380
//9160917876815937
//6629264107419930
//0271037110897873
//0337735480322223
}

0 comments on commit d192e61

Please sign in to comment.