views:

139

answers:

2

Hi all, I'm developing a web application using asp.net Mvc 2 and NHibernate, and I'm paging data (products in a category) in my page, but this data are random, so, I'm using a HQL statement link this:

string hql = "from Product p where p.Category.Id=:IdCategory order by rand()";

It's working fine, but when I page, sometimes the same product appears in the first, second, etc... pages because it's order by rand().

Is there any way to make a random order by fixed by period (time internal) ? Or any solution ?

thanks

Cheers

+1  A: 

I don't see why you're deliberately randomising the data. If there's no particular order you want them in, why not just order by the primary key?

Jon Skeet
Hi Jon, it's a advertisement product page, and I'd like to randomize the records to the users... because if i didn't do that, the same products are on the firts page always!
Felipe
In that case you need to provide some stable ordering based on something which stays constant for each user. Do you have a cookie of any kind there?
Jon Skeet
Well, I dont have any cookie or session. But I can rand it and put on Session!
Felipe
+1  A: 

Seed the random number generator:

order by rand(123)

I would suggest using a session-scoped random number as your seed. That way, the page doesn't suddenly re-sort for a single user, but it's still be sorted differently for every user.

Dolph
can I use a SessionId ? Or use Random object to get a number and put on Session and use it! well.. i'll try this! Thanks
Felipe
You can use the session ID but you need to convert it to a number first (if it's not already): `Integer.parseInt(sessionId, radix)`.
Dolph
thanks man, i think that I'll to cache a number by time (1 or 5 minutes) and use it... thats great! Cheers
Felipe