forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrand.go
44 lines (37 loc) · 1.36 KB
/
rand.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package expression
import "time"
const maxRandValue = 0x3FFFFFFF
// MysqlRng is random number generator and this implementation is ported from MySQL.
// See https://github.com/tikv/tikv/pull/6117#issuecomment-562489078.
type MysqlRng struct {
seed1 uint32
seed2 uint32
}
// NewWithSeed create a rng with random seed.
func NewWithSeed(seed int64) *MysqlRng {
seed1 := uint32(seed*0x10001+55555555) % maxRandValue
seed2 := uint32(seed*0x10000001) % maxRandValue
return &MysqlRng{seed1: seed1, seed2: seed2}
}
// NewWithTime create a rng with time stamp.
func NewWithTime() *MysqlRng {
return NewWithSeed(time.Now().UnixNano())
}
// Gen will generate random number.
func (rng *MysqlRng) Gen() float64 {
rng.seed1 = (rng.seed1*3 + rng.seed2) % maxRandValue
rng.seed2 = (rng.seed1 + rng.seed2 + 33) % maxRandValue
return float64(rng.seed1) / float64(maxRandValue)
}