# Tutorial "Return a random greeting"

**URL:** <https://forum.golangbridge.org/t/tutorial-return-a-random-greeting/36873>\
**Category:** Code Review\
**Created:** [November 14, 2024, 8:46pm UTC](https://forum.golangbridge.org/t/tutorial-return-a-random-greeting/36873 "2024-11-14T20:46:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![goatcabin](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@goatcabin](https://forum.golangbridge.org/u/goatcabin)\
**Post date:** [November 14, 2024, 8:46pm UTC](https://forum.golangbridge.org/t/tutorial-return-a-random-greeting/36873/1 "2024-11-14T20:46:44Z")

</div>

I am doing the Tutorial at go.dev/doc/tutorial/random-greeting.  
I copied the code from the tutorial for hello.go and greetings.go. It runs, but on my machine (Ubunto 24.04) it always returns the same greeting. no matter how many times I run it, which means the rand function is always returning the same index.

```auto
func randomFormat() string {
    // A slice of message formats.
    formats := []string{
        "Hi, %v. Welcome!",
        "Great to see you, %v!",
        "Hail, %v! Well met!",
    }

    // Return a randomly selected message format by specifying
    // a random index for the slice of formats.
    return formats[rand.Intn(len(formats))]
}

```

---

<div class="post-metadata">

**Author:** ![Dean\_Davidson](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/dean_davidson/32/9685_2.png) [@Dean\_Davidson](https://forum.golangbridge.org/u/Dean_Davidson)\
**Post date:** [November 14, 2024, 9:11pm UTC](https://forum.golangbridge.org/t/tutorial-return-a-random-greeting/36873/2 "2024-11-14T21:11:07Z")

</div>

It’s probably a fluke. Also `rand.Intn` is pseudorandom not random. But running this:

```go
func main() {
	for i := 0; i < 5; i++ {
		fmt.Println(randomFormat())
	}
}

func randomFormat() string {
	// A slice of message formats.
	formats := []string{
		"Hi, %v. Welcome!",
		"Great to see you, %v!",
		"Hail, %v! Well met!",
	}
	// Return a randomly selected message format by specifying
	// a random index for the slice of formats.
	return formats[rand.Intn(len(formats))]
}

```

… produces this on the playground:

```auto
Hail, %v! Well met!
Hi, %v. Welcome!
Hi, %v. Welcome!
Hi, %v. Welcome!
Great to see you, %v!

```

Run it yourself:

> **[Go Playground - The Go Programming Language](https://go.dev/play/p/Lsp9SrDbVeW)**

---

<div class="post-metadata">

**Author:** ![goatcabin](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@goatcabin](https://forum.golangbridge.org/u/goatcabin)\
**Post date:** [November 14, 2024, 9:38pm UTC](https://forum.golangbridge.org/t/tutorial-return-a-random-greeting/36873/3 "2024-11-14T21:38:16Z")

</div>

> [@Dean\_Davidson](#):
>
> ```auto
> func randomFormat() string {
> // A slice of message formats.
> formats := []string{
> "Hi, %v. Welcome!",
> "Great to see you, %v!",
> "Hail, %v! Well met!",
> }
> // Return a randomly selected message format by specifying
> // a random index for the slice of formats.
> return formats[rand.Intn(len(formats))]
> }
> 
> ```

thanks, Dean. It did the same on my machine.
