how to reduce time to check conditions in loop or if-else statement

285

As suggested by @Illegal Argument you can use a set which will look like this:

class GenerateQuestionSet {
  var num1, num2, num3, num4;

  final random = Random();

  void generateNum() {
    final numbers = <int>{};
    while (numbers.length < 4) {
      numbers.add(random.nextInt(20));
    }
    final nums = numbers.toList();
    num1 = nums[0];
    num2 = nums[1];
    num3 = nums[2];
    num4 = nums[3];
  }
}

Another solution would be to not regenerate all the numbers when there is a duplicate number:

class GenerateQuestionWhile {
  var num1, num2, num3, num4;

  Random random = Random();
  void generateNum() {
    num1 = random.nextInt(20);
    do {
      num2 = random.nextInt(20);
    } while(num2 == num1);
    do {
      num3 = random.nextInt(20);
    } while(num3 == num2 || num3 == num1);
    do {
      num4 = random.nextInt(20);
    } while(num4 == num3 || num4 == num2 || num4 == num1);
  }
}
Share:
285
Dhananjay Gavali
Author by

Dhananjay Gavali

Updated on December 25, 2022

Comments

  • Dhananjay Gavali
    Dhananjay Gavali over 1 year

    Flutter and Dart. Hey there, I am trying to generate unique random numbers in flutter application. Where I am generating 4 random integers which are the answer options for addition of two random integers. Basically I am generating random addition question. lets say 4 + 5. and I am Giving user 4 options like 2 ,4, 20, 9. I am trying to make options unique and once generated then set it to the Text() widget. But my expression is taking so much time to generate the result that is more than 5 seconds or sometimes 15 to 20. is there any way that I can quickly generate 4 random unique integers? because it is just a 30 second game. and I want to generate answers for next question as soon as user pressed the answer for current question

    import 'dart:math';
    
    void main() {
      GenerateQuestion g = new GenerateQuestion();
      g.generateNum();
    }
    
    class GenerateQuestion {
      var num1, num2, num3, num4;
    
      Random random = new Random();
      void generateNum() {
        num1 = random.nextInt(20);
        num2 = random.nextInt(20);
        num3 = random.nextInt(20);
        num4 = random.nextInt(20);
        bool flag = true;
        while (flag == true) {
          if (num1 != num2 && num1 != num3) {
            if (num1 != num4 && num2 != num3) {
              if (num2 != num4 && num3 != num4) {
                print(num1);
                print(num2);
                print(num3);
                print(num4);
                flag = false;
              }
            }
          } //if condition ends here
    
          else {
            num1 = random.nextInt(20);
            num2 = random.nextInt(20);
            num3 = random.nextInt(20);
            num4 = random.nextInt(20);
            flag = true;
          } // else part ends here
        } // while ends here
      } // generateNum() method ends here
    }
    

    And one more thing. This is just a separate code that I was trying in Dartpad. therefore it has not containing widgets. I am calling the function onPressed of button. but for the first time when Application starts it is showing me values null. yes as function not called it should show null. but How can I also call it before loading the widget and then start working as onPressed?

    • Illegal Argument
      Illegal Argument over 3 years
      Use a set data structure and push random numbers to it until the length is 4.