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.7k Golang : Detect Pascal, Kebab, Screaming Snake and Camel cases
+7k Golang : Muxing with Martini example
+9.7k Golang : Apply Histogram Equalization to color images
+24k Find and replace a character in a string in Go
+4.9k Unix/Linux : How to pipe/save output of a command to file?
+6k Golang : Launching your executable inside a console under Linux
+7.1k Golang : Levenshtein distance example
+4.9k Linux/MacOSX : How to symlink a file?
+6.2k Javascript : Get operating system and browser information
+17.2k Golang : Set up source IP address before making HTTP request
+15.9k Golang : Get checkbox or extract multipart form data value example
+12.6k Elastic Search : Return all records (higher than default 10)