go - Want to choose a random number and not have it picked again until all numbers have gone through -
i'm looking pick random number 1-6 if example 1 picked program want make sure not again used until 2-6 has been picked. want in order have chance go through options instead of having same option 2 or maybe 3 times in row since there 6 options. ideas please?
//choose random number recipe rand.seed(time.now().utc().unixnano()) myrand := random(1, 6) fmt.println(myrand)
...
function processes it
//random number function func random(min, max int) int { return rand.intn(max-min) + min
}
the part of program uses myrand each run used in if statement , tied recipe picked day
//choose random number recipe rand.seed(time.now().utc().unixnano()) myrand := random(1, 6) fmt.println(myrand) //test below of random replacement list := rand.perm(6) i, _ := range list { fmt.printf("%d\n", list[i]+1) } //logic recipe choose if myrand == 1 { fmt.println(1) printrecipeoftheday(recipe1) } else if myrand == 2 { fmt.println(2) printrecipeoftheday(recipe2) } else if myrand == 3 { fmt.println(3) printrecipeoftheday(recipe3) } else if myrand == 4 { fmt.println(4) }
}
i'm wondering if i'm doing incorrectly ? i'm not sure how can use list on every run generate new number (myrand) , program able remember last number (myrand) when ran previous time?
you can use rand.perm(6)
, loop through result.
for example, here code print numbers 1-6 in random order, no repeats:
package main import "fmt" import "math/rand" func main(){ list := rand.perm(6); i, _ := range list { fmt.printf("%d\n", list[i]+1); } }
note we've added 1 everything, since perm(6)
returns 0-5 , want 1-6. after you've used six, call rand.perm(6)
again 6 new numbers.
Comments
Post a Comment