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
+5.3k PHP : Fix Call to undefined function curl_init() error
+11k Golang : Calculate Relative Strength Index(RSI) example
+9.5k Golang : Copy map(hash table) example
+35.7k Golang : Save image to PNG, JPEG or GIF format.
+10.8k Golang : Fix go.exe is not compatible with the version of Windows you're running
+5.1k Golang : Get FX sentiment from website example
+10.4k Golang : Bubble sort example
+10k Golang : Text file editor (accept input from screen and save to file)
+11k Golang : How to pipe input data to executing child process?
+31.3k Golang : How to convert(cast) string to IP address?
+29.2k Golang : Login(Authenticate) with Facebook example
+14.3k Golang : Rename directory