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
+7.2k JavaScript/JQuery : Detect or intercept enter key pressed example
+8.8k Golang : Calculations using complex numbers example
+12.7k Golang : Convert slice to array
+4.8k Golang : Get all countries phone codes
+8.1k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?
+4k CloudFlare : Another way to get visitor's real IP address
+8.5k Golang : Listen and Serve on sub domain example
+12k Golang : How to tell if a file is compressed either gzip or zip ?
+34.7k Golang : Disable security check for HTTPS(SSL) with bad or expired certificate
+4.2k Golang : Calculate BMI and risk category
+2.8k Golang : A program that contain another program and executes it during run-time
+4.6k Golang : How to stop user from directly running an executable file?