Go
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions and Methods
-
Section 4: Concurrency
-
Section 5: Packages and Tooling
-
Section 6: More Standard Library
-
Section 7: Building Services
-
Section 8: Advanced Go
-
Section 9: Go in the Cloud-Native Ecosystem
-
Section 10: Data Structures and Algorithms in Go
-
Section 11: Testing and Deployment
-
Section 12: Practical Projects
-
Section 13: More Standard Library Practice
-
Section 14: More Practice Projects
-
Section 15: Design Patterns in Go
-
Section 16: Interview Practice
-
Section 17: Package fmt In Depth
-
Section 18: Package strings and strconv
-
Section 19: Package os and io
-
Section 20: Package time
-
Section 21: Package sort and container
-
Section 22: Package encoding
-
Section 23: Package net/http In Depth
-
Section 24: Package context
-
Section 25: Package regexp and bytes
-
Section 26: Package errors In Depth
-
Section 27: Package crypto and hash
-
Section 28: Package flag and log
-
Section 29: Package sync In Depth
-
Section 30: More Practice Exercises
-
Section 31: Go Modules and Workspaces In Depth
-
Section 32: Generics Deep Dive (Go 1.18+)
-
Section 33: Testing Package In Depth
-
Section 34: More Interview and Whiteboard Practice
-
Section 35: Package math and unicode
-
Section 36: Package path and filepath
-
Section 37: Package database/sql
-
Section 38: Package text/template and html/template
-
Section 39: Package archive and compress
-
Section 40: Lower-Level net Package
-
Section 41: Package runtime and reflect
-
Section 42: Package embed and io/fs
-
Section 43: Go Assembly and CGO Basics
-
Section 44: Building CLIs and TUIs
-
Section 45: Go Performance Tuning
-
Section 46: More Real-World Projects
-
Section 47: Go in Production
-
Section 48: Go Design Patterns
-
Section 49: Go Interfaces Deep Dive
-
Section 50: Final Practice and Review
185: math/rand vs math/rand/v2
I often see developers assume that math/rand/v2 is just a "version update" in the way a library might go from 1.1 to 1.2. They think they can just swap the import path and everything will work the same, only slightly faster. That's a dangerous assumption because v2 isn't a patch; it's a redesign that fixes several architectural mistakes we've had to live with in Go for over a decade.
Thinking v2 is a drop-in replacement
If you try to just change your import and run your code, your compiler is going to yell at you immediately. The most visible change is the naming convention. In v1, we had rand.Intn(). In v2, it's rand.IntN(). It seems like a trivial change—just a capital 'N'—but it's part of a larger effort to make the API consistent with the rest of the Go standard library.
// This works in v1, but fails in v2
import "math/rand"
val := rand.Intn(100)
// This is the v2 way
import "math/rand/v2"
val := rand.IntN(100)
But the real issue isn't the capitalization. It's the behavior of the global generator. In v1, the global functions are protected by a mutex. If you have a high-concurrency application—say, a load balancer assigning random weights to thousands of requests per second—all those goroutines are fighting over a single lock. I've seen production apps crawl to a halt because of contention in math/rand.
Moving past the global seed ritual
For years, the first thing every Go developer wrote was rand.Seed(time.Now().UnixNano()). If you forgot it, your "random" numbers were the same every time you restarted the app. It was a boilerplate tax we all paid.
In v2, that ritual is dead. The global generator is now automatically seeded. You don't need to call a seed function just to get basic randomness. More importantly, v2 introduces ChaCha8, a much faster and more statistically sound algorithm than the old lagged Fibonacci generator used in v1. When I'm writing new services now, I don't even think about v1 unless I'm maintaining a legacy codebase.
Here is how the mental model shifts when you actually need a specific source of randomness (like for a reproducible test) in v2:
import "math/rand/v2"
func main() {
// Instead of the old rand.NewSource(), we use NewPCG
// PCG is a modern, efficient generator
pcg := rand.NewPCG(1, 2)
r := rand.New(pcg)
fmt.Println(r.IntN(100))
}
Notice NewPCG. The v1 Source interface was clunky and limited. The new PCG (Permuted Congruential Generator) is simply better. It's faster, has a longer period, and is easier to reason about. I'll be honest: the transition to v2 is one of the few times where the "breaking change" of a version bump is actually a gift to the developer.
- Use
math/rand/v2for all new projects. - Stop using
rand.Seed(); it's gone (and unnecessary). - Check your function calls:
Intnis nowIntN,Float64stays the same, but the underlying engine is vastly improved.
📋 Practical Task
Refactoring a Random Session ID Generator to v2
You have a legacy function that generates a random session string. It currently uses math/rand (v1) and relies on the old Seed pattern. Your task is to refactor this to use math/rand/v2.
Requirements:
- Replace the
math/randimport withmath/rand/v2. - Remove the call to
rand.Seed()entirely. - Update the random integer generation to use the correct
v2function naming. - Ensure the function still produces a random string of 10 alphanumeric characters.
package main
import (
"fmt"
"math/rand" // Change this
"time"
)
func init() {
// Remove this logic
rand.Seed(time.Now().UnixNano())
}
func generateSessionID() string {
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
b := make([]byte, 10)
for i := range b {
// Update this call to v2
b[i] = charset[rand.Intn(len(charset))]
}
return string(b)
}
func main() {
fmt.Println("Session ID:", generateSessionID())
}
There are no comments for now.