views:

118

answers:

1

I have to generate a list of items on a website which are random for the session of the user for that particular list of items.

I am going to add a link to demonstrate the problem. WebSite Link

Scenario: When a user comes in and clicks on the link, the items on the page should be randomized. As the user clicks on page two, three onwards, it should follow the same random pattern that it generated the first time so that when I go back to the first page the items on that page would be same as when the user first clicked on the link.

I did think of bringing out a dataset of all items randomized once and keeping them in session but that is a last resort.

+1  A: 

1) Your randomizer must be repeatable: by using a unique seed for each user and using the Random() class, you can generate the same sequence of random numbers across multiple HTTP requests. However, you must store the seed somewhere (I would suggest a cookie or hidden input element).

public Random GetGenerator() {
DateTime now = new DateTime();
long ticks = now.Ticks();

if(getCookie("ticks") > 0) {
// existing user:
ticks = getCookie("ticks"); // you must implement this to get the user's seed
} else {
// new user:
setCookie(now.Ticks()); // you must implement this to set a Cookie/input field value
}

return new Random(ticks);
}

2) You must generate M*(N-1) numbers to finally get to the random numbers for page N, where M is the number of items per page. Only then can you start generating random numbers for the requested page.

Jeff Meatball Yang
I tried this technique. I did the same thing as you have written. Now what happens is that it works fine on my machine. It generates same set of random numbers with a given seed. When I put this on a QA server, it returns different values for a given seed which it should technically not do. Any ideas?
Syed Sajid Nizami
Never mind that :) It works superb with LINQ.
Syed Sajid Nizami
Sorry, that doesn't sound right. Are you sure the seed is the same across requests?
Jeff Meatball Yang
Yes I kept the seed inside the ViewState and reflected it in URL as well.
Syed Sajid Nizami