Go: Generate a random character (rune)

Between 'a' and 'z'

Use the rand.Intn function in package math/rand.

rand.Seed(time.Now().UnixNano())
c := 'a' + rand.Intn(26)

Without the call to rand.Seed, a program will produce the same sequence of pseudo-random numbers for each execution.

From arbitrary set

chars := []rune("ab⌘")
c := chars[rand.Intn(len(chars))]

Comments

Be the first to comment!