flutterdartrandomnon-repetitive

Create 6 Distinctive Random Numbers


Create 6 Distinctive Random Numbers using Flutter and Dart to make an app. Where these 6 different numbers don't repeat themselves when they are randomize

Although I used this

import 'dart:math';

void main() {
  List<int> values = [];

  while (values.length < 6) {
    int randomValue = Random().nextInt(50);

    if (!values.contains(randomValue)) {
      values.add(randomValue);
    }
  }

  print('Randomized values: $values');
}

But I can't display it on the text widget in my app

And also I tried creating an elevated button so that when I click it, the figures change


Solution

  • You can use Set instead of List.

    void main() {
      Set<int> values = {};
    
      while (values.length < 6) {
        int randomValue = Random().nextInt(50);
    
        if (!values.contains(randomValue)) {
          values.add(randomValue);
        }
      }
    
      print('Randomized values: $values');
    }
    

    This can take quite a time based on random generation.