Title: Generate unique random numbers in Dart
Tags: random;flutter;dart
Question: How can I generate two different random numbers in dart?
I have generated two random numbers using code below.
int rand1 = Random().nextInt(16);
int rand2 = Random().nextInt(16);
if(rand1 == rand2)
// generate new random
How do I generate new random numbers until rand1 != rand2?
Comment: Use `do-while` loop..

Comment: In the most general case when you want N distinct numbers randomly chosen from a set of M (where N < M and M is reasonably small), just fill an array with the M numbers and run N iterations of the Fisher-Yates shuffle. If M is too big for that, use Floyd's algorithm.

Here is the accepted answer: If you need to have two different random numbers in a fixed range, then the simplest approach would be:
var random = Random();
var n1 = random.nextInt(16);
var n2 = random.nextInt(15);
if (n2 &gt;= n1) n2 += 1;
This ensures that the first number can assume all 16 values, and the second number can assume any of the remaining 15 values, and with as even a distribution as the random generator allows.
For two ordered distinct numbers in the range 0..15, there are 16 * 15 possible outcomes, and you probably want each outcome to be equally probable.
This code achieves this by picking the first number at random, and then picking the second number to be one of the numbers different from the first by ensuring that it is in either the range [0 .. (n1 - 1)] or the range [(n1 + 1) .. 15] ... by picking a number in the range [0 .. 14] and adding one if >= n1, shifting the range [n1 .. 14] into [(n1 + 1) .. 15].
You can do this for more numbers, but you have to do more tests and additions.
Comment for this answer: can you explain this in more details?
Comment for this answer: This is similar to Floyd's algorithm, although the latter generalizes better to N>2.

Here is another answer: To generate any 5 unique random numbers under a range 0-49
List<int&gt; numberList=[];
Random random = new Random();
while(numberList.length<5){
int random_number = randomizer.nextInt(50);
if (!numberList.contains(random_number)){
numberList.add(random_number);

Here is another answer: maybe useful for you.
'max' is exclusive. the unique result number is in the return List.
List<int&gt; getRandomNums(int countOfNum, int max) {
int num = 0;
List<int&gt; numList = [];
var random = Random();
int i = 0;
while ( i < countOfNum) {
int oldNum = num;
num = random.nextInt(max);
if (numList.contains(num)) continue;
numList.add(num);
i++;
return numList;

Here is another answer: This is the simplest way to do it:
import 'dart:math';
void main() {
int rand1 = Random().nextInt(16);
int rand2 = Random().nextInt(16);
while(rand1 == rand2){
rand1 = Random().nextInt(16);
rand2 = Random().nextInt(16);
print('$rand1 $rand2');

Here is another answer: A potential solution for pulling many out of many (say, 50 out of thousands) is to loop and memoize with a hash set.
I pulled this from my code, but something like:
var hashSet = HashSet();
for (var i = 0; i < boardSize; i++) {
words.add([]);
for (var j = 0; j < boardSize; j++) {
Random _random = new Random();
String wordToAdd = abstractPossibleWords[_random.nextInt(abstractPossibleWords.length)];
while (hashSet.contains(wordToAdd)) {
wordToAdd = abstractPossibleWords[_random.nextInt(abstractPossibleWords.length)];
words[i].add(wordToAdd);

Here is another answer: I would suggest you a different approach, looping can be pain.
// create a list say of 16 numbers.
List list = List.generate(16, (i) =&gt; i);
// shuffle it
list.shuffle();
// take the numbers now, they are always unique
int firstRandonNum = list[0];
int secondRandonNum = list[1];
Comment for this answer: This is a good choice if the number of desired elements is large relative to the range involved, but is pretty wasteful of resources when the range is large and the number drawn is small.  For this example acceptance/rejection will draw 1 + 16/15 = 2.0666... random numbers on average, while shuffling will require 15 random numbers and swaps.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 6, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:08/26/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<random><flutter><dart>', 'Title': 'Generate unique random numbers in Dart', 'ViewCount': 7488, 'id': 57657287, 'user_AboutMe': None, 'user_AccountId': 8710892.0, 'user_DisplayName': 'Manish Wagle', 'user_Reputation': 180.0, 'user_Views': 58.0, 'user_id': 6517083.0}}