Skip to main content

Generate random number in golang

Go program for Generate random number . Here problem description and other solutions.

package main
import "math/rand"
import "math"
import "fmt"
import "time"


// Print random number in given size
func simpleRandom(size int) {
	var i int = 0
	for (i < size) {
		// Get new rand number
		fmt.Println(rand.Intn(math.MaxInt64))
		i+=1
	}
}
func randomBetweenRange(min, max int) {
	// Calculate random number
	fmt.Println(rand.Intn(max - min) + min)
}
func main() {
	rand.Seed(time.Now().UnixNano())
	// Test Case
	simpleRandom(3)
	// Range from 1 to 10
	randomBetweenRange(1, 10)
	// Range from 50 to 100
	randomBetweenRange(50, 100)
	// Range from 1000 to 2000
	randomBetweenRange(1000, 2000)
}

Output

6910686081659432654
3675623921858517422
5176568151540234538
6
87
1709




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment