1

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:

  • Randomize List
  • Take the results for a given page from the List
  • Get the next page of the same randomized list as necessary.

Is this even possible, or do I need to have a logically maintained order to successfully implement custom paging?

Clem
  • 83
  • 3

4 Answers4

2

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;
Guvante
  • 18,775
  • 1
  • 33
  • 64
  • 1
    Hmmmm, yes, that's simpler than my suggestion. My suggestion is only better if T is a value type and the T's are modified while the application is running. Well, my suggestion has the word 'permutation' in it, so it's better in *that* aspect :-) – zmbq Feb 27 '12 at 22:21
  • I think the odds for that in the OP's case are very low. – zmbq Feb 27 '12 at 22:31
2

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.

Vincent Hubert
  • 1,386
  • 9
  • 23
1

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.

zmbq
  • 38,013
  • 14
  • 101
  • 171
1

The easiest way would be to :

  • Randomize your whole dataset.
  • Then, partition it in pages, you can see here a way to partition a list into a list of smaller list.

You need a way to get rid of your list of list when not needed anymore.

Community
  • 1
  • 1
Vincent Hubert
  • 1,386
  • 9
  • 23