• Josh Bleecher Snyder's avatar
    math/rand: add Shuffle · a2dfe5d2
    Josh Bleecher Snyder authored
    Shuffle uses the Fisher-Yates algorithm.
    
    Since this is new API, it affords us the opportunity
    to use a much faster Int31n implementation that mostly avoids division.
    As a result, BenchmarkPerm30ViaShuffle is
    about 30% faster than BenchmarkPerm30,
    despite requiring a separate initialization loop
    and using function calls to swap elements.
    
    Fixes #20480
    Updates #16213
    Updates #21211
    
    Change-Id: Ib8956c4bebed9d84f193eb98282ec16ee7c2b2d5
    Reviewed-on: https://go-review.googlesource.com/51891
    Run-TryBot: Ian Lance Taylor <iant@golang.org>
    TryBot-Result: Gobot Gobot <gobot@golang.org>
    Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
    a2dfe5d2
example_test.go 4.3 KB