Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

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.

PreviousTime Formatting / ParsingNextNumber Parsing

Last updated 1 year ago

Was this helpful?