Added assertions to check conditions for exiting while loop

in _raisePermanenceToThreshold method

fixes #1322 
This PR includes two different changes for different issues. It makes it a bit harder. I was fine with the other changes but not fine with the change to use `random`.