github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/util/shuffle/shuffle.go (about) 1 // Copyright 2016 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package shuffle 12 13 import "math/rand" 14 15 // Interface for shuffle. When it is satisfied, a collection can be shuffled by 16 // the routines in this package. The methods require that the elements of the 17 // collection be enumerable by an integer index. This interface is similar to 18 // sort.Interface. 19 type Interface interface { 20 // Len is the number of elements in the collection. 21 Len() int 22 // Swap swaps the elements with indexes i and j. 23 Swap(i, j int) 24 } 25 26 // Shuffle randomizes the order of the array. 27 func Shuffle(data Interface) { 28 n := data.Len() 29 for i := 1; i < n; i++ { 30 data.Swap(i, rand.Intn(i+1)) 31 } 32 }