-5

I have tried some solutions from the forum but did not worked for me, if the answer be specific in kotlin language then it will me more helpful for me.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Arjun Solanki
  • 236
  • 1
  • 11

4 Answers4

6

You can try this out with recursion function that will only return unique random number in the range of 0 to 6.

private var randomNumber: Int = 0
private var integerList: MutableList<Int>? = null

private fun getRandomNumber(): Int {
    val rand = Random()
    randomNumber = rand.nextInt(7)
    if (integerList!!.contains(randomNumber)) {
        getRandomNumber()
    } else {
        integerList!!.add(randomNumber)
    }
    return randomNumber
}
Hardik Vegad
  • 111
  • 1
  • 7
1

As Andrew Thompson say here you can

  • Add each number in the range sequentially in a list
  • Shuffle it
  • Take the first 'n' numbers from the list

A simple implementation would be:

import java.util.ArrayList;
import java.util.Collections;

public class UniqueRandomNumbers {

    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        for (int i=1; i<11; i++) {
            list.add(new Integer(i));
        }
        Collections.shuffle(list);
        for (int i=0; i<3; i++) {
            System.out.println(list.get(i));
        }
    }
}

This would print 3 unique random numbers from 1 to 10

MarioBerrios
  • 19
  • 1
  • 4
0
int[] Numbers;
int num = 0;

getRandomNum();

public void getRandomNum()
{
   Random rand = new Random(10);
   if(Numbers.contains(rand))
   {
      Log.i("console","Try Again");
   }
   else
   {
      num = rand;
      Numbers.add(rand); 
   }
}
mohamad sheikhi
  • 394
  • 5
  • 16
0

First of all I wouldn't recommend a function for that, rather use a class that encapsulates the logic which increases usability.

1st option

You can use the List function (yes, it's really a function here) to generate a list of all possible numbers, then shuffle it and after that iterate over it.

class UniqueRandomNumbers(lowerBound: Int, upperBound: Int) {
    private val iterator = List(upperBound - lowerBound) { 
        lowerBound + it + 1
    }.shuffled().iterator()
    val next get() = if (iterator.hasNext()) iterator.next() else null
}

2nd option

Alternatively you could generate random numbers until you find one that has not been consumed before.

class UniqueRandomNumbers(val lowerBound: Int, val upperBound: Int) {
    private val consumedNumbers = mutableSetOf<Int>()
    val next: Int?
        get() {
            var newNumber: Int

            if (consumedNumbers.size == (upperBound - lowerBound + 1)) {
                return null
            }

            do {
                newNumber = Random.nextInt(lowerBound, upperBound + 1)
            } while (newNumber in consumedNumbers)

            consumedNumbers += newNumber

            return newNumber
        }
}

Usage in both cases:

val numberProvider = UniqueRandomNumbers(0, 2)

// returns 0, 1, 2 or null if all numbers have been consumed
numberProvider.next 
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121