GoLang Dice Project

by Amir Sadeghian Posted on | Golang

The main goal of this project is to make you more familiar with random in Go programming language. In this project you are going to practice your knowledge in the following topics:

  • If else condition
  • Function definition, function parameter
  • Unix time
  • Random
  • Println

To generate a random number in Go, rand from math need to be imported. Then by calling rand.Intl(100) a number between 0 to 100 can be generated. The second call to rand.Intl will generate the same number.  To get a real random number every time the seed of rand needs to be set to a new number.

The best way to generate a unique number for rand seed is to use Unix timestamp.  time.Now().UnixNano() returns the Unix timestamp which is an unique number. Following code snippet generates a number between 0 and 50:

var seed int64 = time.Now().UnixNano()
rand.Seed(seed)
var randomNumber int = rand.Intn(50)

The full source code for the Dice project is available here: Golang Dice Project.