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.
Asked
Active
Viewed 676 times
-5
-
yes there are, but I did not found them helpful. – Arjun Solanki Feb 06 '19 at 12:48
4 Answers
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
-
what is integer list in this example? why not use a map for more efficient lookups? – William Reed Feb 06 '19 at 13:52
-
see the edited code, integerList is a mutable list that will contain the generated random numbers. – Hardik Vegad Feb 07 '19 at 07:34
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
-
This could get unwieldy if the user requires integers from 1 - million. – Steve Smith Feb 06 '19 at 12:56
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
-
-
3In order to improve the quality of your answer, please add some commentary about what this code does and how it solves the original question. – SiKing Feb 06 '19 at 23:27
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