Is there a way to pick random numbers in a fashion that, never ever will the same number be returned to me twice in succession.
And everytime there's an algorithm behind a random number, that random number is not random because it's predictable from the algorithm (in theory).
In almost all cases this is enough, but it's not 100% random only 99.999%
Marco Alka
Software Engineer, Technical Consultant & Mentor
You have to differentiate between random number generators (RNGs):
Talking about TRNGs, you can easily program such a RNG, but the most important part is a hardware device which generates an unpredictable input to the RNG. One choice of device is a sensor for EM waves from space, which tend to be quite uncalculable. It might yield sequences of the same number, however that's only natural and secure, as you have the same number of possible results for every new random number!
In the world of computers, we only have PRNGs, which take the hash of stuff, like time, current CPU temperature, current CPU boost speed, etc. (depending on the implementation) as input to generate a sequence of random numbers. Most libraries in use today are quite good at it and qualify as CSPRNGs. However, no matter how secure they are, they will always just calculate numbers. There is nothing random in calculating numbers.
One of the problems is that most input parameters in todays regular computers are quite predictable. Let's take a PRNG on a server we want to attack which takes the above mentioned input parameters. The time is known. An attacker might find out about the used server hardware and infrastucture and manipulate it to have a load of 100%. Then the CPU will have a predictable temperature and a predictable boost speed (with minimal deviations). So we can feed our local algorithm with the input values we predict and get the same "random" number the PRNG on the server would calculate. That prediction can be used to break every known cryptographic algorithm which works with a generated key. In reality, it's not that easy, of course ;)
In order to get "random numbers" which don't repeat the same number twice in a row, you would have to manipulate a PRNG. There is no implementation available (because it's a trivial special case), but you can build one yourself by caching the previously generated number, and if the next number is the same as the one in the cache, have the RNG generate a new number. However, that compromises security, because the random number becomes even more predictable (a number cannot occur twice in a row)
The math for that:
// Sample implementation in JS; Math.random() is not a CSPRNG function manipulatedPRNG(min, max) { let rn; do { // if you only want to return integers, // you will have to use Math.round() here rn = Math.random() * (max - min) + min; } while (manipulatedPRNG._cachedRN === rn); manipulatedPRNG._cachedRN = rn; return rn; }