• 3.8 随机数


    
    package main
    
    import (
    	crypto "crypto/rand"
    	"fmt"
    	"math/big"
    	"math/rand"
    )
    
    func main() {
    
    	sec1 := rand.New(rand.NewSource(10))
    	sec2 := rand.New(rand.NewSource(10))
    	for i := 0; i < 5; i++ {
    		rnd1 := sec1.Int()
    		rnd2 := sec2.Int()
    		if rnd1 != rnd2 {
    			fmt.Println("Rand generated non-equal sequence")
    			break
    		} else {
    			fmt.Printf("Math/Rand1: %d , Math/Rand2: %d
    ", rnd1, rnd2)
    		}
    	}
    
    	for i := 0; i < 5; i++ {
    		safeNum := NewCryptoRand()
    		safeNum2 := NewCryptoRand()
    		if safeNum == safeNum2 {
    			fmt.Println("Crypto generated equal numbers")
    			break
    		} else {
    			fmt.Printf("Crypto/Rand1: %d , Crypto/Rand2: %d
    ", safeNum, safeNum2)
    		}
    	}
    }
    
    func NewCryptoRand() int64 {
    	safeNum, err := crypto.Int(crypto.Reader, big.NewInt(100234))
    	if err != nil {
    		panic(err)
    	}
    	return safeNum.Int64()
    }
    
    /*
    Math/Rand1: 5221277731205826435 , Math/Rand2: 5221277731205826435
    Math/Rand1: 3852159813000522384 , Math/Rand2: 3852159813000522384
    Math/Rand1: 8532807521486154107 , Math/Rand2: 8532807521486154107
    Math/Rand1: 3888302351045490779 , Math/Rand2: 3888302351045490779
    Math/Rand1: 4512466281294657143 , Math/Rand2: 4512466281294657143
    Crypto/Rand1: 8037 , Crypto/Rand2: 39548
    Crypto/Rand1: 70036 , Crypto/Rand2: 36267
    Crypto/Rand1: 57332 , Crypto/Rand2: 51974
    Crypto/Rand1: 71516 , Crypto/Rand2: 5445
    Crypto/Rand1: 62317 , Crypto/Rand2: 61380
    
    */
    
    
  • 相关阅读:
    02-zabbix安装部署
    01-zabbix服务说明
    00-ContOS 7.5编译安装MySQL-5.7.30
    bitset习题
    数颜色[分块]
    旋转子段 (思维stl)
    双栈排序(洛谷P1155)二分图的判定+思维贪心
    常见的系统获取唯一码方式
    SHELL-数组
    Prometheus-alertmanager组件使用
  • 原文地址:https://www.cnblogs.com/zrdpy/p/8620793.html
Copyright © 2020-2023  润新知