Best way to randomize an array with .NET

后端 未结 17 1261
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 01:55

What is the best way to randomize an array of strings with .NET? My array contains about 500 strings and I\'d like to create a new Array with the same strings b

17条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-22 02:31

    Randomizing the array is intensive as you have to shift around a bunch of strings. Why not just randomly read from the array? In the worst case you could even create a wrapper class with a getNextString(). If you really do need to create a random array then you could do something like

    for i = 0 -> i= array.length * 5
       swap two strings in random places
    

    The *5 is arbitrary.

提交回复
热议问题