github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/common/util.go (about)

     1  package common
     2  
     3  import (
     4  	"crypto/rand"
     5  	"github.com/shopspring/decimal"
     6  	"io"
     7  	"math/big"
     8  )
     9  
    10  func RandomCSPRNG(n int) []byte {
    11  	buff := make([]byte, n)
    12  	_, err := io.ReadFull(rand.Reader, buff)
    13  	if err != nil {
    14  		panic("reading from crypto/rand failed: " + err.Error())
    15  	}
    16  	return buff
    17  }
    18  
    19  func CalcPer(a,b *big.Int) (float64,bool) {
    20  	//a,b := big.NewInt(1),big.NewInt(3)
    21  	ad,bd := decimal.NewFromBigInt(a,0),decimal.NewFromBigInt(b,0)
    22  	cd := ad.DivRound(bd,4)
    23  	return cd.Float64()
    24  }