function rand ( option ) {

return option [ Math . floor ( Math . random ( ) * option. length ) ] ;

}

function genScramble ( ) {

var opposite = new Object ( ) ;

// Defines opposites of moves so things like "R L' R2"

// Don't show up in the final scramble

opposite [ "R" ] = "L" ; opposite [ "L" ] = "R" ;

opposite [ "U" ] = "D" ; opposite [ "D" ] = "U" ;

opposite [ "F" ] = "B" ; opposite [ "B" ] = "F" ;

// Defines moves as array

// Alter makes moves normal (R), inverted (R'), or double (R2)

var alter = [ "" , "'" , "2" ] ;

// firstPart and secondPart are two parts of the scramble

// firstPart has all double (R2) moves except for U and D face

// secondPart is simply random moves

var firstPart = 10 ; var secondPart = 12 ;

// before is the last move, beforeThat is the move before that

var before ; var beforeThat ;

// finalArray is the scramble itself, before it becomes a string

var finalArray = [ ] ;

// Loops 10 times to create first part of scramble

while ( firstPart > 0 ) {

// Possibilities

var poss = [ "R" , "L" , "U" , "D" , "F" , "B" ] ;

// Removes last move from poss so "R' R2" is impossible

poss. splice ( poss. indexOf ( before ) , 1 ) ;

if ( beforeThat == opposite [ before ] ) {

poss. splice ( poss. indexOf ( beforeThat ) ) ;

}

// Defines var move as random choice from poss

// If move is *undefined* (preceded by "R L") redefines it.

// I don't know how to fix that glitch pls send help

var move = rand ( poss ) ;

if ( move == undefined ) {

move = rand ( [ "U" , "D" , "B" , "F" ] ) ;

}

// Redefines beforeThat and before to be what they describe

beforeThat = before ;

before = move ;

// If move is U or D, appends either an empty string, ', or 2

if ( move == "U" || move == "D" ) {

move = move. concat ( rand ( alter ) ) ;

}

// Otherwise, append 2

else {

move = move. concat ( alter [ 2 ] ) ;

}

// Add value of string move to finalArray

finalArray. push ( move ) ;

firstPart -= 1 ;

}

// Remove undefined element at end of finalArray

finalArray. splice ( 10 ) ;

while ( secondPart > 0 ) {

// Possibilities

var poss = [ "R" , "L" , "U" , "D" , "F" , "B" ] ;

// Removes last move from poss so "R' R2" is impossible

poss. splice ( poss. indexOf ( before ) , 1 ) ;

if ( beforeThat == opposite [ before ] ) {

poss. splice ( poss. indexOf ( beforeThat ) ) ;

}

// Defines var move as random choice from poss

// If move is *undefined* (preceded by "R L") redefines it.

// I don't know how to fix that glitch pls send help

var move = rand ( poss ) ;

if ( move == undefined ) {

move = rand ( [ "U" , "D" , "B" , "F" ] ) ;

}

// Redefines beforeThat and before to be what they describe

beforeThat = before ;

before = move ;

move = move. concat ( rand ( alter ) ) ;

finalArray. push ( move ) ;

secondPart -= 1 ;

}

return finalArray. join ( " " , 1 ) ;

}