code.vegaprotocol.io/vega@v0.79.0/libs/test/random.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package test 17 18 import ( 19 crand "crypto/rand" 20 "encoding/hex" 21 "fmt" 22 "math/rand" 23 "strconv" 24 25 "golang.org/x/crypto/sha3" 26 ) 27 28 func RandomVegaID() string { 29 data := make([]byte, 10) 30 if _, err := crand.Read(data); err != nil { 31 panic(fmt.Errorf("couldn't generate random string: %w", err)) 32 } 33 34 hashFunc := sha3.New256() 35 hashFunc.Write(data) 36 hashedData := hashFunc.Sum(nil) 37 38 return hex.EncodeToString(hashedData) 39 } 40 41 func RandomNegativeI64() int64 { 42 return (rand.Int63n(1000) + 1) * -1 43 } 44 45 func RandomNegativeI64AsString() string { 46 return strconv.FormatInt(RandomNegativeI64(), 10) 47 } 48 49 func RandomI64() int64 { 50 return rand.Int63() 51 } 52 53 func RandomPositiveI64() int64 { 54 return rand.Int63() 55 } 56 57 func RandomPositiveI64Before(n int64) int64 { 58 return rand.Int63n(n) 59 } 60 61 func RandomPositiveU32() uint32 { 62 return rand.Uint32() + 1 63 } 64 65 func RandomPositiveU64() uint64 { 66 return rand.Uint64() + 1 67 } 68 69 func RandomPositiveU64AsString() string { 70 return strconv.FormatUint(RandomPositiveU64(), 10) 71 } 72 73 func RandomPositiveU64Before(n int64) uint64 { 74 return uint64(rand.Int63n(n)) 75 }