I have an project using Hibernate.
This has a class named Question.
Each Question has a difficulty level (1,2,3);
Each Question also has an attribute is field (a,b,c,d,e,f,g,h,i,k);
Suppose I have 100 questions. I want to get randomly 20 question with conditions:
- 7 questions in level 1
- 7 questions in level 2
- 6 questions in level 3.
- Each field have at least 1 question
Thanks to all ^^
First of all, I think this problem is too difficult to be solved by using a Hibernate query, or even a set of Hibernate queries.
Here’s how I would do:
Map<Field, List<Question>>, and shuffle all the lists in this mapThis should work if, for each field, you’re guaranteed to have at least a question for every level. If it’s not the case, then it’s more difficult.
If you have much more questions that 100, and they can’t be loaded in memory, you could use the same kind of algorithm, but use random queries to select the questions: