Golang generate random boolean [Simple Examples]

In this tutorial, we will walk through some examples of generating random boolean in Golang

 

Example 1: Simple rand.Intn() solution to generate random boolean

func (r *Rand) Intn(n int) int: Intn returns, as an int, a non-negative pseudo-random number in the half-open interval [0,n). It panics if n <= 0.

Advertisement

In this example, we will write a function using rand.Intn() to random generate boolean. The ideal is: we random an int (which can be 0 or 1), if the result is then return false, otherwise return true. Consider the code show below:

package main

import (
    "math/rand"
    "time"
	"fmt"
)

func main() {
	// try to generate random boolean 20 times
	for i := 1; i <= 20; i++ {
		testBoo := randomBool()
		fmt.Println("Random the", i, "time, result is: ", testBoo)
	}
}

// returns a random boolean value based on the current time
func randomBool() bool {
    rand.Seed(time.Now().UnixNano())
    return rand.Intn(2) == 1
}

Output:

Random the 1 time, result is:  false
Random the 2 time, result is:  false
Random the 3 time, result is:  false
Random the 4 time, result is:  false
Random the 5 time, result is:  true
Random the 6 time, result is:  true
Random the 7 time, result is:  true
Random the 8 time, result is:  true
Random the 9 time, result is:  true
Random the 10 time, result is:  false
Random the 11 time, result is:  false
Random the 12 time, result is:  false
Random the 13 time, result is:  false
Random the 14 time, result is:  true
Random the 15 time, result is:  true
Random the 16 time, result is:  true
Random the 17 time, result is:  true
Random the 18 time, result is:  true
Random the 19 time, result is:  true
Random the 20 time, result is:  true

Noted that: this is indicated in the math/Rand package documentation: If distinct behavior is desired for each run, use the Seed method to initialize the default Source. If we don't seed, our application will always return the same pseudo-random data.

 

Example 2: Same idea but some variant examples

This function returns true if the random integer is even else it returns false:

func randomBool() bool{
	rand.Seed(time.Now().UnixNano())
	return rand.Int() % 2 == 0 
}

In this code shown below, we random a float32, if the value is smaller than 0.5 then return true, otherwise return false:

// returns a random boolean value based on the current time
func randomBool() bool {
    rand.Seed(time.Now().UnixNano())
    return rand.Float32() < 0.5
}
package main

import (
	"fmt"
	"math/rand"
	"time"
	)

func main() {
	// try to generate random boolean 20 times
	for i := 1; i <= 20; i++ {
		testBoo := randomBool()
		fmt.Println("Random the", i, "time, result is: ", testBoo)
	}
}

func randomBool() bool {
	rand.Seed(time.Now().UnixNano())
    return rand.Float32() < 0.5
}

Output:

Advertisement
Random the 1 time, result is:  false
Random the 2 time, result is:  false
Random the 3 time, result is:  false
Random the 4 time, result is:  false
Random the 5 time, result is:  false
Random the 6 time, result is:  false
Random the 7 time, result is:  false
Random the 8 time, result is:  false
Random the 9 time, result is:  false
Random the 10 time, result is:  false
Random the 11 time, result is:  false
Random the 12 time, result is:  true
Random the 13 time, result is:  true
Random the 14 time, result is:  true
Random the 15 time, result is:  true
Random the 16 time, result is:  true
Random the 17 time, result is:  false
Random the 18 time, result is:  false
Random the 19 time, result is:  false
Random the 20 time, result is:  false

 

Summary

A few examples of creating a random boolean have been covered. The general ideal is to generate a single random number between 0 and 2, and to return true if the value is 1 and false otherwise. The article also introduces a few modifications on the previously mentioned strategy.

 

References

https://pkg.go.dev/math/rand
generate a random bool in go

 

Categories GO

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X