How to fill an array with 1 and 0 in random locations with Go

1 Answer

0 votes
package main

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

func fillArrayWithRandom1And0(array []int) {
    rand.Seed(time.Now().UnixNano())
    
    for i := range array {
        array[i] = rand.Intn(2) // Generates either 0 or 1
    }
}

func main() {
    size := 10
    array := make([]int, size)
    
    fillArrayWithRandom1And0(array)
    
    fmt.Println(array)
}



/*
run:

[0 0 0 0 1 0 1 0 0 1]

*/

 



answered Jan 26, 2025 by avibootz

Related questions

...