sajidnizami
sajidnizami

Reputation: 477

Paging on a Random Items Data Set

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.

Upvotes: 2

Views: 395

Answers (1)

Jeff Meatball Yang
Jeff Meatball Yang

Reputation: 39057

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.

Upvotes: 1

Related Questions