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
+11.6k Golang : How to detect a server/machine network interface capabilities?
+9.5k Golang : Validate IPv6 example
+6.5k Golang : Convert an executable file into []byte example
+6k Golang : Build new URL for named or registered route with Gorilla webtoolkit example
+10.2k Golang : Embed secret text string into binary(executable) file
+23.5k Golang : minus time with Time.Add() or Time.AddDate() functions to calculate past date
+13.8k Golang : convert(cast) string to float value
+6k Golang : Missing Subversion command
+5k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version
+44.7k Golang : Use wildcard patterns with filepath.Glob() example
+8.8k Golang : Find network service name from given port and protocol
+38k Golang : Read a text file and replace certain words