Commit dab4af4

Richard Luby <richluby@gmail.com>
2017-01-26 12:55:41
added shuffle array function
function uses Fisher-Yates algorithm for array shuffling
1 parent e93fa1e
Changed files (1)
serverHandlers.go
@@ -19,6 +19,22 @@ import (
 var userTests map[string][]ClientTest
 var userTestsLock = sync.RWMutex{}
 
+// randomizeArray takes an array and returns the shuffled version
+// following the Fisher-Yates shuffle
+func randomizeArray(array []Record) []Record {
+	var tempRecord Record
+	maxIndex := len(array)
+	currentIndex := maxIndex
+	for currentIndex != 0 {
+		randomIndex := rand.Intn(currentIndex)
+		currentIndex -= 1
+		tempRecord = array[currentIndex]
+		array[currentIndex] = array[randomIndex]
+		array[randomIndex] = tempRecord
+	}
+	return array
+}
+
 // buildRecordArray builds an array of Records based on the parameter criteria
 func buildRecordArray(numQuestions int, selectedCategories []string) ([]Record, error) {
 	recordArray := []Record{}