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
+3.9k Golang : Get a list of crosses(instruments) available to trade from Oanda account
+4.2k Golang : Calculate half life decay example
+7.1k Golang : Auto-generate reply email with text/template package
+14.2k Golang : Delete certain files in a directory
+5k Golang : Experimenting with the Rejang script
+11.4k Golang : Exit, terminating or aborting a program
+16.2k Golang : Clone with pointer and modify value
+6.7k Golang : Grayscale Image
+5.1k Golang : Extract XML attribute data with attr field tag example
+12k Golang : How to get a user home directory path?
+7k Golang : Append and add item in slice
+23.2k Golang : GORM read from database example