lundi 13 juillet 2020

Nested loop and out of memory in dart

I tried to make a simple random number generator program using deduplication codes in nested for loop. But when I tried to run this code on dartpad or android studio, two or three times work, and after that, I got "out of memory" message. Are there any problems on my codes?

import 'dart:math';   
void main(){
  
  List<int> myNum= [];
  var random = Random();
  var num;
  
  for(int i=0; i<6; i++){
    
    num = random.nextInt(45)+1;
    myNum.add(num);
    
    for(int j=0; j<i; j++){
      if(myNum[i]==myNum[j]){
        i--;
        break;
      }
    }
  }
  
  print(myNum);
  
}



Aucun commentaire:

Enregistrer un commentaire