Programming Language: Golang

Namespace/Package Name: math/rand

Class/Type: Rand

Method/Function: Float32

Examples at hotexamples.com: 21

The `math/rand` package in Go provides functions to generate pseudo-random numbers. One of these functions is `Rand.Float32()`, which generates a random float between 0 and 1.

**Example 1:** Rolling a random number for a game

This code uses the `Rand.Float32()` function to roll a dice by generating a random number between 0 and 1 and multiplying it by 6.

**Example 2:** Generating a random RGB color

package main import ( "fmt" "math/rand" "time" ) func main() { rand.Seed(time.Now().UnixNano()) num := rand.Float32() * 6 // Roll a dice fmt.Println(num) }

This code uses the `Rand.Float32()` function to roll a dice by generating a random number between 0 and 1 and multiplying it by 6.

package main import ( "fmt" "math/rand" "time" ) func main() { rand.Seed(time.Now().UnixNano()) red := rand.Float32() green := rand.Float32() blue := rand.Float32() fmt.Printf("#%02x%02x%02x\n", int(red*255), int(green*255), int(blue*255)) }This code uses the `Rand.Float32()` function to generate random values for red, green, and blue color channels and then formats them as an RGB hexadecimal code. Both examples use the `math/rand` package in Go to generate random numbers.

Frequently Used Methods

Frequently Used Methods

Related

Related in langs