rand(1,N) but excluding array(a,b,c,..),
is there already a built-in function that I don’t know or do I have to implement it myself(how?) ?
UPDATE
The qualified solution should have gold performance whether the size of the excluded array is big or not.
No built-in function, but you could do this:
That’s off the top of my head, so it could use polishing – but at least you can’t end up in an infinite-loop scenario, even hypothetically.
Note: The function breaks if
$exceptionsexhausts your range – e.g. callingrandWithout(1, 2, array(1,2))orrandWithout(1, 2, array(0,1,2,3))will not yield anything sensible (obviously), but in that case, the returned number will be outside the$from–$torange, so it’s easy to catch.If
$exceptionsis guaranteed to be sorted already,sort($exceptions);can be removed.Eye-candy: Somewhat minimalistic visualisation of the algorithm.