How to shuffle a string in Go

1 Answer

0 votes
package main

import (
    "fmt"
    "math/rand"
    "time"
)

func main() {
    rand.Seed(time.Now().UnixNano()) // Seed the random number generator
    str := "Golang Programming"
    
    runes := []rune(str) // Convert string to a slice of runes

    rand.Shuffle(len(runes), func(i, j int) {
        runes[i], runes[j] = runes[j], runes[i]
    })

    shuffledStr := string(runes) // Convert the slice of runes back to a string
    
    fmt.Println(shuffledStr)
}



/*
run:

ngoin mmlgaGargroP

*/

 



answered Nov 4, 2024 by avibootz
...