Programming Language: Golang

Namespace/Package Name: math/rand

Class/Type: Rand

Method/Function: Int

Examples at hotexamples.com: 30

The go math/rand package provides pseudorandom number generation functions. One of the most commonly used functions is RandInt, which generates a random integer within a specified range.

**Example 1:** Generate a random number between 0 and 9 inclusive.

**Example 2:** Generate a random number between -50 and 50 inclusive.

import ( "fmt" "math/rand" ) func main() { rand.Seed(time.Now().UnixNano()) // set seed to current time num := rand.Intn(10) // generate a random number between 0 and 9 fmt.Println("Random number:", num) }

import ( "fmt" "math/rand" ) func main() { rand.Seed(time.Now().UnixNano()) num := rand.Intn(101) - 50 // generate a random number between -50 and 50 fmt.Println("Random number:", num) }In both examples, the RandInt function is used to generate a random integer within a specified range. The first example generates a number between 0 and 9 inclusive, while the second example generates a number between -50 and 50 inclusive. The math/rand package is used in both examples.

Frequently Used Methods

Frequently Used Methods

Related

Related in langs