Random Numbers

This Go code demonstrates the use of the rand package to generate random numbers. Let's go through the code with inline comments and explanations:

// Importing necessary packages.
import (
	"fmt"
	"math/rand"
	"time"
)

// The main function, where the execution of the program begins.
func main() {
	// Generating random integers between 0 and 99.
	fmt.Print(rand.Intn(100), ",")
	fmt.Print(rand.Intn(100))
	fmt.Println()

	// Generating a random floating-point number between 0.0 and 1.0.
	fmt.Println(rand.Float64())

	// Generating random floating-point numbers in a specific range.
	fmt.Print((rand.Float64()*5)+5, ",")
	fmt.Print((rand.Float64()*5)+5)
	fmt.Println()

	// Using a specific seed to generate random numbers (seeded by current time).
	s1 := rand.NewSource(time.Now().UnixNano())
	r1 := rand.New(s1)
	fmt.Print(r1.Intn(100), ",")
	fmt.Print(r1.Intn(100))
	fmt.Println()

	// Using a specific seed (42) to generate random numbers.
	s2 := rand.NewSource(42)
	r2 := rand.New(s2)
	fmt.Print(r2.Intn(100), ",")
	fmt.Print(r2.Intn(100))
	fmt.Println()

	// Using the same seed (42) to generate consistent random numbers.
	s3 := rand.NewSource(42)
	r3 := rand.New(s3)
	fmt.Print(r3.Intn(100), ",")
	fmt.Print(r3.Intn(100))
}

Output

1,80
0.6344691951573025
6.025591616231384,7.334962673868585
70,43
5,87
5,87

Explanation:

  1. Generating Random Integers:

    • rand.Intn(100) generates a random integer between 0 and 99.

  2. Generating Random Floating-Point Numbers:

    • rand.Float64() generates a random floating-point number between 0.0 and 1.0.

    • (rand.Float64()*5)+5 generates a random floating-point number in the range [5.0, 10.0).

  3. Seeding Random Number Generator:

    • rand.NewSource(time.Now().UnixNano()) creates a new source using the current Unix timestamp as the seed.

    • rand.New(s1) creates a new random number generator using the source.

  4. Consistent Random Numbers with the Same Seed:

    • Using a specific seed (42) results in the same sequence of random numbers each time.

This code demonstrates the basic usage of the rand package in Go to generate random numbers, both integers and floating-point, with and without seeding. Seeding is useful to ensure reproducibility if needed.

Last updated