0

I would like to ask what is the best way to generate five UNIQUE random numbers (0-99).

I'm only familiar with generating a random number like this:

var randomizer = Random();

var num = randomizer.nextInt(100);
print(num);

Is there a way how to easily generate 5 different random numbers?

1
  • That is the best way of generating random numbers Commented May 25, 2020 at 14:14

2 Answers 2

4

I think the simplest way to get five unique numbers from 100 is:

void main() {
  var list = List<int>.generate(100, (i) => i)..shuffle();
  list = list.take(5).toList();
  print(list);
}

we generate list with elements, then shuffle it and take first five elements

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

Comments

4

If you meant to generate list of 5 random numbers.

You can

var randomizer = new Random();
var l = List.generate(5, (_) => randomizer.nextInt(100));

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.