That's some goofy looking code, OP.
(Why is the function called
fib when there's nothing Fibonacci-esque going on? What is that unused
n parameter supposed to be for? Why are you checking if something is present in
elements2, but only ever appending things to
elements? etc.)
There's an off-by-one error in the loop count (that is, if you meant for
every integer, between, and including, 10000 and 20000 to be in the
elements list, then you should be waiting for its length to be 10001, rather than 10000; as it stands now,
elements will always end up without one random integer from that range). Alternatively, if you
did actually only want
elements to have 10000 entries, then you should be picking your random numbers like this:
random.randint(10000, 19999).
If all you meant to accomplish was to produce a shuffled list of every integer in
range(10000, 20000), then it's much faster (~750x on my machine, compared to your script), and much clearer, to just do the following:
import random
elements = [x for x in range(10000, 20000)]
random.shuffle(elements)
Your second script is harder to make sense of (because you're testing the contents of
elements2 without ever actually appending anything to it:
elements2 is empty when the script starts, and is still empty by the time the script ends). If I fix that problem, your script still doesn't make much sense, because then the loop termination logic (which only considers
elements) will often leave
elements2 with either too few, or (one) too many entries.
Because you seemed surprised that your second script took longer to execute than your first, I can only assume that what you were trying to do was to run a 10000-iteration loop that left
elements with 5000 random integers from
range(10000, 20000) and
elements2 with 5000 random integers from
range(20000, 30000). If that's what you were aiming at, then this should do the trick:
import random
elements = random.sample(range(10000, 20000), k=5000)
elements2 = random.sample(range(20000, 30000), k=5000)
(I know I'm bumping an old topic; I'm catching up on unread stuff.)