Is using Random and OrderBy a good shuffle algorithm?

前端 未结 12 1707
-上瘾入骨i
-上瘾入骨i 2020-11-21 11:41

I have read an article about various shuffle algorithms over at Coding Horror. I have seen that somewhere people have done this to shuffle a list:

var r = ne         


        
12条回答
  •  无人及你
    2020-11-21 12:27

    It's probablly ok for most purposes, and almost always it generates a truly random distribution (except when Random.Next() produces two identical random integers).

    It works by assigning each element of the series a random integer, then ordering the sequence by these integers.

    It's totally acceptable for 99.9% of the applications (unless you absolutely need to handle the edge case above). Also, skeet's objection to its runtime is valid, so if you're shuffling a long list you might not want to use it.

提交回复
热议问题