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
+15.2k Golang : Check if a string contains multiple sub-strings in []string?
+8k Golang : Generate EAN barcode
+32.8k Golang : convert(cast) bytes to string
+6.8k Golang : Grayscale Image
+9.9k Golang : Web routing/multiplex example
+10.9k Golang : Pagination with go-paginator configuration example
+3.6k Javascript : Detect when console is activated and do something about it
+19.6k Golang : Clean up null characters from input data
+20.5k Golang : Convert seconds to minutes and remainder seconds
+9.3k Golang : Convert file unix timestamp to UTC time example
+4.5k Golang : Generate Interleaved 2 inch by 5 inch barcode
+14k Golang : Find location by IP address and display with Google Map