Is there an easy way to randomize a list in VB.NET?

后端 未结 8 1376
无人及你
无人及你 2020-12-02 00:35

I have a list of type System.IO.FileInfo, and I would like to randomize the list. I thought I remember seeing something like list.randomize() a li

相关标签:
8条回答
  • 2020-12-02 01:22

    If you have the number of elements then a pseudo-random method can be used whereby you choose the first element at random (e.g. using the inbuilt random number function) then add a prime and take the remainder after division by the number of values. e.g. for a list of 10 you could do i = (i + prime) % 10 to generated indices i from some starting value. As long as the prime is greater than the number of values in the list then you create a sequence which runs through all of the numbers 0...n where n is the number of values - 1, but in a pseudorandom order.

    0 讨论(0)
  • 2020-12-02 01:24

    You could create custom comparer that just returns a random number, then sort the list using this comparer. It could be horribly inefficient and cause an almost infinite loop, but might be worth a try.

    0 讨论(0)
提交回复
热议问题