0

I have a spring based server which has a request method which when invoked by an user should return one out of number of entries in the database at random. But they should be non repeated for a user in a session (like non repeating questions in a quiz.) Each entry is identified by a unique entry number. (from 1 to n) I have managed to have sessions by having the same HttpContext for a user across requests.

I was thinking of doing something like Java generating non-repeating random numbers by having an array field in the controller which is shuffled during every call of the method and the first array element to be used as the entry number for the entry to be fetched from the db. However, this may have implications if multiple users are connected to the controller at the same time. I was wondering of what is a good way to design this. I can share the code of my controller if required.

Community
  • 1
  • 1
doomguy
  • 401
  • 10
  • 26

2 Answers2

0

I tried this by simply playing with the system date and time. If you take the System date and time then it will be unique for each moment so you can use this trick to generate unique random number through the whole system. Best luck!

Laxminarayan
  • 188
  • 2
  • 16
0

If you have sessions, why don't use it to store your array field. That solves your problem about multiple users.

nboncoure
  • 56
  • 5
  • Am not sure how to have a per session field. Since the Controller class is singleton, any member field I have there will be common to all sessions. – doomguy Feb 03 '15 at 06:40
  • In your spring controller you can get your current session lby adding 'HttpSession session' in your method's arguments – nboncoure Feb 03 '15 at 07:10
  • Thanks. Got this working using the shuffled array as a session attribute variable. – doomguy Feb 04 '15 at 16:33