A really short Javascript scrambler function

I don't like this thread as much as the other code golf one :p

For clarity, here are the rules (as far as I know):
- Random moves: Generate a sequence of 25 moves in standard notation. Turning the same face twice is not allowed. Turning the same face twice with only an opposite-face turn between the two is not allowed.
- Random state: Choose a random solvable position on the cube and output a sequence of moves in standard notation that solves it.

The rules are also listed in the Gist.
 
That may be true, but considering most of this thread is people posting and helping with programs that clearly don't generate working scrambles, they're definitely not being followed or understood.

Yeah, I added the rules as a result of seeing *not-really-functional* scramblers. The entries in the Gist are all tested and work. However, there are only 3 entries, and I'd like more to be put in... Anyway, I did add a warning. It's all on the Gist.

I also updated my scrambler: It's not wrapped in a function anymore, meaning that it works in browser Javascript, and I used console.log as an output. I ended up with 113 bytes, which isn't too bad of a byte raise.

Code:
for(a=s=y=r='',x=Math.random;a++<25;s+=r+["'",2,''][0|x(y=r)*3]+' ')for(;r==y;r='RLUDFB'[0|x()*6]);console.log(s)
 
Yeah, I added the rules as a result of seeing *not-really-functional* scramblers.

You act like you're some kind of savior from bad scramblers when in fact you're one of those bad people, and worse than most, as you're so actively advocating bad scrambling. How many more times does it need to be pointed out to you that your rules are bad and that even your own scrambler is bad?

Code:
for(a=s=y=r='',x=Math.random;a++<25;s+=r+["'",2,''][0|x(y=r)*3]+' ')for(;r==y;r='RLUDFB'[0|x()*6]);console.log(s)

Just tried it, got these:

L' B' U' L' B U' B' D2 L2 D U' B' R U2 D U' F L F' R U2 L' F' L B2
B' F2 L2 D U B2 F' B' F D B D2 F' R D2 L B' D B2 U' L2 F B' U D
 
Last edited:
what is God's number if you require a different axis for each move? how much more than 20?

i know the mainstream superflip alg has some parallel moves.

if you could get a bound on that, it might be much more efficient (and good) in ur codes
 
You act like you're some kind of savior from bad scramblers when in fact you're one of those bad people, and worse than most, as you're so actively advocating bad scrambling. How many more times does it need to be pointed out to you that your rules are bad and that even your own scrambler is bad?



Just tried it, got these:

L' B' U' L' B U' B' D2 L2 D U' B' R U2 D U' F L F' R U2 L' F' L B2
B' F2 L2 D U B2 F' B' F D B D2 F' R D2 L B' D B2 U' L2 F B' U D

Okay, chill. I see what you mean. I wasn't trying to act like a savior, and I sure never said my rules OR scrambler was good...

But thanks for the words of encouragement, though. Love it :) ! It's great to have people criticizing me; it's a great chance for me to improve. Keep it up, Stefan! I sure will need it.

Anyway, I did take the same-axis problem into account. I decided to disregard it (for now) because I wasn't too sure of a way to solve the problem efficiently. So for the sake of code golf, I decided to keep away from that issue until either I figured it out. However, thanks to you, Stefan, I have decided to work on the solution to the same-axis problem. I'll probably have the solution sometime soon? Not too sure. But I'm working on it.
 
Okay, chill.

Sorry, was just really getting frustrating, and apparently yelling was actually necessary to finally get your attention and to get you acknowledge the issue.

I decided to disregard it (for now) because I wasn't too sure of a way to solve the problem efficiently.

That's a really really bad reason. And you at least should've mentioned it instead of weakening the standard and making people who don't know better think it's ok. The publicity is the problem. Nobody would (or could) care if you had kept it to yourself.

I agree that code golf is allowed to simplify things. I'm not requesting that we must use random state scramblers for it, am I? But like Michael said and like you can see from the old thread that was pointed out to you already in the very first response in this thread, this issue is really not difficult.

That said, we could code-golf several categories:
  1. 25 random moves.
  2. 25 random moves, no side turned twice in a row.
  3. 25 random moves, also no trivial cancellations like R L R2 (i.e., what has been the standard for random-moves-scrambles for many years, for good reasons)
  4. random state (i.e., what has been the standard for scrambles for several years now)
But only if those categories are made clear.
 
Last edited:
That said, we could code-golf several categories:
  1. 25 random moves.
  2. 25 random moves, no side turned twice in a row.
  3. 25 random moves, also no trivial cancellations like R L R2 (i.e., what has been the standard for random-moves-scrambles for many years, for good reasons)
  4. random state (i.e., what has been the standard for scrambles for several years now)
But only if those categories are made clear.

That'll work. I'm working on category 3 right now, but splitting into categories is a good idea.
 
Some versions of the script I posted at the start:

category 1:
Code:
<script>r=Math.random;for(j=25;j--;document.write("URFBLD"[m]+" 2'"[0|r()*3]+" "))m=0|r()*6</script>

category 2:
Code:
<script>r=Math.random;for(b=j=25;j;b-m?document.write("URFBLD"[j--,b=m]+" 2'"[0|r()*3]+" "):0)m=0|r()*6</script>
 
Here are some 2x2x2 random-state scramblers. They may be broken due to typing/thinking errors but the concepts probably work.

First approach: permute each piece one by one, then orient each non-DFR piece one by one. DLB stays put. The function called e essentially returns a random element of the array, or an empty string, so we don't have to put empty strings in all of our permute/orient arrays. (353 bytes)
Code:
y=["R","R2","R'","RU","RU2"]
z=[v="L2F2D'L'DF'LD'LD",v+v]
document.write([["F","F2","F'","U'F'","UF2","RF"],x=["R'","R2","R","U'R'","U2R'"],["R'U'R","R'UR","B'U'B","R'U2R"],["U","U2","U'"],[w="R'ULU'RUL'U'",w+w],["U"+w+"U2"]].map(e=(j)=>{return j[0|Math.random()*(1+j.length)]||""}).join("")+[0,1,2,3,4].map((i)=>{return x[i]+e(z)+y[i]}).join("")+e(z))

Second approach: apply a 7-cycle from 0 to 6 times, a twisted 6-cycle (all pieces but DFL) from 0 to 5 times, a twisted 5-cycle (all pieces but DFL and DRB, twists DFL) from 0 to 4 times, etc. (181 bytes)
Code:
for(j=7;j--;)document.write(["UF'UF'RFU2R2F'R'","RU'R'UF2UF2U'F","R'F2RU'FU'F'U2","R'F'R2U2FUF'UR'","U2F2UF2UR'F'RF","F2R2U2RUF","F2R'F'RF2R'UFU2"][j].repeat(3*(j+1)*Math.random()))
 
I guess we need to split category 4 into "reasonable scramble length" and "not reasonable scramble length" :-)

And I can't run your first one. Tried it in Chrome and IE and both complain about a syntax error apparently somewhere in the "e=(j)=>{" and I don't know what to do.
 
Last edited:
I guess we need to split category 4 into "reasonable length" and "not reasonable length" :-)

And I can't run your first one. Tried it in Chrome and IE and both complain about a syntax error somewhere in the "e=(j)=>{" and I don't know what to do.

That's because the code is in ES6 (the new Javascript that's coming out - Note that current browser Javascript is ES5). The error is thrown at the => part, which is an ES6 arrow function syntax. The second script is also written in ES6 because of the String.prototype.repeat function (which is also a part of ES6).

I compiled the code and ran it, and yes - the scrambles do seem a bit long. And there's also the issue of move cancellation...

First code's sample:
Code:
UF2R2B'U'BR'ULU'RUL'U'R'ULU'RUL'U'UR'ULU'RUL'U'U2R'L2F2D'L'DF'LD'LDL2F2D'L'DF'LD'LDRR2L2F2D'L'DF'LD'LDL2F2D'L'DF'LD'LDR2RR'U'R'L2F2D'L'DF'LD'LDRUU2R'RU2

Second code's sample:
Code:
F2R'F'RF2R'UFU2F2R'F'RF2R'UFU2F2R2U2RUFU2F2UF2UR'F'RFU2F2UF2UR'F'RFU2F2UF2UR'F'RFU2F2UF2UR'F'RFU2F2UF2UR'F'RFU2F2UF2UR'F'RFU2F2UF2UR'F'RFR'F'R2U2FUF'UR'R'F'R2U2FUF'UR'R'F'R2U2FUF'UR'R'F'R2U2FUF'UR'R'F'R2U2FUF'UR'R'F'R2U2FUF'UR'R'F'R2U2FUF'UR'R'F'R2U2FUF'UR'R'F2RU'FU'F'U2R'F2RU'FU'F'U2R'F2RU'FU'F'U2RU'R'UF2UF2U'FRU'R'UF2UF2U'FRU'R'UF2UF2U'FRU'R'UF2UF2U'FUF'UF'RFU2R2F'R'
 
Last edited:
Psh, who needs move cancellation? :p And yeah, the ()=>{} syntax is pretty new. It works for me in Firefox. As molarmanful mentions, .repeat() is the same.

Here's a 3x3x3 random state scrambler in 613 bytes. At least, it should be random state :p. It's like the 2x2x2 scrambler but it generates a random corner state (8 algs instead of 7) and then a random edge state (may swap UFR and UFB but it doesn't matter). I'm sure the algs can be improved, especially if you use slice turns.
Code:
for(j=8;j--;)document.write(["UF'R'U2F2LBR'UF'","F'UL'U'L'F'LF2","U'RFU'BR'U2LD'","RU'LDF2L2D'R'F","F2UR'F'L'D'RF'U","U2F'R2UF'DL","RU2F'UL'DBR2B2","FU'RFR'F2R'F'D'F"][j].repeat(3*(j+1)*(r=Math.random)()));for(j=12;j--;)document.write(["F2L2F2UL'RBU2L2U2B'LR'U'","R2FRBF2UL2U'F'LB'F2URU","L'D'B'LU'BDFL'F2L2F","B'RF'D2FRB2F2L2BDB2R2F2","B2LR2D'R'D2L'D'R'BU'B2L'B'","L2B'L2BL2B2L'B'U'F'L'D'L2DLF","B'L2BFU2B2R'D2F'D2R'D2BR2UL2","U2B2U2F2DL'FU'B'ULBF2R'F'R","D2L2B2D2R2U'B2F2LB'L2R2FLB2R2DB2","L2D2BL2B'LDB'L2U'F2L'FU'F'UL'F'","F2R2D'U2L2R2B'F2R2UL2FLF'LFUF'","DB2D2U'F2LBD2LR'DB2U2RUL2U'B"][j].repeat(2*(j+1)*r()))

Stefan, you had a really short solver a while back, right? Think you could turn it into a random-state scrambler?
 
Last edited:
I compiled the code and ran it

Thanks, posting their outputs is very helpful. I note the number of moves: 96 for the first, 229 for the second. That's about 11 and 26 times the average number of moves that 2x2x2 positions need (~8.76 moves).

there's also the issue of move cancellation

For random state scrambling that's ok. It makes them longer, but doesn't affect the distribution quality. In case it's not clear to everybody: Random moves scramblers are biased towards "easy states", so we want at least non--trivially-canceling 25 moves to achieve a somewhat decent distribution. Random state scramblers, by definition, have perfect distribution regardless of how their move sequences look. So cancellations are irrelevant, only the lengths should be considered a secondary criterion.

Stefan, you had a really short solver a while back, right? Think you could turn it into a random-state scrambler?

Maybe. It's been so long that I don't remember how it works and I might even need to relearn some Perl syntax to understand it :-). But here it is, in case you or someone else want to have a look.

How many moves does yours produce on average?
 
Last edited:
And yeah, the ()=>{} syntax is pretty new. It works for me in Firefox.
Yep, Firefox currently is the only browser with ES6 support. I use Safari and Chrome, so I have to compile the code with Traceur.

For random state scrambling that's ok. It makes them longer, but doesn't affect the distribution quality. In case it's not clear to everybody: Random moves scramblers are biased towards "easy states", so we want at least non--trivially-canceling 25 moves to achieve a somewhat decent distribution. Random state scramblers, by definition, have perfect distribution regardless of how their move sequences look. So cancellations are irrelevant, only the lengths should be considered a secondary criterion.

I actually did not know that. Thanks!
 
Back
Top