> For the complete documentation index, see [llms.txt](https://ineelhere.gitbook.io/code-with-go/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ineelhere.gitbook.io/code-with-go/57-random-numbers.md).

# 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:

```go
// 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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ineelhere.gitbook.io/code-with-go/57-random-numbers.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
