Reputation: 83
I have a ListView bound to a List. The listview implements custom paging where only the number of results on the page are returned, which works great when the results are in alphabetical order. However, I would like to try and return the results in a random order - the scenario is a sales office with team members displaying on the listview.
I'm looking to find an algorithm that will allow the custom paging but maintain the randomization over the results. For example, in alphabetical order, it's simple - .Take(Page# * ResultsOnPage). However, if it's randomized each time the page is loaded, the scenario where individuals could be shown on multiple pages, and some not shown at all.
So the goal is:
Is this even possible, or do I need to have a logically maintained order to successfully implement custom paging?
Upvotes: 1
Views: 749
Reputation: 1396
Actually, there is no such thing as "Random" with computer.
The Random function will return the same series of values if you seed it with the same seed value. That is the reason why random number often seed on current time. If you save the seed value into the view state, you should be able to regenerate the same list over and over again. If you want to generate a new random order, just re-generate the seed.
Upvotes: 2
Reputation: 19223
Expanding on zmbq's answer, you don't need to create a class, you can simply fill a new list with the randomized values. The following is from this post.
Random rnd = new Random();
var randomizedList = from item in list
orderby rnd.Next()
select item;
Upvotes: 2
Reputation: 1396
The easiest way would be to :
You need a way to get rid of your list of list when not needed anymore.
Upvotes: 1
Reputation: 39029
It's obviously possible, the question is - what is the best way of doing it.
I think you should randomize your list, and let everything else work as usual. If that's not possible, you should put a Randomizer (my term, don't look it up...) in front of the list. Something like this:
class Randomizer<T> : IList<T>
{
private IList<T> _underlyingList;
private List<int> _permutation;
public Randomizer(IList<t> underlying)
{
_underlyingList = underlying;
_permutation = GenerateRandomPermutation(_underlyingList.Count);
}
// Implement the IList interface, for example
public T this[int index] { get { return _underlyingList[_permutation[value]]; } }
}
You might want to implement ICollection instead, because it can save you some effort.
Anyway, connect your ListView to the Randomizer instead of the List, and let everything else remain the same.
Upvotes: 1