LoginSignup
3
2

More than 1 year has passed since last update.

Go lang で1桁のランダムな数値を生成する / rand.Intn(9) / #go

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

package main

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

func main() {
	rand.Seed(time.Now().UnixNano())

	for i := 0; i < 100; i++ {
		fmt.Println(rand.Intn(9))
                // e.g exclude zero
                // fmt.Println(1 + rand.Intn(9-1))
	}

}

// Result example
//
// 2
// 7
// 1
// 0
// 2
// 8
// 3
// 1
// 2
// 4
// 3
// 3
// 3
// 4
// 4
// 7
// 3
// 2
// 6
// 7
// 2
// 4
// 1
// 4
// 4
// 2
// 1
// 2
// 1
// 7
// 1
// 0
// 8
// 1
// 4
// 3
// 5
// 7
// 0
// 7
// 6
// 3
// 8
// 4
// 8
// 4
// 1
// 8
// 2
// 0
// 2
// 4
// 1
// 6
// 7
// 7
// 1
// 1
// 2
// 3
// 5
// 5
// 6
// 5
// 2
// 4
// 5
// 6
// 2
// 4
// 6
// 8
// 2
// 7
// 1
// 3
// 8
// 8
// 6
// 3
// 8
// 0
// 0
// 3
// 1
// 8
// 6
// 1
// 5
// 5
// 1
// 5
// 5
// 1
// 5
// 5
// 2
// 1
// 3
// 7

rand.Intn() に上限の値を与えれば良いみたいだ。

Original by Github issue

チャットメンバー募集

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

Twitter

3
2
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
2