-1

The algorithm is as follows:

a = rand  % a random number between 0 and 1
b = a
while b == a
    b = rand
end

Here rand is a function that returns a random number, generated uniformly, between 0 and 1. Let us say that this is the MATLAB function rand.

What is the time complexity of this algorithm?

It looks like the best and average complexities are $O(1)$ and the worst complexity is unbounded.

Raphael
  • 73,212
  • 30
  • 182
  • 400
drzbir
  • 1,000
  • 9
  • 22

1 Answers1

2

rand () is probably a random number generator, but the details are unknown. Since we don't know how exactly rand () behaves we don't know how long this will take.

I would assume that with many random number generators it is possible to prove that rand () will not be called more than three times (that is there is no state where rand () would return the same value three times in a row). Obviously if you use something like Mersenne Twister with thousands of bits of state, a rather lengthy sequence of identical numbers will happen in some states.

gnasher729
  • 32,238
  • 36
  • 56