If I assume that the TakenPos array starts out filled with false values (it is unless there is other code involved not posted).
This means that unless Pos never happens to generate the same value twice within about 5 tries (which is a very low probability), this would run forever. Put another way, all entries in TakenPos start out as false, which means any Pos value will satisfy the first test, setting the TakenPos[ Pos ] to true. However, there is a very good likelihood that Pos will end up being set to a value already set to true, at which point j is decremented. As the routine proceeds, however, the probability that Pos produces a value for which TakenPos[ Pos ] is already true approaches 1 (or 100%).
Put another way, it would be a very rare situation where this code doesn’t create a TakenPos array that is all true, and thus defines an infinite loop. That is ensured by the outer loop in ‘i’, because even though j will be reset to zero, TakenPos will already be a list of ‘true’ bools, or so close to it that by the second or third pass through ‘i’, there’s virtually no chance the first test will ever fire again, meaning this will eternally loop with j being decremented, then incremented, then decremented, then incremented - with no possible way out.
What you require is to decide what TakenPos should look like when correctly initialized. I assume that would be somewhere around 1/3 to no more than 2/3rds true entries, and the rest as false entries remaining.
A simple, single loop in j of 6 items would suffice, without decrementing j, and no outer loop in i, merely skipping any repeated Pos entries.
However, this is likely a waste of time. The theory of operation is that only Pos entry duplicates can hope to create non-true results in the result array of TakenPos.
It would be better and more straight forward to establish a volume that is randomly assigned between 2 and 4 (or some other extremes, I merely assume you’d want at least two true entries but no more than 4 entries). Then, loop that volume, creating the random Pos entries for that count, perhaps not even bothering to check if they are already true (that merely continues to randomize the ultimate volume of true entries).