package main
import (
"fmt"
"math/rand"
"strings"
"time"
)
func replaceRandomWord(text string, replacements []string) string {
words := strings.Split(text, " ")
if len(words) == 0 || len(replacements) == 0 {
return text // nothing to do
}
// Seed the random generator
rand.Seed(time.Now().UnixNano())
// Pick random index in the sentence
idx := rand.Intn(len(words))
// Pick random replacement word
newWord := replacements[rand.Intn(len(replacements))]
// Replace it
words[idx] = newWord
// Rebuild the string
return strings.Join(words, " ")
}
func main() {
text := "The quick brown fox jumps over the lazy dog"
replacements := []string{"c#", "c++", "java", "rust", "python"}
result := replaceRandomWord(text, replacements)
fmt.Println(result)
}
/*
run:
The quick brown fox jumps python the lazy dog
*/