How can I initialy set a random value over uint 64 through adopting big type


Like the picture given illstruate that the big cannot initially set value over int64, but if I want to do it, how can I to achieve it?

I want to randomly initiate the value!

@calmh

Can you help me to solve it? Thank you!

Look at the big package’s Rand funciton: https://golang.org/pkg/math/big/#Int.Rand

Tanks for your answer. Firstly, I’m sorry about I don’t correctly raise the question. What I mean is that I want to set a value over uint64, like 9999999999999. How can I initiate the value through type of big

Please don’t tag me personally. And, @CurtGreen answered your question perfectly, I think.

Use the SetString method… to wit:

package main

import (
	"fmt"
	"math/big"
)

func main() {
	i := new(big.Int)
	i.SetString("10000000000000000000000000000000000000000000000000000000001", 10) // octal
	fmt.Println(i)
}

Not very random.

In my opinion the only true answer for “random value” has been given by @CurtGreen in How can I initialy set a random value over uint 64 through adopting big type

Indeed! Perhaps OP found the oddness of the receiver confusing… a complete example at https://play.golang.org/p/fAloeFThFgT

For convenience:

package main

import (
	"fmt"
	"math/big"
	"math/rand"
)

func main() {
	n := new(big.Int)
	n.SetString("10000000000000000000000000000000000000000000000000000000001", 10)
	src := rand.NewSource(10000001)
	rdm := rand.New(src)
	x := new(big.Int)
	bigrand := x.Rand(rdm,n)
	fmt.Printf("n=%v\n", n)
	fmt.Printf("x=%v\n", x)
	fmt.Printf("bigrand=%v\n", bigrand)
}
1 Like