1

I am beginner in flutter. I am trying to make simple addition program which will generate random number to add with each other and also create 4 random options for answer out of which one will be correct answer. But the problem is sometimes similar options and I don't want to correct options for result.

Below is my code. can anybody help me out?

class _StartGameState extends State<StartGame> {
  int inp1, inp2;
  GenerateQuestion g = new GenerateQuestion();
  int num1, num2, num3, num4;
  int no1, no2, no3, no4;
  int pos;
  int res;
  Map answers;
  bool flag = true;

  void getNum() {
    num1 = g.generateNum();
    num2 = g.generateNum();
    num3 = g.generateNum();
    num4 = g.generateNum();
    inp1 = g.generateValue();
    inp2 = g.generateValue();
    // this method returns value from 0 to 3
    pos = g.answerPosition();
    res = inp1 + inp2;
    switch (pos) {
      case 0:
        num1 = res;
        break;
      case 1:
        num2 = res;
        break;
      case 2:
        num3 = res;
        break;
      case 3:
        num4 = res;
        break;
    }
    if (res == num1 || res == num2 || res == num3 || res == num4) {
      if (num1 != num2 &&
          num1 != num3 &&
          num1 != num4 &&
          num2 != num3 &&
          num2 != num4 &&
          num3 != num4) {
        setState(() {
          no1 = num1;
          no2 = num2;
          no3 = num3;
          no4 = num4;
        });
      }
    }
  }
}

GenerateQuestion is a separate class file. which has simple methods which creates random object with range and returns value;

My Interface is like enter image description here

5
  • I don't understand "But the problem is sometimes similar options and I don't want to correct options for result.". Can you try clarify what the issue is? Also, you code can be made a lot simpler but I need to know what the expected behavior should be. Commented Nov 16, 2020 at 18:04
  • I need 4 unique options as a answer for addition. but random() returns sometimes two similar integers because the range is only upto 20. so for example if my question is 10 + 5 = ? I want answers options i.e, num1, num2, num3, num4 as may be 10, 15, 20, 18. but sometimes i am getting options like , 10, 15, 15, 20. Here 15 is correct answer. but as two options are generated for correct answer(15) user might get confuse which one to select. for that i need unqiue options and one of them should be result of addition. Commented Nov 16, 2020 at 18:06
  • You mean equal integers? Similar should not be a problem as long it is not the same as another value? Commented Nov 16, 2020 at 18:09
  • Yes equal integers. I don't want to equal integers for correct answer Commented Nov 16, 2020 at 18:10
  • I have this kind of interface i.sstatic.net/VrN5Y.png where the four boxes are options for the question. Commented Nov 16, 2020 at 18:12

2 Answers 2

1

In order to make sure that options don't get repeat, you have to validate an option after generating it with already generated options for e.g,

//create a list to store options
var optionList = new List(4);
while(optionList.length() < 4){
  // generate your option
  int option = g.generateNum();
  if(!optionList.contains(option)){
      optionList.add(option);
   }
}
// then get your options
num1 = optionList[0];
num2 = optionList[1];
num3 = optionList[2];
num4 = optionList[3];

 
Sign up to request clarification or add additional context in comments.

Comments

1

I have made the following example which shows how you can generate your options:

import 'dart:math';

void main() {
  final options = getOptions(15, 0, 20, 4);

  final num1 = options[0];
  final num2 = options[1];
  final num3 = options[2];
  final num4 = options[3];

  print('num1: $num1'); // num1: 17
  print('num2: $num2'); // num2: 15
  print('num3: $num3'); // num3: 5
  print('num4: $num4'); // num4: 0
}

List<int> getOptions(int answer, int min, int max, int numberOfOptions) {
  final rnd = Random();
  final options = {answer};

  while (options.length < numberOfOptions) {
    options.add(min + rnd.nextInt(max - min));
  }

  return options.toList()..shuffle();
}

The list of possible generated values will be including min but excluding max.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.