Manish Wagle
Manish Wagle

Reputation: 545

Generate unique random numbers in Dart

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?

Upvotes: 5

Views: 11792

Answers (6)

To generate any 5 unique random numbers under a range 0-49

  List<int> numberList = [];
  Random randomizer = new Random();
  while (numberList.length < 5) {
    int random_number = randomizer.nextInt(50);
    if (!numberList.contains(random_number)) {
      numberList.add(random_number);
    }
  }

Upvotes: 1

Abdulrahman Sarmini
Abdulrahman Sarmini

Reputation: 126

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');
}

Upvotes: 1

Alexweng
Alexweng

Reputation: 458

maybe useful for you.

'max' is exclusive. the unique result number is in the return List.

List<int> getRandomNums(int countOfNum, int max) {
    int num = 0;
    List<int> 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;
  }

Upvotes: -1

Matt Takao
Matt Takao

Reputation: 3006

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);
  }
}

Upvotes: 0

lrn
lrn

Reputation: 71838

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 >= 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.

Upvotes: 8

CopsOnRoad
CopsOnRoad

Reputation: 268404

I would suggest you a different approach, looping can be pain.

// create a list say of 16 numbers. 
List list = List.generate(16, (i) => i);

// shuffle it 
list.shuffle();

// take the numbers now, they are always unique
int firstRandonNum = list[0];
int secondRandonNum = list[1];

Upvotes: 8

Related Questions