Golang : Flip coin example
This is a simple program to simulate flip coin that I use to train a small artificial intelligence program. Basically what it does is to randomly pick an item from a two elements slice.
Here you go!
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
coin := []string{
"heads",
"tails",
}
rand.Seed(time.Now().UnixNano())
// flip the coin
side := coin[rand.Intn(len(coin))]
fmt.Println("Flipped the coin and you get : ", side)
}
Output:
$./flip
Flipped the coin and you get : heads
$ ./flip
Flipped the coin and you get : heads
$ ./flip
Flipped the coin and you get : tails
References:
https://socketloop.com/tutorials/golang-randomly-pick-an-item-from-a-slice-array-example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+42.6k Golang : Use wildcard patterns with filepath.Glob() example
+5.6k Golang : Skip or discard items of non-interest when iterating example
+5.2k Golang : Experimental emojis or emoticons icons programming language
+19.2k Golang : Saving private and public key to files
+5k Java : Human readable password generator
+8.7k Golang : Check if user agent is a robot or crawler example
+4.1k Golang : micron to centimeter example
+14.9k Golang : Get file permission
+3.9k PHP : Extract part of a string starting from the middle
+5.4k Golang : Convert an executable file into []byte example
+19.7k Curl usage examples with Golang
+21.1k Golang : Read a file into an array or slice example