@@ -179,27 +179,29 @@ func createMultipleChoiceQuestionFromAnswers(question, correctAnswer string, all
179179func getRandomDistractors (correctAnswer string , allAnswers []string , seed int ) []string {
180180 r := rand .New (rand .NewSource (time .Now ().UnixNano () + int64 (seed * 13 )))
181181
182- var distractors []string
183182 var otherAnswers []string
183+ unique := make (map [string ]struct {})
184184
185185 for _ , answer := range allAnswers {
186186 if answer != correctAnswer {
187- otherAnswers = append (otherAnswers , answer )
187+ if _ , exists := unique [answer ]; ! exists {
188+ unique [answer ] = struct {}{}
189+ otherAnswers = append (otherAnswers , answer )
190+ }
188191 }
189192 }
190193
191- if len (otherAnswers ) > 0 {
192- r .Shuffle (len (otherAnswers ), func (i , j int ) {
193- otherAnswers [i ], otherAnswers [j ] = otherAnswers [j ], otherAnswers [i ]
194- })
194+ if len (otherAnswers ) == 0 {
195+ return []string {}
196+ }
195197
196- maxDistractors := len (otherAnswers )
197- if maxDistractors > 3 {
198- maxDistractors = 3
199- }
198+ r .Shuffle (len (otherAnswers ), func (i , j int ) {
199+ otherAnswers [i ], otherAnswers [j ] = otherAnswers [j ], otherAnswers [i ]
200+ })
200201
201- distractors = otherAnswers [:maxDistractors ]
202+ if len (otherAnswers ) > 3 {
203+ otherAnswers = otherAnswers [:3 ]
202204 }
203205
204- return distractors
206+ return otherAnswers
205207}
0 commit comments