lundi 11 janvier 2021

Some random thoughts on algorithm to create positive random number and time complexity

I was just browsing some great posts here and there online and just started wondering if there is an algorithm that generates random positive numbers, ranging from 0-infinite.

I've seen the Fisher-Yates algorithm that creates an array of numbers in order and shuffles within O(n).

Just out of curiosity, what would be the time complexity of the following pseudocode?

ranNumGenerator(int n) {
repeat until you get n random numbers {
  create randNumArray
  generate a random positive number (range 0-largest integer available)
  if !(randNumArray contains the generated number)
    add to randNumArray
  }
}

I can't seem to figure out what would be its time complexity, neither how I would make it better and more efficient.




Aucun commentaire:

Enregistrer un commentaire