"while" can be very useful to use. No reason to avoid it. You just need to make sure you're not causing an infinite loop by using it.
The method I proposed was to choose a random number and if it's either of the last two values it tries again. The only drawback is it could have the chance of looping a lot if one of the last values gets picked over and over again, but that is a slim chance in this case.
The method korbaach suggests is to have a list of all the numbers except the two last and choose one of them. Arrays could be used to do it or even text like the following.
number cur=0
number last1=0
number last2=0
text list=""
on click
--- set last2 to last1
--- set last1 to cur
--- set list to replace(replace("123456", str(last1), ""), str(last2), "")
--- set cur to int(mid(list,int(random(len(list))),1))