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
+16.4k Golang : Send email and SMTP configuration example
+9.2k nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
+22.1k Golang : Match strings by wildcard patterns with filepath.Match() function
+8.2k Golang : Get final or effective URL with Request.URL example
+4.9k JQuery : Calling a function inside Jquery(document) block
+10.2k Golang : Wait and sync.WaitGroup example
+5.6k Golang : Configure crontab to poll every two minutes 8am to 6pm Monday to Friday
+9.3k Golang : Timeout example
+8k Golang : Get all countries phone codes
+19.3k Golang : Get host name or domain name from IP address
+13.5k Golang : How to get year, month and day?
+10.3k Golang : Convert file content to Hex