Generate big number

hi I would like to generate a number between 1 and 11003746553177333670973076095724981362930873955835419598865333617435674804223

only after several searches I can’t find anything here is a piece of my code :

	// Initialise big numbers with small numbers
count := big.NewInt(0)
count.SetString(String(77), 10)

// Loop forever because we're never going to hit the end anyway
for {
	// Increment our counter
	count := big.NewInt(0)
	count.SetString(String(77), 10)
1 Like
package main

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

// randInt returns a random number in the interval [min, max]
func randInt(min, max *big.Int) (*big.Int, error) {
	if min.Cmp(max) > 0 {
		min, max = max, min
	}
	intvl := new(big.Int)
	intvl.Add(intvl.Sub(max, min), big.NewInt(1))
	r, err := rand.Int(rand.Reader, intvl)
	if err != nil {
		return nil, err
	}
	r.Add(r, min)
	return r, nil
}

func main() {
	min, ok := new(big.Int).SetString(
		"1",
		10,
	)
	if !ok {
		return
	}
	max, ok := new(big.Int).SetString(
		"11003746553177333670973076095724981362930873955835419598865333617435674804223",
		10,
	)
	if !ok {
		return
	}
	fmt.Println(min, max)
	r, err := randInt(min, max)
	fmt.Println(r, err)
	if err != nil {
		return
	}
}

https://play.golang.org/p/IvdhPo7D0xj

1 11003746553177333670973076095724981362930873955835419598865333617435674804223
10540576144031712458356493685186348014723344343763680035148450277505266702161 <nil>
2 Likes

thank you very much :grin:

random between 1000 and 11003746553177333670973076095724981362930873955835419598865333617435674804223 ?

See my revised, earlier answer. Set

min = 1000
max = 11003746553177333670973076095724981362930873955835419598865333617435674804223