Here is some code that I use to generate random 3x3x3 cube states. It is

written in Objective C which you may be familiar with if you're working in iOS.

Everything is fairly generic until the end where I translate the position and

orientation info into my working cube representation.

(This editor strips out the tabs. I hope this isn't too hard to read.)

**// Generate a Random Cube State**

**// The cubies are placed at random positions **

**// and orientations within the restrictions**

**// of parity.**

enum RM_Cubicle { UF, UR, UB, UL, DF, DR, DB, DL, FR, FL, BR, BL, UFR, URB, UBL, ULF, DRF, DFL, DLB, DBR};

typedef enum RM_Cubicle RM_Cubicle;

enum { RM_TWIST_NO = 0 , RM_TWIST_CW , RM_TWIST_CCW };

-(NSData *)randomState

{

uint8 state[20];

uint64 rNumber;

uint8 perm[20],

orient[20],

offset[20],

twist[3] = { 0 , 0 , 0 };

int i, n, p,

range,

cubie;

BOOL done;

for( i = 0 ; i < 20 ; i++ )

perm*[ i ] = UINT8_MAX;*

** //assign the edges random orientations: 0 thru 1, 0 = no flip**

** **

rNumber = [self random64]; //64 random bits from random device

for( cubie = UF , orient[BL] = 0 ; cubie < BL ; cubie++ )

{

orient[cubie] = rNumber & 1;

rNumber >>= 1;

orient[BL] ^= orient[cubie]; //the orientation of the last cubie is constrained

}

//assign the corners random orientations: 0 thru 2,

for( cubie = UFR ; cubie < DBR ; cubie++ )

{

orient[cubie] = rNumber % 3;

rNumber /= 3;

twist[ orient[cubie] ]++;

}

** //Balance the twist with the DBR cubie**

** **

twist[RM_TWIST_CW] %= 3;

twist[RM_TWIST_CCW] %= 3;

orient[DBR] = RM_TWIST_NO;

if( twist[RM_TWIST_CW] > twist[RM_TWIST_CCW] )

{

twist[RM_TWIST_CW] -= twist[RM_TWIST_CCW];

if( twist[RM_TWIST_CW] == 1 )

orient[DBR] = RM_TWIST_CCW;

else

orient[DBR] = RM_TWIST_CW;

}

else

{

if( twist[RM_TWIST_CCW] > twist[RM_TWIST_CW] )

{

twist[RM_TWIST_CCW] -= twist[RM_TWIST_CW];

if( twist[RM_TWIST_CCW] == 1 )

orient[DBR] = RM_TWIST_CW;

else

orient[DBR] = RM_TWIST_CCW;

}

}

//generate random offsets in a list of unoccupied cubicles

rNumber = [self random64];

for( cubie = UF , range = 12 ; cubie < UFR ; cubie++ , range-- )

{

offset[cubie] = rNumber % range; // 0 thru 11 , 0 thru 10 , 0 thru 9 ...

rNumber /= range;

}

rNumber = [self random64];

for( cubie = UFR , range = 8 ; cubie < 20 ; cubie++ , range-- )

{

offset[cubie] = rNumber % range;

rNumber /= range;

}

//place the cubies at random positions based on the above offsets

for( cubie = 0 ; cubie < 20 ; cubie++)

{

done = NO;

i = 0;

range = 0;

do

{

if( perm* == UINT8_MAX )*

* {*

* if( range == offset[cubie] )*

* {*

* perm** = cubie;*

* done = YES;*

* }*

* range++;*

* }*

* i++;*

* }while( done == NO );*

* }*

** // rectify the parity if necessary**

** **

** // Count inversions**** **

for( p = 0 , i = 0 ; i < 19 ; i++ )

for( n = i + 1 ; n < 20 ; n ++ )

if( perm* > perm[n] )*

* p ^= 1;*

* if( p == 1 )*

* {*

* i = perm[19];*

* perm[19] = perm[18];*

* perm[18] = i;*

* }*

** //translate the permutation and orientations to a list of geometric transforms**

** //to apply to the cubies**

** **

for(cubie = 0 ; cubie < 20 ; cubie++ )

state[cubie] = [self symTagToPlaceCubie: cubie

inCubicle: perm[cubie]

withOrientation: orient[cubie] ];

return [NSData dataWithBytes: state length: sizeof(CM_SYMTAG [20])];

}