Skip to content

Instantly share code, notes, and snippets.

@advincze
Created Jun 7, 2016
Embed
What would you like to do?
raffle.go
package main
import "fmt"
import "math/rand"
import "time"
func init() {
rand.Seed(time.Now().Unix())
}
func main() {
dudes := []string{"alice", "bob", "charlie", "david", "eric", "fred"}
prizes := []string{"hat", "hat", "tshirt", "tshirt"}
winners := raffle(len(dudes), len(prizes))
for i, winner := range winners {
fmt.Printf("%s won a %s\n", dudes[winner], prizes[i])
}
}
func raffle(ld int, lp int) []int {
var wins, winners = make([]int, lp), make([]int, ld)
for i := 1; i < len(winners); i++ {
winners[i] = i
}
var k int
for i := 0; i < lp; i++ {
k = i % ld
if k == 0 {
//reshuffle
for i := 1; i < len(winners); i++ {
j := rand.Intn(i + 1)
winners[i], winners[j] = winners[j], winners[i]
}
}
wins[i] = winners[k]
}
return wins
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment