How can I pick almost random questions from a list depending on which level you are on?
I'm working on a project and I want to pick up different questions depending on which level you are on.
For example, the first 25 levels I want my system to pick easy questions for example in a 80% chance and medium questions in a 20%. At the end on level 80 I want the difficulty to slowly increase and the system will 100% only pick hard questions. How can I write a working slowly increasing graph matematically to implement to my arraypicker?
I have tried to shuffle ArrayList
objects with:
Collections.shuffle(random);
for example in order to get percent, but I there should be an easier workaround this.
Hard to explain but I hope you understand, ask if you need more information.
I'm working on a libgdx project on Android studio
java android-studio libgdx
add a comment |
I'm working on a project and I want to pick up different questions depending on which level you are on.
For example, the first 25 levels I want my system to pick easy questions for example in a 80% chance and medium questions in a 20%. At the end on level 80 I want the difficulty to slowly increase and the system will 100% only pick hard questions. How can I write a working slowly increasing graph matematically to implement to my arraypicker?
I have tried to shuffle ArrayList
objects with:
Collections.shuffle(random);
for example in order to get percent, but I there should be an easier workaround this.
Hard to explain but I hope you understand, ask if you need more information.
I'm working on a libgdx project on Android studio
java android-studio libgdx
Can you add all questions in an arraylist sorted by difficulty, then get question asmyList.get(Math.random(level + whatever))
.
– LunaticJape
Nov 13 '18 at 14:10
add a comment |
I'm working on a project and I want to pick up different questions depending on which level you are on.
For example, the first 25 levels I want my system to pick easy questions for example in a 80% chance and medium questions in a 20%. At the end on level 80 I want the difficulty to slowly increase and the system will 100% only pick hard questions. How can I write a working slowly increasing graph matematically to implement to my arraypicker?
I have tried to shuffle ArrayList
objects with:
Collections.shuffle(random);
for example in order to get percent, but I there should be an easier workaround this.
Hard to explain but I hope you understand, ask if you need more information.
I'm working on a libgdx project on Android studio
java android-studio libgdx
I'm working on a project and I want to pick up different questions depending on which level you are on.
For example, the first 25 levels I want my system to pick easy questions for example in a 80% chance and medium questions in a 20%. At the end on level 80 I want the difficulty to slowly increase and the system will 100% only pick hard questions. How can I write a working slowly increasing graph matematically to implement to my arraypicker?
I have tried to shuffle ArrayList
objects with:
Collections.shuffle(random);
for example in order to get percent, but I there should be an easier workaround this.
Hard to explain but I hope you understand, ask if you need more information.
I'm working on a libgdx project on Android studio
java android-studio libgdx
java android-studio libgdx
edited Nov 17 '18 at 17:24
pizza static void main
1,7291924
1,7291924
asked Nov 13 '18 at 14:06
joelibgdxjoelibgdx
63
63
Can you add all questions in an arraylist sorted by difficulty, then get question asmyList.get(Math.random(level + whatever))
.
– LunaticJape
Nov 13 '18 at 14:10
add a comment |
Can you add all questions in an arraylist sorted by difficulty, then get question asmyList.get(Math.random(level + whatever))
.
– LunaticJape
Nov 13 '18 at 14:10
Can you add all questions in an arraylist sorted by difficulty, then get question as
myList.get(Math.random(level + whatever))
.– LunaticJape
Nov 13 '18 at 14:10
Can you add all questions in an arraylist sorted by difficulty, then get question as
myList.get(Math.random(level + whatever))
.– LunaticJape
Nov 13 '18 at 14:10
add a comment |
2 Answers
2
active
oldest
votes
You have several problems wrapped up in one question, making your question a little broad for this site, but let's break down your issues:
- Create a question class with varying levels of difficulty. Let's call this class
Question
and level of difficulty,Difficulty
- Create a class that holds these questions in a collection or collections of some sort, and allow the user to request a random question from this class. Let's call this class
QuestionCollection
and the method used to request a random question,public Question getRandomQuestion(...)
. - Allow the user to have their own level of advancement. This can be the
User
class - The distribution of the questions possibly received from the request
getRandomQuestion(...)
will depend on the User's level of advancement
So first to encapsulate level of difficulty, let's create an enum, one that has 3 (or more) levels:
public enum Difficulty
EASY, MEDIUM, HARD
Then the Question class can have a private Difficulty difficulty;
field, one set in its constructor, and with a public getter method, public Difficulty getDifficulty()
. A simplified Question class could look something like this:
public class Question
private String question;
private String answer;
private Difficulty difficulty;
public Question(String question, String answer, Difficulty difficulty)
this.question = question;
this.answer = answer;
this.difficulty = difficulty;
public String getQuestion()
return question;
public String getAnswer()
return answer;
public Difficulty getDifficulty()
return difficulty;
Again this is all an over simplification, but it can be used to help illustrate the problem and a possible solution. If desired, you could have this class implement Comparable<Question>
, and using Difficulty to help do the comparison, and allow you to sort a List<Question>
by difficulty.
Then the key to all of this would be the QuestionCollection
class, the one that held collection(s) of questions and that had the getRandomQuestion(...)
method -- how to implement this.
One way, is to rather than worry about User level of advancement at this point, give the getRandomQuestion(...)
method some parameters that allow the QuestionCollection
to know what distribution to use. Easiest in my mind is to give it a relative frequency of difficulty, a percentage of Difficulty.EASY
, Difficulty.MEDIUM
, and Difficulty.HARD
, something like:
public Question getRandomQuestion(int percentEasy, int percentMedium, int percentHard)
// ... code here to get the random question
OK, so now we're at how to build the internal workings of the QuestionCollection
class, and then use this to get a proper distribution of random questions based on the parameter percentages. Probably the simplest is put a question into its own List, such as an ArrayList based on its level of Difficulty -- so here 3 Lists, one for EASY, one for MEDIUM and one for HARD questions.
So:
private List<Question> easyQuestions = new ArrayList<>();
private List<Question> mediumQuestions = new ArrayList<>();
private List<Question> hardQuestions = new ArrayList<>();
Or another possibly cleaner solution is to use a Map<Difficulty, List<Question>>
rather than separate Lists, but I'll keep things simple for now and leave it at 3 Lists.
Then the class would have an public void addQuestion(Question q)
method that would add questions to the correct list based on difficulty level:
public void addQuestion(Question q)
switch (q.getDifficulty())
case EASY:
easyQuestions.add(q);
break;
case MEDIUM:
mediumQuestions.add(q);
break;
case HARD:
hardQuestions.add(q);
OK, so we've got our lists filled with questions, we're now at the core issue of how to get the right distribution of randomization? I would recommend a 2 step process -- first use Math.random()
or an instance of the Random class to choose which list to get a question from, and then use use randomization to select a random question from within the chosen list.
So the first step, getting the random List could look like so:
// declare variable before the if blocks
List<Question> randomList = null;
// get a random int from 0 to 99
int rand = (int) (100 * Math.random());
// get the random list using basic math and if blocks
if (rand < percentEasy)
randomList = easyQuestions;
else if (rand < percentEasy + percentMedium)
randomList = mediumQuestions;
else
randomList = hardQuestions;
OK once the randomList has been obtained, then get a random question from it:
// first get a random index to the list from 0 to < size
int size = randomList.size();
int listIndex = (int)(size * Math.random());
Question randomQuestion = randomList.get(listIndex);
return randomQuestion;
The whole QuestionCollection
class (simplified version) could look like so:
// imports here
public class QuestionCollection
private List<Question> easyQuestions = new ArrayList<>();
private List<Question> mediumQuestions = new ArrayList<>();
private List<Question> hardQuestions = new ArrayList<>();
public void addQuestion(Question q)
switch (q.getDifficulty())
case EASY:
easyQuestions.add(q);
break;
case MEDIUM:
mediumQuestions.add(q);
break;
case HARD:
hardQuestions.add(q);
public Question getRandomQuestion(int percentEasy, int percentMedium, int percentHard)
// if the numbers don't add up to 100, the distribution is broken -- throw an exception
if (percentEasy + percentMedium + percentHard != 100)
String format = "For percentEasy: %d, percentMedium: %d, percentHard: %d";
String text = String.format(format, percentEasy, percentMedium, percentHard);
throw new IllegalArgumentException(text);
List<Question> randomList = null;
int rand = (int) (100 * Math.random());
if (rand < percentEasy)
randomList = easyQuestions;
else if (rand < percentEasy + percentMedium)
randomList = mediumQuestions;
else
randomList = hardQuestions;
// we've now selected the correct List
// now get a random question from the list:
// first get a random index to the list from 0 to < size
int size = randomList.size();
int listIndex = (int)(size * Math.random());
Question randomQuestion = randomList.get(listIndex);
return randomQuestion;
So to test proof of concept, a test program shows that the distribution works:
// imports
public class QuestionFun
public static void main(String args)
// create QuestionCollection object
QuestionCollection questionCollection = new QuestionCollection();
// fill it with questions with random difficulty
for (int i = 0; i < 1000; i++)
String question = "Question #" + i;
String answer = "Answer #" + i;
int randomIndex = (int) (Difficulty.values().length * Math.random());
Difficulty difficulty = Difficulty.values()[randomIndex];
Question q = new Question(question, answer, difficulty);
questionCollection.addQuestion(q);
Map<Difficulty, Integer> frequencyDistMap = new EnumMap<>(Difficulty.class);
for (Difficulty diff : Difficulty.values())
frequencyDistMap.put(diff, 0);
int easyPercent = 20;
int mediumPercent = 70;
int hardPercent = 10;
int questionCount = 10000;
for (int i = 0; i < questionCount; i++)
Question q = questionCollection.getRandomQuestion(easyPercent, mediumPercent, hardPercent);
Difficulty difficulty = q.getDifficulty();
int currentCount = frequencyDistMap.get(difficulty);
currentCount++;
frequencyDistMap.put(difficulty, currentCount);
System.out.println("Difficulty: Count (Percent)");
String format = "%-12s %4d (%02d)%n";
for (Difficulty difficulty : Difficulty.values())
int number = frequencyDistMap.get(difficulty);
int percent = (int) Math.round((100.0 * number) / questionCount);
System.out.printf(format, difficulty + ":", number, percent);
Which returns the original distribution percentages:
Difficulty: Count (Percent)
EASY: 200325 (20)
MEDIUM: 699341 (70)
HARD: 100334 (10)
add a comment |
You should put a probability increasing based on their level.
I would suggest you to think about the hard
level odd first. In this example I set it to this.hardQuestionOdd = (playerLevel - 40) * 0.025;
. That's means it will be 0% until the level 40, and every next level will increase the hard
odd of 2.5%.
Next you set your medium
level odd. In this example I set it to this.mediumQuestionOdd = (playerLevel - 5) * 0.01;
. That's means it will be 0% until the level 5, and every next level will increase the medium
odd of 1%.
After you roll a random with Math.random() + 0.01
. That will provide a number betwwen 0.01 to 1 (1% to 100%) and you compare it with the odd calculation of the hardQuestionOdd
.
If hardQuestionOdd
equal 0.25
, it means 25% of chance, so we check if the roll is under or equal the 0.25
in order to see if it hits the odd. If yes, we know it is a hard
question, else, we do the same for the medium
question. At the end, if it is not a hard
or medium
, it will be easy
by default.
import java.lang.Math;
public class Question
private Integer playerLevel; // Level of the player provided
private String questionLevel; // Level of the question
private Double easyQuestionOdd; // Odd of easy question
private Double mediumQuestionOdd; // Odd of medium question
private Double hardQuestionOdd; // Odd of hard question
// Question class constructor, triggered when we call new Question()
public Question(Integer _playerLevel)
this.playerLevel = _playerLevel; // Set the player level with the one provided
this.mediumQuestionOdd = (playerLevel - 5) * 0.01; // 1% of chance by level over 5 (20% at level 25);
this.hardQuestionOdd = (playerLevel - 40) * 0.025; // 2.5% of chance by level over 40 (100% at level 80);
questionLevel = getQuestionLevel(); // Generate the random selection of question level
System.out.print(questionLevel); // Print the result
// Method to generate a random result of question level based on the level
public String getQuestionLevel()
Double roll = Math.random() + 0.01; // Create a result between 0.01 (1%) to 1 (100%)
if(roll <= this.hardQuestionOdd) questionLevel = "hard"; // Check if the roll is smaller than hardQuestionOdd. If yes, it is an hard question
else
roll = Math.random() + 0.01;
if(roll <= this.mediumQuestionOdd) questionLevel = "medium"; // Check if the roll is smaller than mediumQuestionOdd. If yes, it is an medium question
else questionLevel = "easy"; // Else it is an easy question
return questionLevel; // Return the final value
public static void main(String args)
new Question(80); // Create the question object when the code is running
Output:
new Question(80); // hard
new Question(1); // easy
Odd Table
level easyOdd mediumOdd hardOdd
1 100% 0% 0%
2 100% 0% 0%
3 100% 0% 0%
4 100% 0% 0%
5 100% 0% 0%
6 99% 1% 0%
7 98% 2% 0%
8 97% 3% 0%
9 96% 4% 0%
10 95% 5% 0%
15 90% 10% 0%
20 85% 15% 0%
25 80% 20% 0%
30 75% 25% 0%
35 70% 30% 0%
40 65% 35% 0%
45 47.5% 40% 12.5%
50 35% 45% 25%
60 0% 50% 50%
70 0% 25% 75%
80 0% 0% 100%
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282829%2fhow-can-i-pick-almost-random-questions-from-a-list-depending-on-which-level-you%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have several problems wrapped up in one question, making your question a little broad for this site, but let's break down your issues:
- Create a question class with varying levels of difficulty. Let's call this class
Question
and level of difficulty,Difficulty
- Create a class that holds these questions in a collection or collections of some sort, and allow the user to request a random question from this class. Let's call this class
QuestionCollection
and the method used to request a random question,public Question getRandomQuestion(...)
. - Allow the user to have their own level of advancement. This can be the
User
class - The distribution of the questions possibly received from the request
getRandomQuestion(...)
will depend on the User's level of advancement
So first to encapsulate level of difficulty, let's create an enum, one that has 3 (or more) levels:
public enum Difficulty
EASY, MEDIUM, HARD
Then the Question class can have a private Difficulty difficulty;
field, one set in its constructor, and with a public getter method, public Difficulty getDifficulty()
. A simplified Question class could look something like this:
public class Question
private String question;
private String answer;
private Difficulty difficulty;
public Question(String question, String answer, Difficulty difficulty)
this.question = question;
this.answer = answer;
this.difficulty = difficulty;
public String getQuestion()
return question;
public String getAnswer()
return answer;
public Difficulty getDifficulty()
return difficulty;
Again this is all an over simplification, but it can be used to help illustrate the problem and a possible solution. If desired, you could have this class implement Comparable<Question>
, and using Difficulty to help do the comparison, and allow you to sort a List<Question>
by difficulty.
Then the key to all of this would be the QuestionCollection
class, the one that held collection(s) of questions and that had the getRandomQuestion(...)
method -- how to implement this.
One way, is to rather than worry about User level of advancement at this point, give the getRandomQuestion(...)
method some parameters that allow the QuestionCollection
to know what distribution to use. Easiest in my mind is to give it a relative frequency of difficulty, a percentage of Difficulty.EASY
, Difficulty.MEDIUM
, and Difficulty.HARD
, something like:
public Question getRandomQuestion(int percentEasy, int percentMedium, int percentHard)
// ... code here to get the random question
OK, so now we're at how to build the internal workings of the QuestionCollection
class, and then use this to get a proper distribution of random questions based on the parameter percentages. Probably the simplest is put a question into its own List, such as an ArrayList based on its level of Difficulty -- so here 3 Lists, one for EASY, one for MEDIUM and one for HARD questions.
So:
private List<Question> easyQuestions = new ArrayList<>();
private List<Question> mediumQuestions = new ArrayList<>();
private List<Question> hardQuestions = new ArrayList<>();
Or another possibly cleaner solution is to use a Map<Difficulty, List<Question>>
rather than separate Lists, but I'll keep things simple for now and leave it at 3 Lists.
Then the class would have an public void addQuestion(Question q)
method that would add questions to the correct list based on difficulty level:
public void addQuestion(Question q)
switch (q.getDifficulty())
case EASY:
easyQuestions.add(q);
break;
case MEDIUM:
mediumQuestions.add(q);
break;
case HARD:
hardQuestions.add(q);
OK, so we've got our lists filled with questions, we're now at the core issue of how to get the right distribution of randomization? I would recommend a 2 step process -- first use Math.random()
or an instance of the Random class to choose which list to get a question from, and then use use randomization to select a random question from within the chosen list.
So the first step, getting the random List could look like so:
// declare variable before the if blocks
List<Question> randomList = null;
// get a random int from 0 to 99
int rand = (int) (100 * Math.random());
// get the random list using basic math and if blocks
if (rand < percentEasy)
randomList = easyQuestions;
else if (rand < percentEasy + percentMedium)
randomList = mediumQuestions;
else
randomList = hardQuestions;
OK once the randomList has been obtained, then get a random question from it:
// first get a random index to the list from 0 to < size
int size = randomList.size();
int listIndex = (int)(size * Math.random());
Question randomQuestion = randomList.get(listIndex);
return randomQuestion;
The whole QuestionCollection
class (simplified version) could look like so:
// imports here
public class QuestionCollection
private List<Question> easyQuestions = new ArrayList<>();
private List<Question> mediumQuestions = new ArrayList<>();
private List<Question> hardQuestions = new ArrayList<>();
public void addQuestion(Question q)
switch (q.getDifficulty())
case EASY:
easyQuestions.add(q);
break;
case MEDIUM:
mediumQuestions.add(q);
break;
case HARD:
hardQuestions.add(q);
public Question getRandomQuestion(int percentEasy, int percentMedium, int percentHard)
// if the numbers don't add up to 100, the distribution is broken -- throw an exception
if (percentEasy + percentMedium + percentHard != 100)
String format = "For percentEasy: %d, percentMedium: %d, percentHard: %d";
String text = String.format(format, percentEasy, percentMedium, percentHard);
throw new IllegalArgumentException(text);
List<Question> randomList = null;
int rand = (int) (100 * Math.random());
if (rand < percentEasy)
randomList = easyQuestions;
else if (rand < percentEasy + percentMedium)
randomList = mediumQuestions;
else
randomList = hardQuestions;
// we've now selected the correct List
// now get a random question from the list:
// first get a random index to the list from 0 to < size
int size = randomList.size();
int listIndex = (int)(size * Math.random());
Question randomQuestion = randomList.get(listIndex);
return randomQuestion;
So to test proof of concept, a test program shows that the distribution works:
// imports
public class QuestionFun
public static void main(String args)
// create QuestionCollection object
QuestionCollection questionCollection = new QuestionCollection();
// fill it with questions with random difficulty
for (int i = 0; i < 1000; i++)
String question = "Question #" + i;
String answer = "Answer #" + i;
int randomIndex = (int) (Difficulty.values().length * Math.random());
Difficulty difficulty = Difficulty.values()[randomIndex];
Question q = new Question(question, answer, difficulty);
questionCollection.addQuestion(q);
Map<Difficulty, Integer> frequencyDistMap = new EnumMap<>(Difficulty.class);
for (Difficulty diff : Difficulty.values())
frequencyDistMap.put(diff, 0);
int easyPercent = 20;
int mediumPercent = 70;
int hardPercent = 10;
int questionCount = 10000;
for (int i = 0; i < questionCount; i++)
Question q = questionCollection.getRandomQuestion(easyPercent, mediumPercent, hardPercent);
Difficulty difficulty = q.getDifficulty();
int currentCount = frequencyDistMap.get(difficulty);
currentCount++;
frequencyDistMap.put(difficulty, currentCount);
System.out.println("Difficulty: Count (Percent)");
String format = "%-12s %4d (%02d)%n";
for (Difficulty difficulty : Difficulty.values())
int number = frequencyDistMap.get(difficulty);
int percent = (int) Math.round((100.0 * number) / questionCount);
System.out.printf(format, difficulty + ":", number, percent);
Which returns the original distribution percentages:
Difficulty: Count (Percent)
EASY: 200325 (20)
MEDIUM: 699341 (70)
HARD: 100334 (10)
add a comment |
You have several problems wrapped up in one question, making your question a little broad for this site, but let's break down your issues:
- Create a question class with varying levels of difficulty. Let's call this class
Question
and level of difficulty,Difficulty
- Create a class that holds these questions in a collection or collections of some sort, and allow the user to request a random question from this class. Let's call this class
QuestionCollection
and the method used to request a random question,public Question getRandomQuestion(...)
. - Allow the user to have their own level of advancement. This can be the
User
class - The distribution of the questions possibly received from the request
getRandomQuestion(...)
will depend on the User's level of advancement
So first to encapsulate level of difficulty, let's create an enum, one that has 3 (or more) levels:
public enum Difficulty
EASY, MEDIUM, HARD
Then the Question class can have a private Difficulty difficulty;
field, one set in its constructor, and with a public getter method, public Difficulty getDifficulty()
. A simplified Question class could look something like this:
public class Question
private String question;
private String answer;
private Difficulty difficulty;
public Question(String question, String answer, Difficulty difficulty)
this.question = question;
this.answer = answer;
this.difficulty = difficulty;
public String getQuestion()
return question;
public String getAnswer()
return answer;
public Difficulty getDifficulty()
return difficulty;
Again this is all an over simplification, but it can be used to help illustrate the problem and a possible solution. If desired, you could have this class implement Comparable<Question>
, and using Difficulty to help do the comparison, and allow you to sort a List<Question>
by difficulty.
Then the key to all of this would be the QuestionCollection
class, the one that held collection(s) of questions and that had the getRandomQuestion(...)
method -- how to implement this.
One way, is to rather than worry about User level of advancement at this point, give the getRandomQuestion(...)
method some parameters that allow the QuestionCollection
to know what distribution to use. Easiest in my mind is to give it a relative frequency of difficulty, a percentage of Difficulty.EASY
, Difficulty.MEDIUM
, and Difficulty.HARD
, something like:
public Question getRandomQuestion(int percentEasy, int percentMedium, int percentHard)
// ... code here to get the random question
OK, so now we're at how to build the internal workings of the QuestionCollection
class, and then use this to get a proper distribution of random questions based on the parameter percentages. Probably the simplest is put a question into its own List, such as an ArrayList based on its level of Difficulty -- so here 3 Lists, one for EASY, one for MEDIUM and one for HARD questions.
So:
private List<Question> easyQuestions = new ArrayList<>();
private List<Question> mediumQuestions = new ArrayList<>();
private List<Question> hardQuestions = new ArrayList<>();
Or another possibly cleaner solution is to use a Map<Difficulty, List<Question>>
rather than separate Lists, but I'll keep things simple for now and leave it at 3 Lists.
Then the class would have an public void addQuestion(Question q)
method that would add questions to the correct list based on difficulty level:
public void addQuestion(Question q)
switch (q.getDifficulty())
case EASY:
easyQuestions.add(q);
break;
case MEDIUM:
mediumQuestions.add(q);
break;
case HARD:
hardQuestions.add(q);
OK, so we've got our lists filled with questions, we're now at the core issue of how to get the right distribution of randomization? I would recommend a 2 step process -- first use Math.random()
or an instance of the Random class to choose which list to get a question from, and then use use randomization to select a random question from within the chosen list.
So the first step, getting the random List could look like so:
// declare variable before the if blocks
List<Question> randomList = null;
// get a random int from 0 to 99
int rand = (int) (100 * Math.random());
// get the random list using basic math and if blocks
if (rand < percentEasy)
randomList = easyQuestions;
else if (rand < percentEasy + percentMedium)
randomList = mediumQuestions;
else
randomList = hardQuestions;
OK once the randomList has been obtained, then get a random question from it:
// first get a random index to the list from 0 to < size
int size = randomList.size();
int listIndex = (int)(size * Math.random());
Question randomQuestion = randomList.get(listIndex);
return randomQuestion;
The whole QuestionCollection
class (simplified version) could look like so:
// imports here
public class QuestionCollection
private List<Question> easyQuestions = new ArrayList<>();
private List<Question> mediumQuestions = new ArrayList<>();
private List<Question> hardQuestions = new ArrayList<>();
public void addQuestion(Question q)
switch (q.getDifficulty())
case EASY:
easyQuestions.add(q);
break;
case MEDIUM:
mediumQuestions.add(q);
break;
case HARD:
hardQuestions.add(q);
public Question getRandomQuestion(int percentEasy, int percentMedium, int percentHard)
// if the numbers don't add up to 100, the distribution is broken -- throw an exception
if (percentEasy + percentMedium + percentHard != 100)
String format = "For percentEasy: %d, percentMedium: %d, percentHard: %d";
String text = String.format(format, percentEasy, percentMedium, percentHard);
throw new IllegalArgumentException(text);
List<Question> randomList = null;
int rand = (int) (100 * Math.random());
if (rand < percentEasy)
randomList = easyQuestions;
else if (rand < percentEasy + percentMedium)
randomList = mediumQuestions;
else
randomList = hardQuestions;
// we've now selected the correct List
// now get a random question from the list:
// first get a random index to the list from 0 to < size
int size = randomList.size();
int listIndex = (int)(size * Math.random());
Question randomQuestion = randomList.get(listIndex);
return randomQuestion;
So to test proof of concept, a test program shows that the distribution works:
// imports
public class QuestionFun
public static void main(String args)
// create QuestionCollection object
QuestionCollection questionCollection = new QuestionCollection();
// fill it with questions with random difficulty
for (int i = 0; i < 1000; i++)
String question = "Question #" + i;
String answer = "Answer #" + i;
int randomIndex = (int) (Difficulty.values().length * Math.random());
Difficulty difficulty = Difficulty.values()[randomIndex];
Question q = new Question(question, answer, difficulty);
questionCollection.addQuestion(q);
Map<Difficulty, Integer> frequencyDistMap = new EnumMap<>(Difficulty.class);
for (Difficulty diff : Difficulty.values())
frequencyDistMap.put(diff, 0);
int easyPercent = 20;
int mediumPercent = 70;
int hardPercent = 10;
int questionCount = 10000;
for (int i = 0; i < questionCount; i++)
Question q = questionCollection.getRandomQuestion(easyPercent, mediumPercent, hardPercent);
Difficulty difficulty = q.getDifficulty();
int currentCount = frequencyDistMap.get(difficulty);
currentCount++;
frequencyDistMap.put(difficulty, currentCount);
System.out.println("Difficulty: Count (Percent)");
String format = "%-12s %4d (%02d)%n";
for (Difficulty difficulty : Difficulty.values())
int number = frequencyDistMap.get(difficulty);
int percent = (int) Math.round((100.0 * number) / questionCount);
System.out.printf(format, difficulty + ":", number, percent);
Which returns the original distribution percentages:
Difficulty: Count (Percent)
EASY: 200325 (20)
MEDIUM: 699341 (70)
HARD: 100334 (10)
add a comment |
You have several problems wrapped up in one question, making your question a little broad for this site, but let's break down your issues:
- Create a question class with varying levels of difficulty. Let's call this class
Question
and level of difficulty,Difficulty
- Create a class that holds these questions in a collection or collections of some sort, and allow the user to request a random question from this class. Let's call this class
QuestionCollection
and the method used to request a random question,public Question getRandomQuestion(...)
. - Allow the user to have their own level of advancement. This can be the
User
class - The distribution of the questions possibly received from the request
getRandomQuestion(...)
will depend on the User's level of advancement
So first to encapsulate level of difficulty, let's create an enum, one that has 3 (or more) levels:
public enum Difficulty
EASY, MEDIUM, HARD
Then the Question class can have a private Difficulty difficulty;
field, one set in its constructor, and with a public getter method, public Difficulty getDifficulty()
. A simplified Question class could look something like this:
public class Question
private String question;
private String answer;
private Difficulty difficulty;
public Question(String question, String answer, Difficulty difficulty)
this.question = question;
this.answer = answer;
this.difficulty = difficulty;
public String getQuestion()
return question;
public String getAnswer()
return answer;
public Difficulty getDifficulty()
return difficulty;
Again this is all an over simplification, but it can be used to help illustrate the problem and a possible solution. If desired, you could have this class implement Comparable<Question>
, and using Difficulty to help do the comparison, and allow you to sort a List<Question>
by difficulty.
Then the key to all of this would be the QuestionCollection
class, the one that held collection(s) of questions and that had the getRandomQuestion(...)
method -- how to implement this.
One way, is to rather than worry about User level of advancement at this point, give the getRandomQuestion(...)
method some parameters that allow the QuestionCollection
to know what distribution to use. Easiest in my mind is to give it a relative frequency of difficulty, a percentage of Difficulty.EASY
, Difficulty.MEDIUM
, and Difficulty.HARD
, something like:
public Question getRandomQuestion(int percentEasy, int percentMedium, int percentHard)
// ... code here to get the random question
OK, so now we're at how to build the internal workings of the QuestionCollection
class, and then use this to get a proper distribution of random questions based on the parameter percentages. Probably the simplest is put a question into its own List, such as an ArrayList based on its level of Difficulty -- so here 3 Lists, one for EASY, one for MEDIUM and one for HARD questions.
So:
private List<Question> easyQuestions = new ArrayList<>();
private List<Question> mediumQuestions = new ArrayList<>();
private List<Question> hardQuestions = new ArrayList<>();
Or another possibly cleaner solution is to use a Map<Difficulty, List<Question>>
rather than separate Lists, but I'll keep things simple for now and leave it at 3 Lists.
Then the class would have an public void addQuestion(Question q)
method that would add questions to the correct list based on difficulty level:
public void addQuestion(Question q)
switch (q.getDifficulty())
case EASY:
easyQuestions.add(q);
break;
case MEDIUM:
mediumQuestions.add(q);
break;
case HARD:
hardQuestions.add(q);
OK, so we've got our lists filled with questions, we're now at the core issue of how to get the right distribution of randomization? I would recommend a 2 step process -- first use Math.random()
or an instance of the Random class to choose which list to get a question from, and then use use randomization to select a random question from within the chosen list.
So the first step, getting the random List could look like so:
// declare variable before the if blocks
List<Question> randomList = null;
// get a random int from 0 to 99
int rand = (int) (100 * Math.random());
// get the random list using basic math and if blocks
if (rand < percentEasy)
randomList = easyQuestions;
else if (rand < percentEasy + percentMedium)
randomList = mediumQuestions;
else
randomList = hardQuestions;
OK once the randomList has been obtained, then get a random question from it:
// first get a random index to the list from 0 to < size
int size = randomList.size();
int listIndex = (int)(size * Math.random());
Question randomQuestion = randomList.get(listIndex);
return randomQuestion;
The whole QuestionCollection
class (simplified version) could look like so:
// imports here
public class QuestionCollection
private List<Question> easyQuestions = new ArrayList<>();
private List<Question> mediumQuestions = new ArrayList<>();
private List<Question> hardQuestions = new ArrayList<>();
public void addQuestion(Question q)
switch (q.getDifficulty())
case EASY:
easyQuestions.add(q);
break;
case MEDIUM:
mediumQuestions.add(q);
break;
case HARD:
hardQuestions.add(q);
public Question getRandomQuestion(int percentEasy, int percentMedium, int percentHard)
// if the numbers don't add up to 100, the distribution is broken -- throw an exception
if (percentEasy + percentMedium + percentHard != 100)
String format = "For percentEasy: %d, percentMedium: %d, percentHard: %d";
String text = String.format(format, percentEasy, percentMedium, percentHard);
throw new IllegalArgumentException(text);
List<Question> randomList = null;
int rand = (int) (100 * Math.random());
if (rand < percentEasy)
randomList = easyQuestions;
else if (rand < percentEasy + percentMedium)
randomList = mediumQuestions;
else
randomList = hardQuestions;
// we've now selected the correct List
// now get a random question from the list:
// first get a random index to the list from 0 to < size
int size = randomList.size();
int listIndex = (int)(size * Math.random());
Question randomQuestion = randomList.get(listIndex);
return randomQuestion;
So to test proof of concept, a test program shows that the distribution works:
// imports
public class QuestionFun
public static void main(String args)
// create QuestionCollection object
QuestionCollection questionCollection = new QuestionCollection();
// fill it with questions with random difficulty
for (int i = 0; i < 1000; i++)
String question = "Question #" + i;
String answer = "Answer #" + i;
int randomIndex = (int) (Difficulty.values().length * Math.random());
Difficulty difficulty = Difficulty.values()[randomIndex];
Question q = new Question(question, answer, difficulty);
questionCollection.addQuestion(q);
Map<Difficulty, Integer> frequencyDistMap = new EnumMap<>(Difficulty.class);
for (Difficulty diff : Difficulty.values())
frequencyDistMap.put(diff, 0);
int easyPercent = 20;
int mediumPercent = 70;
int hardPercent = 10;
int questionCount = 10000;
for (int i = 0; i < questionCount; i++)
Question q = questionCollection.getRandomQuestion(easyPercent, mediumPercent, hardPercent);
Difficulty difficulty = q.getDifficulty();
int currentCount = frequencyDistMap.get(difficulty);
currentCount++;
frequencyDistMap.put(difficulty, currentCount);
System.out.println("Difficulty: Count (Percent)");
String format = "%-12s %4d (%02d)%n";
for (Difficulty difficulty : Difficulty.values())
int number = frequencyDistMap.get(difficulty);
int percent = (int) Math.round((100.0 * number) / questionCount);
System.out.printf(format, difficulty + ":", number, percent);
Which returns the original distribution percentages:
Difficulty: Count (Percent)
EASY: 200325 (20)
MEDIUM: 699341 (70)
HARD: 100334 (10)
You have several problems wrapped up in one question, making your question a little broad for this site, but let's break down your issues:
- Create a question class with varying levels of difficulty. Let's call this class
Question
and level of difficulty,Difficulty
- Create a class that holds these questions in a collection or collections of some sort, and allow the user to request a random question from this class. Let's call this class
QuestionCollection
and the method used to request a random question,public Question getRandomQuestion(...)
. - Allow the user to have their own level of advancement. This can be the
User
class - The distribution of the questions possibly received from the request
getRandomQuestion(...)
will depend on the User's level of advancement
So first to encapsulate level of difficulty, let's create an enum, one that has 3 (or more) levels:
public enum Difficulty
EASY, MEDIUM, HARD
Then the Question class can have a private Difficulty difficulty;
field, one set in its constructor, and with a public getter method, public Difficulty getDifficulty()
. A simplified Question class could look something like this:
public class Question
private String question;
private String answer;
private Difficulty difficulty;
public Question(String question, String answer, Difficulty difficulty)
this.question = question;
this.answer = answer;
this.difficulty = difficulty;
public String getQuestion()
return question;
public String getAnswer()
return answer;
public Difficulty getDifficulty()
return difficulty;
Again this is all an over simplification, but it can be used to help illustrate the problem and a possible solution. If desired, you could have this class implement Comparable<Question>
, and using Difficulty to help do the comparison, and allow you to sort a List<Question>
by difficulty.
Then the key to all of this would be the QuestionCollection
class, the one that held collection(s) of questions and that had the getRandomQuestion(...)
method -- how to implement this.
One way, is to rather than worry about User level of advancement at this point, give the getRandomQuestion(...)
method some parameters that allow the QuestionCollection
to know what distribution to use. Easiest in my mind is to give it a relative frequency of difficulty, a percentage of Difficulty.EASY
, Difficulty.MEDIUM
, and Difficulty.HARD
, something like:
public Question getRandomQuestion(int percentEasy, int percentMedium, int percentHard)
// ... code here to get the random question
OK, so now we're at how to build the internal workings of the QuestionCollection
class, and then use this to get a proper distribution of random questions based on the parameter percentages. Probably the simplest is put a question into its own List, such as an ArrayList based on its level of Difficulty -- so here 3 Lists, one for EASY, one for MEDIUM and one for HARD questions.
So:
private List<Question> easyQuestions = new ArrayList<>();
private List<Question> mediumQuestions = new ArrayList<>();
private List<Question> hardQuestions = new ArrayList<>();
Or another possibly cleaner solution is to use a Map<Difficulty, List<Question>>
rather than separate Lists, but I'll keep things simple for now and leave it at 3 Lists.
Then the class would have an public void addQuestion(Question q)
method that would add questions to the correct list based on difficulty level:
public void addQuestion(Question q)
switch (q.getDifficulty())
case EASY:
easyQuestions.add(q);
break;
case MEDIUM:
mediumQuestions.add(q);
break;
case HARD:
hardQuestions.add(q);
OK, so we've got our lists filled with questions, we're now at the core issue of how to get the right distribution of randomization? I would recommend a 2 step process -- first use Math.random()
or an instance of the Random class to choose which list to get a question from, and then use use randomization to select a random question from within the chosen list.
So the first step, getting the random List could look like so:
// declare variable before the if blocks
List<Question> randomList = null;
// get a random int from 0 to 99
int rand = (int) (100 * Math.random());
// get the random list using basic math and if blocks
if (rand < percentEasy)
randomList = easyQuestions;
else if (rand < percentEasy + percentMedium)
randomList = mediumQuestions;
else
randomList = hardQuestions;
OK once the randomList has been obtained, then get a random question from it:
// first get a random index to the list from 0 to < size
int size = randomList.size();
int listIndex = (int)(size * Math.random());
Question randomQuestion = randomList.get(listIndex);
return randomQuestion;
The whole QuestionCollection
class (simplified version) could look like so:
// imports here
public class QuestionCollection
private List<Question> easyQuestions = new ArrayList<>();
private List<Question> mediumQuestions = new ArrayList<>();
private List<Question> hardQuestions = new ArrayList<>();
public void addQuestion(Question q)
switch (q.getDifficulty())
case EASY:
easyQuestions.add(q);
break;
case MEDIUM:
mediumQuestions.add(q);
break;
case HARD:
hardQuestions.add(q);
public Question getRandomQuestion(int percentEasy, int percentMedium, int percentHard)
// if the numbers don't add up to 100, the distribution is broken -- throw an exception
if (percentEasy + percentMedium + percentHard != 100)
String format = "For percentEasy: %d, percentMedium: %d, percentHard: %d";
String text = String.format(format, percentEasy, percentMedium, percentHard);
throw new IllegalArgumentException(text);
List<Question> randomList = null;
int rand = (int) (100 * Math.random());
if (rand < percentEasy)
randomList = easyQuestions;
else if (rand < percentEasy + percentMedium)
randomList = mediumQuestions;
else
randomList = hardQuestions;
// we've now selected the correct List
// now get a random question from the list:
// first get a random index to the list from 0 to < size
int size = randomList.size();
int listIndex = (int)(size * Math.random());
Question randomQuestion = randomList.get(listIndex);
return randomQuestion;
So to test proof of concept, a test program shows that the distribution works:
// imports
public class QuestionFun
public static void main(String args)
// create QuestionCollection object
QuestionCollection questionCollection = new QuestionCollection();
// fill it with questions with random difficulty
for (int i = 0; i < 1000; i++)
String question = "Question #" + i;
String answer = "Answer #" + i;
int randomIndex = (int) (Difficulty.values().length * Math.random());
Difficulty difficulty = Difficulty.values()[randomIndex];
Question q = new Question(question, answer, difficulty);
questionCollection.addQuestion(q);
Map<Difficulty, Integer> frequencyDistMap = new EnumMap<>(Difficulty.class);
for (Difficulty diff : Difficulty.values())
frequencyDistMap.put(diff, 0);
int easyPercent = 20;
int mediumPercent = 70;
int hardPercent = 10;
int questionCount = 10000;
for (int i = 0; i < questionCount; i++)
Question q = questionCollection.getRandomQuestion(easyPercent, mediumPercent, hardPercent);
Difficulty difficulty = q.getDifficulty();
int currentCount = frequencyDistMap.get(difficulty);
currentCount++;
frequencyDistMap.put(difficulty, currentCount);
System.out.println("Difficulty: Count (Percent)");
String format = "%-12s %4d (%02d)%n";
for (Difficulty difficulty : Difficulty.values())
int number = frequencyDistMap.get(difficulty);
int percent = (int) Math.round((100.0 * number) / questionCount);
System.out.printf(format, difficulty + ":", number, percent);
Which returns the original distribution percentages:
Difficulty: Count (Percent)
EASY: 200325 (20)
MEDIUM: 699341 (70)
HARD: 100334 (10)
edited Nov 17 '18 at 20:41
answered Nov 17 '18 at 15:12
Hovercraft Full Of EelsHovercraft Full Of Eels
261k20211317
261k20211317
add a comment |
add a comment |
You should put a probability increasing based on their level.
I would suggest you to think about the hard
level odd first. In this example I set it to this.hardQuestionOdd = (playerLevel - 40) * 0.025;
. That's means it will be 0% until the level 40, and every next level will increase the hard
odd of 2.5%.
Next you set your medium
level odd. In this example I set it to this.mediumQuestionOdd = (playerLevel - 5) * 0.01;
. That's means it will be 0% until the level 5, and every next level will increase the medium
odd of 1%.
After you roll a random with Math.random() + 0.01
. That will provide a number betwwen 0.01 to 1 (1% to 100%) and you compare it with the odd calculation of the hardQuestionOdd
.
If hardQuestionOdd
equal 0.25
, it means 25% of chance, so we check if the roll is under or equal the 0.25
in order to see if it hits the odd. If yes, we know it is a hard
question, else, we do the same for the medium
question. At the end, if it is not a hard
or medium
, it will be easy
by default.
import java.lang.Math;
public class Question
private Integer playerLevel; // Level of the player provided
private String questionLevel; // Level of the question
private Double easyQuestionOdd; // Odd of easy question
private Double mediumQuestionOdd; // Odd of medium question
private Double hardQuestionOdd; // Odd of hard question
// Question class constructor, triggered when we call new Question()
public Question(Integer _playerLevel)
this.playerLevel = _playerLevel; // Set the player level with the one provided
this.mediumQuestionOdd = (playerLevel - 5) * 0.01; // 1% of chance by level over 5 (20% at level 25);
this.hardQuestionOdd = (playerLevel - 40) * 0.025; // 2.5% of chance by level over 40 (100% at level 80);
questionLevel = getQuestionLevel(); // Generate the random selection of question level
System.out.print(questionLevel); // Print the result
// Method to generate a random result of question level based on the level
public String getQuestionLevel()
Double roll = Math.random() + 0.01; // Create a result between 0.01 (1%) to 1 (100%)
if(roll <= this.hardQuestionOdd) questionLevel = "hard"; // Check if the roll is smaller than hardQuestionOdd. If yes, it is an hard question
else
roll = Math.random() + 0.01;
if(roll <= this.mediumQuestionOdd) questionLevel = "medium"; // Check if the roll is smaller than mediumQuestionOdd. If yes, it is an medium question
else questionLevel = "easy"; // Else it is an easy question
return questionLevel; // Return the final value
public static void main(String args)
new Question(80); // Create the question object when the code is running
Output:
new Question(80); // hard
new Question(1); // easy
Odd Table
level easyOdd mediumOdd hardOdd
1 100% 0% 0%
2 100% 0% 0%
3 100% 0% 0%
4 100% 0% 0%
5 100% 0% 0%
6 99% 1% 0%
7 98% 2% 0%
8 97% 3% 0%
9 96% 4% 0%
10 95% 5% 0%
15 90% 10% 0%
20 85% 15% 0%
25 80% 20% 0%
30 75% 25% 0%
35 70% 30% 0%
40 65% 35% 0%
45 47.5% 40% 12.5%
50 35% 45% 25%
60 0% 50% 50%
70 0% 25% 75%
80 0% 0% 100%
add a comment |
You should put a probability increasing based on their level.
I would suggest you to think about the hard
level odd first. In this example I set it to this.hardQuestionOdd = (playerLevel - 40) * 0.025;
. That's means it will be 0% until the level 40, and every next level will increase the hard
odd of 2.5%.
Next you set your medium
level odd. In this example I set it to this.mediumQuestionOdd = (playerLevel - 5) * 0.01;
. That's means it will be 0% until the level 5, and every next level will increase the medium
odd of 1%.
After you roll a random with Math.random() + 0.01
. That will provide a number betwwen 0.01 to 1 (1% to 100%) and you compare it with the odd calculation of the hardQuestionOdd
.
If hardQuestionOdd
equal 0.25
, it means 25% of chance, so we check if the roll is under or equal the 0.25
in order to see if it hits the odd. If yes, we know it is a hard
question, else, we do the same for the medium
question. At the end, if it is not a hard
or medium
, it will be easy
by default.
import java.lang.Math;
public class Question
private Integer playerLevel; // Level of the player provided
private String questionLevel; // Level of the question
private Double easyQuestionOdd; // Odd of easy question
private Double mediumQuestionOdd; // Odd of medium question
private Double hardQuestionOdd; // Odd of hard question
// Question class constructor, triggered when we call new Question()
public Question(Integer _playerLevel)
this.playerLevel = _playerLevel; // Set the player level with the one provided
this.mediumQuestionOdd = (playerLevel - 5) * 0.01; // 1% of chance by level over 5 (20% at level 25);
this.hardQuestionOdd = (playerLevel - 40) * 0.025; // 2.5% of chance by level over 40 (100% at level 80);
questionLevel = getQuestionLevel(); // Generate the random selection of question level
System.out.print(questionLevel); // Print the result
// Method to generate a random result of question level based on the level
public String getQuestionLevel()
Double roll = Math.random() + 0.01; // Create a result between 0.01 (1%) to 1 (100%)
if(roll <= this.hardQuestionOdd) questionLevel = "hard"; // Check if the roll is smaller than hardQuestionOdd. If yes, it is an hard question
else
roll = Math.random() + 0.01;
if(roll <= this.mediumQuestionOdd) questionLevel = "medium"; // Check if the roll is smaller than mediumQuestionOdd. If yes, it is an medium question
else questionLevel = "easy"; // Else it is an easy question
return questionLevel; // Return the final value
public static void main(String args)
new Question(80); // Create the question object when the code is running
Output:
new Question(80); // hard
new Question(1); // easy
Odd Table
level easyOdd mediumOdd hardOdd
1 100% 0% 0%
2 100% 0% 0%
3 100% 0% 0%
4 100% 0% 0%
5 100% 0% 0%
6 99% 1% 0%
7 98% 2% 0%
8 97% 3% 0%
9 96% 4% 0%
10 95% 5% 0%
15 90% 10% 0%
20 85% 15% 0%
25 80% 20% 0%
30 75% 25% 0%
35 70% 30% 0%
40 65% 35% 0%
45 47.5% 40% 12.5%
50 35% 45% 25%
60 0% 50% 50%
70 0% 25% 75%
80 0% 0% 100%
add a comment |
You should put a probability increasing based on their level.
I would suggest you to think about the hard
level odd first. In this example I set it to this.hardQuestionOdd = (playerLevel - 40) * 0.025;
. That's means it will be 0% until the level 40, and every next level will increase the hard
odd of 2.5%.
Next you set your medium
level odd. In this example I set it to this.mediumQuestionOdd = (playerLevel - 5) * 0.01;
. That's means it will be 0% until the level 5, and every next level will increase the medium
odd of 1%.
After you roll a random with Math.random() + 0.01
. That will provide a number betwwen 0.01 to 1 (1% to 100%) and you compare it with the odd calculation of the hardQuestionOdd
.
If hardQuestionOdd
equal 0.25
, it means 25% of chance, so we check if the roll is under or equal the 0.25
in order to see if it hits the odd. If yes, we know it is a hard
question, else, we do the same for the medium
question. At the end, if it is not a hard
or medium
, it will be easy
by default.
import java.lang.Math;
public class Question
private Integer playerLevel; // Level of the player provided
private String questionLevel; // Level of the question
private Double easyQuestionOdd; // Odd of easy question
private Double mediumQuestionOdd; // Odd of medium question
private Double hardQuestionOdd; // Odd of hard question
// Question class constructor, triggered when we call new Question()
public Question(Integer _playerLevel)
this.playerLevel = _playerLevel; // Set the player level with the one provided
this.mediumQuestionOdd = (playerLevel - 5) * 0.01; // 1% of chance by level over 5 (20% at level 25);
this.hardQuestionOdd = (playerLevel - 40) * 0.025; // 2.5% of chance by level over 40 (100% at level 80);
questionLevel = getQuestionLevel(); // Generate the random selection of question level
System.out.print(questionLevel); // Print the result
// Method to generate a random result of question level based on the level
public String getQuestionLevel()
Double roll = Math.random() + 0.01; // Create a result between 0.01 (1%) to 1 (100%)
if(roll <= this.hardQuestionOdd) questionLevel = "hard"; // Check if the roll is smaller than hardQuestionOdd. If yes, it is an hard question
else
roll = Math.random() + 0.01;
if(roll <= this.mediumQuestionOdd) questionLevel = "medium"; // Check if the roll is smaller than mediumQuestionOdd. If yes, it is an medium question
else questionLevel = "easy"; // Else it is an easy question
return questionLevel; // Return the final value
public static void main(String args)
new Question(80); // Create the question object when the code is running
Output:
new Question(80); // hard
new Question(1); // easy
Odd Table
level easyOdd mediumOdd hardOdd
1 100% 0% 0%
2 100% 0% 0%
3 100% 0% 0%
4 100% 0% 0%
5 100% 0% 0%
6 99% 1% 0%
7 98% 2% 0%
8 97% 3% 0%
9 96% 4% 0%
10 95% 5% 0%
15 90% 10% 0%
20 85% 15% 0%
25 80% 20% 0%
30 75% 25% 0%
35 70% 30% 0%
40 65% 35% 0%
45 47.5% 40% 12.5%
50 35% 45% 25%
60 0% 50% 50%
70 0% 25% 75%
80 0% 0% 100%
You should put a probability increasing based on their level.
I would suggest you to think about the hard
level odd first. In this example I set it to this.hardQuestionOdd = (playerLevel - 40) * 0.025;
. That's means it will be 0% until the level 40, and every next level will increase the hard
odd of 2.5%.
Next you set your medium
level odd. In this example I set it to this.mediumQuestionOdd = (playerLevel - 5) * 0.01;
. That's means it will be 0% until the level 5, and every next level will increase the medium
odd of 1%.
After you roll a random with Math.random() + 0.01
. That will provide a number betwwen 0.01 to 1 (1% to 100%) and you compare it with the odd calculation of the hardQuestionOdd
.
If hardQuestionOdd
equal 0.25
, it means 25% of chance, so we check if the roll is under or equal the 0.25
in order to see if it hits the odd. If yes, we know it is a hard
question, else, we do the same for the medium
question. At the end, if it is not a hard
or medium
, it will be easy
by default.
import java.lang.Math;
public class Question
private Integer playerLevel; // Level of the player provided
private String questionLevel; // Level of the question
private Double easyQuestionOdd; // Odd of easy question
private Double mediumQuestionOdd; // Odd of medium question
private Double hardQuestionOdd; // Odd of hard question
// Question class constructor, triggered when we call new Question()
public Question(Integer _playerLevel)
this.playerLevel = _playerLevel; // Set the player level with the one provided
this.mediumQuestionOdd = (playerLevel - 5) * 0.01; // 1% of chance by level over 5 (20% at level 25);
this.hardQuestionOdd = (playerLevel - 40) * 0.025; // 2.5% of chance by level over 40 (100% at level 80);
questionLevel = getQuestionLevel(); // Generate the random selection of question level
System.out.print(questionLevel); // Print the result
// Method to generate a random result of question level based on the level
public String getQuestionLevel()
Double roll = Math.random() + 0.01; // Create a result between 0.01 (1%) to 1 (100%)
if(roll <= this.hardQuestionOdd) questionLevel = "hard"; // Check if the roll is smaller than hardQuestionOdd. If yes, it is an hard question
else
roll = Math.random() + 0.01;
if(roll <= this.mediumQuestionOdd) questionLevel = "medium"; // Check if the roll is smaller than mediumQuestionOdd. If yes, it is an medium question
else questionLevel = "easy"; // Else it is an easy question
return questionLevel; // Return the final value
public static void main(String args)
new Question(80); // Create the question object when the code is running
Output:
new Question(80); // hard
new Question(1); // easy
Odd Table
level easyOdd mediumOdd hardOdd
1 100% 0% 0%
2 100% 0% 0%
3 100% 0% 0%
4 100% 0% 0%
5 100% 0% 0%
6 99% 1% 0%
7 98% 2% 0%
8 97% 3% 0%
9 96% 4% 0%
10 95% 5% 0%
15 90% 10% 0%
20 85% 15% 0%
25 80% 20% 0%
30 75% 25% 0%
35 70% 30% 0%
40 65% 35% 0%
45 47.5% 40% 12.5%
50 35% 45% 25%
60 0% 50% 50%
70 0% 25% 75%
80 0% 0% 100%
edited Nov 13 '18 at 20:12
answered Nov 13 '18 at 14:40
Jonathan GagneJonathan Gagne
2,1483721
2,1483721
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282829%2fhow-can-i-pick-almost-random-questions-from-a-list-depending-on-which-level-you%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you add all questions in an arraylist sorted by difficulty, then get question as
myList.get(Math.random(level + whatever))
.– LunaticJape
Nov 13 '18 at 14:10