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.5k Golang : List available AWS regions
+29.9k Golang : How to verify uploaded file is image or allowed file types
+39.7k Golang : Convert to io.ReadSeeker type
+9.9k Golang : Use regular expression to get all upper case or lower case characters example
+11.2k Golang : Generate DSA private, public key and PEM files example
+5.8k PageSpeed : Clear or flush cache on web server
+20.9k Golang : Create and resolve(read) symbolic links
+7.3k Golang : Set horizontal, vertical scroll bars policies and disable interaction on Qt image
+28.4k Golang : Detect (OS) Operating System
+14.2k How to automatically restart your crashed Golang server
+13.8k Golang : Fix image: unknown format error