码迷,mamicode.com
首页 > 其他好文 > 详细

3.8 随机数

时间:2018-03-22 01:44:43      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:ack   read   package   else   UNC   pack   reader   print   enc   


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\n", 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\n", 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

*/

3.8 随机数

标签:ack   read   package   else   UNC   pack   reader   print   enc   

原文地址:https://www.cnblogs.com/zrdpy/p/8620793.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!