A really short Javascript scrambler function

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).
 
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.
 
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:
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:
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.
 
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.
 
Back
Top