# A really short Javascript scrambler function

#### rokicki

##### Member
I recently got into code-golfing, and I decided to JS-golf a 3x3 scrambler. The Gist is at https://gist.github.com/molarmanful/2f299ae96f6120cf5283. What do you guys think? Any ideas for saving more bytes?
Code golf is great fun! How many bytes would you need to *add* to make it be a proper random-state
scrambler with reasonably short result sequences?

#### Lucas Garron

##### Member
Code golf is great fun! How many bytes would you need to *add* to make it be a proper random-state
scrambler with reasonably short result sequences?
This is actually a great question.

My first instinct is to decompose the cube into Thistlethwaite-like steps, and store a table to randomize each subgroup in order. Unfortunately, I'm not aware of a decomposition that's likely to lead to something short enough that a scrambler would be okay using it instead of our current high-quality scrambles (say, under 30 moves).

#### qqwref

##### Member
Code:
<script>r=Math.random;for(c=b=j=25;j;c+b-5|c-m&&b-m?document.write("URFBLD"[j--,c=b,b=m]+" 2'"[0|r()*3]+" "):0)m=0|r()*6</script>

#### rokicki

##### Member
This is actually a great question.

My first instinct is to decompose the cube into Thistlethwaite-like steps, and store a table to randomize each subgroup in order. Unfortunately, I'm not aware of a decomposition that's likely to lead to something short enough that a scrambler would be okay using it instead of our current high-quality scrambles (say, under 30 moves).
Machines are fast enough these days so you can do a full two-phase solver
including generating a partial table for each phase and get results pretty
quickly. Quick enough? Maybe. Is a few seconds fast enough? You don't
really need that much of either table to do a reasonable job, as Kociemba
proved with his Atari ST solver.

And two-phase would simplify things over Thistlethwaite, as well as
guarantee good solutions.

You're still probably talking a kilobyte of compressed code, though.

Maybe I should start another "short cube solving contest" with slightly
different metrics to see what people can come up with.

#### Calode

##### Member
I had a go at it, learned a few things from you. My attempt (129 bytes):
Code:
function(){a=l=[];r=Math.random;while(l++<25)a.push('RLUDFB'.replace(a[l-2],'')[0|r()*5]+["'",2,''][0|r()*3]);return a.join(' ')}
Or expanded:
Code:
function(){
a=l=[];
r=Math.random;
while(l++<25)
a.push('RLUDFB'.replace(a[l-2],'')[0|r()*5]+["'",2,''][0|r()*3]);
return a.join(' ')
}
I love your clever use of taking advantage of the update part of the for loop to add onto the generated move. Instead, I took an approach where I remove the previous move of the possible moves to generate. I still feel like I could make it smaller.

edit: just realized my code for checking the previous move doesn't even work. I fixed it and now it's 132 characters:
Code:
 function(){a=p=l=[];r=Math.random;while(l++<25)a.push((p="RLUDFB".replace(p,'')[0|r()*5])+["'",2,''][0|r()*3]);return a.join(' ')}()
All I did was add p and set p to the previous move.

Last edited:

#### molarmanful

##### Member
I had a go at it, learned a few things from you. My attempt (129 bytes):
Code:
function(){a=l=[];r=Math.random;while(l++<25)a.push('RLUDFB'.replace(a[l-2],'')[0|r()*5]+["'",2,''][0|r()*3]);return a.join(' ')}
Or expanded:
Code:
function(){
a=l=[];
r=Math.random;
while(l++<25)
a.push('RLUDFB'.replace(a[l-2],'')[0|r()*5]+["'",2,''][0|r()*3]);
return a.join(' ')
}
I love your clever use of taking advantage of the update part of the for loop to add onto the generated move. Instead, I took an approach where I remove the previous move of the possible moves to generate. I still feel like I could make it smaller.

edit: just realized my code for checking the previous move doesn't even work. I fixed it and now it's 132 characters:
Code:
 function(){a=p=l=[];r=Math.random;while(l++<25)a.push((p="RLUDFB".replace(p,'')[0|r()*5])+["'",2,''][0|r()*3]);return a.join(' ')}()
All I did was add p and set p to the previous move.
Nice! Little tip: you can save even more bytes by using for instead of while loops.

As of this post, the byte count for my Gist is at 127.

Last edited:

#### Calode

##### Member
I realized as I tried optimizing it more (getting rid of .push, now 129 bytes), that I keep getting closer to your code.
At first, I tried using Array.map and get rid of the loops but as I tried I found out that it was hard so I ended up using a while loop and it kept getting closer to yours.

#### biscuit

##### Member
I've made an example page at this link if people want to test it out:

https://jsfiddle.net/sz8jw62t/2/
pretty good but with your code it allows "useless" moves. Here is a scramble generated with your code. U R2 D' L' D' L R D L R2 F' U2 D B R' B2 F2 B2 F D2 F D2 R2 L' U2

As you can see there is a B2 F2 B2 which is a bad thing.

#### molarmanful

##### Member
Code:
<script>r=Math.random;for(c=b=j=25;j;c+b-5|c-m&&b-m?document.write("URFBLD"[j--,c=b,b=m]+" 2'"[0|r()*3]+" "):0)m=0|r()*6</script>
Wait a sec... Not counting the script tags, you just wrote a 112-byte scrambler. Nice! I'll add this to the Gist in a separate file.

#### molarmanful

##### Member
pretty good but with your code it allows "useless" moves. Here is a scramble generated with your code. U R2 D' L' D' L R D L R2 F' U2 D B R' B2 F2 B2 F D2 F D2 R2 L' U2

As you can see there is a B2 F2 B2 which is a bad thing.
Oh, I completely forgot to take that into account! Well, back to coding...

#### biscuit

##### Member
The set objective for this code golf is to output a scramble right?

<!DOCTYPEhtml><body><p>DRL2BD2F2R2B2F2D'UBD'F2UF'U'D'FDB'F2R2L'R'</P></body> (76 characters by my count)

No? Ok

#### samuelqwe

##### Member
pretty good but with your code it allows "useless" moves. Here is a scramble generated with your code. U R2 D' L' D' L R D L R2 F' U2 D B R' B2 F2 B2 F D2 F D2 R2 L' U2

As you can see there is a B2 F2 B2 which is a bad thing.
I didn't make the scrambler, I just made an example out of the existing one found in this thread.

#### samuelqwe

##### Member
The set objective for this code golf is to output a scramble right?

<!DOCTYPEhtml><body><p>DRL2BD2F2R2B2F2D'UBD'F2UF'U'D'FDB'F2R2L'R'</P></body> (76 characters by my count)

No? Ok
The only problem here is, it's not "Random".

#### qqwref

##### Member
Wait a sec... Not counting the script tags, you just wrote a 112-byte scrambler. Nice! I'll add this to the Gist in a separate file.
Oh, I didn't write that just now, that's a solution from the previous thread (from 2010). I have that entire thing saved as a working .htm file, and both Firefox and Chrome are nice enough to run it even without <html>/<head>/<body> tags.

#### Stefan

##### Member
The set objective for this code golf is to output a scramble right?

<!DOCTYPEhtml><body><p>DRL2BD2F2R2B2F2D'UBD'F2UF'U'D'FDB'F2R2L'R'</P></body> (76 characters by my count)

No? Ok
Beat you by one byte!

#### biscuit

##### Member

arg... I mean the scramble was randomly generated. If I could find a smaller (random) scramble would I win?