How can I obtain or sample a random Rubiks Cube shuffle?
Method 2 can easily be tweaked to only produce solvable scrambled cubes. Simply make sure that whenever you place a piece in such a way that it violates a constraint, you take it out again and fix it.
-
First mix the 12 edge pieces in any order and orientation, and reassemble them into the cube. If the edge flip constraint is violated, flip the last edge piece you placed (which can be assumed without loss of generality to be the piece at location UF).
-
Mix the 8 corner pieces in any order and orientation, and reassemble them into the cube. If the permutation parity constraint is violated, swap the last two corner pieces you placed (which can be assumed without loss of generality to be the piece at location URF and ULF). If the corner twist constraint is violated, rotate the last corner piece you placed (which can be assumed without loss of generality to be the piece at location URF).
In other words, randomly assemble the cube. If permutation parity is violated, swap the pieces at URF and URL. If edge flip is violated, flip the edge at UF. If corner twist is violated, twist the corner at URF.
It is easy to see that this produces a uniform distribution, because every valid cube position is the result of any of exactly 12 randomly assembled cubes, and you are essentially choosing one of the randomly assembled cubes uniformly.
Represent the group of rotations as a permutation group. From a stabilizer chain you can generate uniformly distributed random elements.
Stabilizer chains are the standard data structure for algorithms with permutation groups, you can find a description for example in my lecture notes https://www.math.colostate.edu/~hulpke/CGT/cgtnotes.pdf