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
+9.8k Golang : Check a web page existence with HEAD request example
+5.8k Golang : Build new URL for named or registered route with Gorilla webtoolkit example
+7.2k Golang : Get YouTube playlist
+30.1k Golang : How to redirect to new page with net/http?
+8.5k Golang : Combine slices but preserve order example
+12.2k Golang : Exit, terminating or aborting a program
+7.9k Prevent Write failed: Broken pipe problem during ssh session with screen command
+13.3k Golang : Activate web camera and broadcast out base64 encoded images
+40.7k Golang : How to count duplicate items in slice/array?
+7.6k Golang : Load DSA public key from file example
+4.5k Chrome : How to block socketloop.com links in Google SERP?
+26.8k Golang : Find files by name - cross platform example