Created
June 7, 2016 03:25
-
-
Save advincze/4a36ca04e3187610381c5a6f05a3bb63 to your computer and use it in GitHub Desktop.
raffle.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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