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.5k Java : Random alphabets, alpha-numeric or numbers only string generator
+5.2k Golang : If else example and common mistake
+4.8k Golang : Constant and variable names in native language
+11.2k Golang : Generate DSA private, public key and PEM files example
+5.2k Python : Delay with time.sleep() function example
+7.9k Golang : Tell color name with OpenCV example
+14.7k Golang : package is not in GOROOT during compilation
+12.4k Golang : Drop cookie to visitor's browser and http.SetCookie() example
+23.9k Golang : How to validate URL the right way
+11.8k Golang : Perform sanity checks on filename example
+26.8k Golang : Find files by name - cross platform example
+10.2k Golang : Meaning of omitempty in struct's field tag