Generate a random number up to n-1 and add it on, modulo the original range (shifting because the min is not 0):
i = some random int from 2 to 5 delta = randInt(3) // range of possible values from 2 to 5 is 4, minus 1 to // prevent getting all the way round to i again nextval = (i-2+delta)%4+2 // shift i down by the minimum, add the // delta and modulo the range
This works because it adds up to 1 below the range, so it can never get back to the original number. For example, i=3, random int 0 to 2, so the max is (i-2+2)%3+2=3%3+2=0+2=2.
function differentRandInt(min,max,current) { var shiftedcurrent = current-min; var range = max-min+1; var delta = Math.floor((Math.random()*(range-1))+1); return (shiftedcurrent + delta)%range + min; }
So if i=3, then i-min is 1, the range after adding the delta is 2,3,4, modulo 4 yielding 2,3,0, so adding the min gives us 4,5,2.