I wanna create unique random numbers with size of m
and range of 0 to n-1
. I think there are 2 traditional ways to do this as below.
val m = 1000
val n = 100
val set = mutableSetOf<Int>()
while (set.size < m) {
val v = Random.nextInt(n)
set.add(v)
}
val m = 1000
val n = 100
val set = (0..m).shuffled().subList(0, n).toSet()
Both are good ways to get unique random numbers, but they need some storage to check if numbers are unique or not.
However, I need to generate unique random numbers without any space, because the m
or n
would be quite big(e.g. a billion). And note that the generated numbers do not have to be same every time the code is executed.(like Random
with seed). Is there any algorithm for this ?
Aucun commentaire:
Enregistrer un commentaire