I was trying to write some code that will generate seven sets of seven unique, non-repeated numbers ranging from 1 to 49. However, when I ran the code, it goes into an infinite loop of generating a single number endlessly. This only occurred when I included the little if-else loop under my main() whose functionality is to check for duplicate numbers.
Is there an issue with the logic behind the duplicate check?
package main
import "fmt"
import "math/rand"
import "time"
func main() {
var j [7]int
var n []int
rand.Seed(time.Now().UTC().UnixNano())
for m := 0; m < 7; m++ {
for i := 0; i < 8; i++ {
if i < 7 {
var duplicate int = randInt(1, 49)
n = append(n, duplicate)
if i != 0 {
if !integerinarray(duplicate, n) {
j[i] = duplicate
} else {
i--
}
} else {
j[i] = duplicate
}
fmt.Print(j[i], " ")
} else {
fmt.Println("
")
//fmt.Println(n)
}
}
}
}
func randInt(min int, max int) int {
return min + rand.Intn(max-min)
}
func integerinarray(a int, s []int) bool {
for _, b := range s {
if b == a {
return true
}
}
return false
}