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
+8.1k Golang : Number guessing game with user input verification example
+8.8k Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference
+9.4k Golang : Validate IPv6 example
+23.3k Golang : Check if element exist in map
+9.1k Golang : Terminate-stay-resident or daemonize your program?
+13.7k Golang : Compress and decompress file with compress/flate example
+29k Golang : JQuery AJAX post data to server and send data back to client example
+22.1k Golang : Read directory content with filepath.Walk()
+13.2k Android Studio : Password input and reveal password example
+14.9k JavaScript/JQuery : Detect or intercept enter key pressed example
+15k Golang : Get all local users and print out their home directory, description and group id