Introduction to Go Programming

Errata for Introduction to Go Programming

Submit your own errata for this product.


The errata list is a list of errors and their corrections that were found after the product was released. If the error was corrected in a later version or reprint the date of the correction will be displayed in the column titled "Date Corrected".

The following errata were submitted by our customers and approved as valid errors by the author or editor.

Color Key: Serious Technical Mistake Minor Technical Mistake Language or formatting error Typo Question Note Update



Version Location Description Submitted By Date Submitted Date Corrected
Other Digital Version
video chapter 25

func WeightedShuffle(w WeightedShufleable) { total := 0 for i := 0; i < w.Len(); i++ { total += w.Weight(i) } for i := 0; i < w.Len(); i++ { pos := rand.Intn(total) cum := 0 for j := i; j < w.Len(); j++ { counter += 1 cum += w.Weight(j) if pos >= cum { total -= w.Weight(j) w.Swap(i, j) break } } } } Should be: func WeightedShuffle(w WeightedShufleable) { total := 0 for i := 0; i < w.Len(); i++ { total += w.Weight(i) } for i := 0; i < w.Len(); i++ { pos := rand.Intn(total) cum := 0 for j := i; j < w.Len(); j++ { counter += 1 cum += w.Weight(j) if cum >= pos { total -= w.Weight(j) w.Swap(i, j) break } } } } So if cum >= pos not if pos >= cum

Note from the Author or Editor:
Interestingly the code in the repo. for this course is slightly different and uses the test if pos < cum { [...] } Consider a situation where there are two items with weights 100 and 10. total will be 110 which means that pos can be anywhere from 0 to 109. For i = 0 cum can take the values 100 and 110. I don't think the equality test is necessary because if pos is 0 to 99 then you'd expect the 100 bucket to be chosen, if pos is 100 you'd expect the 10 bucket. So, my vote is that the code that's in the repo be used. https://github.com/jgrahamc/goex/blob/master/sample/src/shuffler/shuffle.go

Anonymous  Apr 10, 2015