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
+17.9k Golang : Get path name to current directory or folder
+7.1k Golang : How to stop user from directly running an executable file?
+27.9k Golang : Change a file last modified date and time
+22.6k Golang : Get ASCII code from a key press(cross-platform) example
+7.6k Golang : Grayscale Image
+10.1k Golang : Generate random integer or float number
+21.4k Golang : How to reverse slice or array elements order
+5.6k Facebook : How to force facebook to scrape latest URL link data?
+20.3k PHP : Convert(cast) int to double/float
+39.5k Golang : UDP client server read write example