13fc5dac00fac654d79dd3d45d0291653b4b954e

Author
TheEdgeOfRage <git@theedgeofrage.com>
Committer
TheEdgeOfRage <git@theedgeofrage.com>
Date

Message

Revert random shuffle to uniform

Diff

 1diff --git a/ui.go b/ui.go
 2index 12e10454e3a114381258c50ac620e73e12eef1d7..413189a94c9a6022be68af4ec80659ac4f834731 100644
 3--- a/ui.go
 4+++ b/ui.go
 5@@ -30,10 +30,10 @@ type audioMsg struct {
 6 }
 7 
 8 type model struct {
 9-	catalog     []Song
10-	order       []int
11-	pos         int
12-	rounds      int
13+	catalog      []Song
14+	order        []int
15+	pos          int
16+	rounds       int
17 	played       int
18 	score        int
19 	resultParts  []string
20@@ -65,7 +65,7 @@ func newModel(catalog []Song, rounds int) model {
21 		catalog:  catalog,
22 		rounds:   rounds,
23 		inputs:   inputs,
24-		order:    balancedOrder(catalog),
25+		order:    shuffled(len(catalog)),
26 		st:       statePlaying,
27 		playing:  true,
28 		active:   0,
29@@ -207,7 +207,7 @@ func (m *model) moveField(dir int) {
30 func (m *model) nextSong() Song {
31 	if m.pos >= len(m.order) {
32 		m.pos = 0
33-		m.order = balancedOrder(m.catalog)
34+		m.order = shuffled(len(m.catalog))
35 	}
36 	idx := m.order[m.pos]
37 	m.pos++
38@@ -340,36 +340,13 @@ func pointWord(n int) string {
39 	return "points"
40 }
41 
42-// balancedOrder builds a play order that gives each artist roughly equal
43-// representation, regardless of how many songs they have in the catalog.
44-// It round-robins across artists: each pass takes one (shuffled) song from
45-// every artist until all songs are used. Small artists are fully represented;
46-// any surplus tracks from large artists fill the remaining slots.
47-func balancedOrder(catalog []Song) []int {
48-	byArtist := make(map[string][]int)
49-	var artists []string
50-	for i, s := range catalog {
51-		a := s.Artist
52-		if _, ok := byArtist[a]; !ok {
53-			artists = append(artists, a)
54-		}
55-		byArtist[a] = append(byArtist[a], i)
56-	}
57-	rand.Shuffle(len(artists), func(i, j int) { artists[i], artists[j] = artists[j], artists[i] })
58-	for _, a := range artists {
59-		songs := byArtist[a]
60-		rand.Shuffle(len(songs), func(i, j int) { songs[i], songs[j] = songs[j], songs[i] })
61-	}
62-
63-	order := make([]int, 0, len(catalog))
64-	next := make(map[string]int, len(artists))
65-	for len(order) < len(catalog) {
66-		for _, a := range artists {
67-			if next[a] < len(byArtist[a]) {
68-				order = append(order, byArtist[a][next[a]])
69-				next[a]++
70-			}
71-		}
72+func shuffled(n int) []int {
73+	idx := make([]int, n)
74+	for i := range idx {
75+		idx[i] = i
76 	}
77-	return order
78+	rand.Shuffle(n, func(i, j int) {
79+		idx[i], idx[j] = idx[j], idx[i]
80+	})
81+	return idx
82 }