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.4k Golang : Implementing class(object-oriented programming style)
+3.5k Golang : Fix go-cron set time not working issue
+24.6k Golang : GORM read from database example
+14.7k Golang : Convert(cast) int to float example
+17.5k Golang : Multi threading or run two processes or more example
+13.5k Golang : Increment string example
+12.9k Swift : Convert (cast) Int or int32 value to CGFloat
+11.3k Golang : Calculate Relative Strength Index(RSI) example
+4.4k Javascript : How to show different content with noscript?
+27.3k Golang : Find files by name - cross platform example
+9.3k nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)