3
1

More than 1 year has passed since last update.

Go 言語で配列(Slice)からランダムに文字列を選ぶ ( #go random select string in slice )

Last updated at Posted at 2020-03-16
// https://golang.org/pkg/math/rand/

package main

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

func main() {
	cards := []string{"A", "B", "C"}

	rand.Seed(time.Now().UnixNano())

	for i := 0; i < 10; i++ {
		num := rand.Intn(len(cards))
		fmt.Println(cards[num])
	}
}

// result

// e.g

// C
// C
// C
// C
// C
// B
// B
// C
// B
// C

Original by Github issue

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1