KCL: Fix random number generator

Fix the random number generator by adding seed.
https://stackoverflow.com/questions/12321133/golang-random-number-generator-how-to-seed-properly

Jira CNA-1119

Change-Id: Idfe23d84f31a47dcf43c8025632ff6f115614d34
This commit is contained in:
Tao Jiang 2018-09-04 09:09:06 -07:00
parent 22de13ef8a
commit 9addbb57f0
3 changed files with 53 additions and 2 deletions

View file

@ -20,6 +20,7 @@ package utils
import (
"math/rand"
"time"
)
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
@ -31,6 +32,7 @@ const (
func RandStringBytesMaskImpr(n int) string {
b := make([]byte, n)
rand.Seed(time.Now().UTC().UnixNano())
// A rand.Int63() generates 63 random bits, enough for letterIdxMax letters!
for i, cache, remain := n-1, rand.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {

View file

@ -0,0 +1,50 @@
/*
* Copyright (c) 2018 VMware, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package utils
import (
"fmt"
"math/rand"
"testing"
"time"
)
func TestRandom(t *testing.T) {
for i := 0; i < 10; i++ {
s1 := RandStringBytesMaskImpr(10)
s2 := RandStringBytesMaskImpr(10)
if s1 == s2 {
t.Fatalf("failed in generating random string. s1: %s, s2: %s", s1, s2)
}
}
}
func TestRandomNum(t *testing.T) {
rand.Seed(time.Now().UTC().UnixNano())
for i := 0; i < 10; i++ {
s1 := rand.Int63()
s2 := rand.Int63()
if s1 == s2 {
t.Fatalf("failed in generating random string. s1: %d, s2: %d", s1, s2)
}
fmt.Println(s1)
fmt.Println(s2)
}
}

View file

@ -335,10 +335,9 @@ func (checkpointer *DynamoCheckpoint) getItem(shardID string) (map[string]*dynam
}
func (checkpointer *DynamoCheckpoint) removeItem(shardID string) error {
var item *dynamodb.DeleteItemOutput
err := try.Do(func(attempt int) (bool, error) {
var err error
item, err = checkpointer.svc.DeleteItem(&dynamodb.DeleteItemInput{
_, err = checkpointer.svc.DeleteItem(&dynamodb.DeleteItemInput{
TableName: aws.String(checkpointer.TableName),
Key: map[string]*dynamodb.AttributeValue{
LEASE_KEY_KEY: {