A really short Javascript scrambler function

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

Well, if you enter a submission that's short enough, then I can put it on my gist.

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.

Oh yeah, I put your code on the Gist. I also have a suggested modification that puts you at 127 bytes:

Code:
function s(){for(a=p=l=[],r=Math.random;l++<25;)a.push(p="RLUDFB".replace(p,'')[0|r()*5]+["'",2,''][0|r()*3]);return a.join(' ')}

- Changed while to for (no bytes directly saved)
- Took out parentheses between the ( and the p and between the ] and + (2 bytes)
- Put variable declarations inside for loop (1 byte)
This isn't the code I have on the Gist, but let me know if you want me to switch you to this version.
 
Last edited by a moderator:
Okay, I lowered my code's byte count to 112 bytes!!! Sadly, my edits make the code only compatible with ES6+. The source is on the gist, but I'll put it here for reference:

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]);return s}
 
Last edited:
Oh yeah, I put your code on the Gist. I also have a suggested modification that puts you at 127 bytes:

Code:
function s(){for(a=p=l=[],r=Math.random;l++<25;)a.push(p="RLUDFB".replace(p,'')[0|r()*5]+["'",2,''][0|r()*3]);return a.join(' ')}

- Changed while to for (no bytes directly saved)
- Took out parentheses between the ( and the p and between the ] and + (2 bytes)
- Put variable declarations inside for loop (1 byte)
This isn't the code I have on the Gist, but let me know if you want me to switch you to this version.

Won't taking out the parentheses assign p to "RLUDFB".replace(p,'')[0|r()*5] + ["'",2,''][0|r()*3] rather than just the first part like I want?

Just tested it: yes. It did what I didn't want it to.
I wanted it to evaluate p = the move, then add the move and the ' , 2 or nothing.


Okay, I lowered my code's byte count to 112 bytes!!! Sadly, my edits make the code only compatible with ES6+. The source is on the gist, but I'll put it here for reference:

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]);return s}

I was going to try making one with io.js but couldn't figure out how to run it. And then learning the tricks would take some time. I felt a bit lazy. Is it just replacing function with arrow notation that you did?

edit: I don't know how to use forums so until someone posts again, I'll keep editing this post

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(' ')}

I brought it down to 125!!

and 124 if you replace while with for
Code:
function(){for(a=p=l=[],r=Math.random;l++<25;)a.push((p='RLUDFB'.replace(p,'')[0|r()*5])+" '2"[0|r()*3]);return a.join(' ')}

basically: replacing the ["'",2,''] with " '2" to remove 4 bytes
 
Last edited:
Won't taking out the parentheses assign p to "RLUDFB".replace(p,'')[0|r()*5] + ["'",2,''][0|r()*3] rather than just the first part like I want?

Just tested it: yes. It did what I didn't want it to.
I wanted it to evaluate p = the move, then add the move and the ' , 2 or nothing.




I was going to try making one with io.js but couldn't figure out how to run it. And then learning the tricks would take some time. I felt a bit lazy. Is it just replacing function with arrow notation that you did?

edit: I don't know how to use forums so until someone posts again, I'll keep editing this post

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(' ')}

I brought it down to 125!!

and 124 if you replace while with for
Code:
function(){for(a=p=l=[],r=Math.random;l++<25;)a.push((p='RLUDFB'.replace(p,'')[0|r()*5])+" '2"[0|r()*3]);return a.join(' ')}

basically: replacing the ["'",2,''] with " '2" to remove 4 bytes

For me, I switched from using an array to just using a string to do all the work. That eliminated the need to use .join(). Then I added the arrow function, which cut out the need to write out 'function.'
 
Neither are yours (or anyone's, truly, right?).

That's said, this is interesting so let's keep this to actual solutions and not a bunch of useless circle-jerking.

Actually it is random because I used a random scrambler to pick the entire set of moves. But you're right it's pointless and unproductive so let's just move on.
 
My 4 minute attempt in Python:

123 bytes;
93 chars no spaces;
117 chars spaces;

Really easy to write and follow, although uses unorthodox notation; U+ = U; U- = U':


Code:
import random as r
c = 'UDLRFB'
m = '-2+'
s = ""
for i in range(0, 25):
    s += (r.choice(c) + r.choice(m) + " ")
print(s)

output:
Code:
L2 U- R- D- F+ B- B2 D- F- R2 R+ F- R- U+ U+ U+ R+ U+ B2 B- B2 B- U- U2 U+
 
We're doing python? Sweet!

Code:
import random as r;' '.join([('UDRLFB'[r.randint(0,5)]+" '2"[r.randint(0,2)]) for i in range(25)])

Exactly as it is, it is 98 bytes. It does not however prevent the same moves being consecutive. I'll be working on that now.

Got yours down to 83 bytes (82 characters, the new line is required before the for statement apparently):

Code:
import random as r;s="";
for i in range(25):s+=(r.choice('UDRLFB')+r.choice('-2+')+" ");

Just removed the declarations of c and m and moved them inside of the choices.

A simple edit of yours brings it up to to 102 bytes but keeps track of previous move:

Code:
import random as r;s="";
for i in range(25):p=r.choice('UDRLFB'.replace(p,''));s+=p+r.choice('-2+')+' ';

Where p is the previous move. Replaces the previous move with '' removes the previous move as a possible choice.

Used choice for mine and brought it down to 86 bytes:

Code:
import random as r;' '.join([(r.choice('UDRLFB')+r.choice(" '2")) for i in range(25)])
 
Last edited:
We're doing python? Sweet!

Code:
import random as r;' '.join([('UDRLFB'[r.randint(0,5)]+" '2"[r.randint(0,2)]) for i in range(25)])

Exactly as it is, it is 98 bytes. It does not however prevent the same moves being consecutive. I'll be working on that now.

Got yours down to 83 bytes (82 characters, the new line is required before the for statement apparently):

Code:
import random as r;s="";
for i in range(25):s+=(r.choice('UDRLFB')+r.choice('-2+')+" ");

Just removed the declarations of c and m and moved them inside of the choices.

A simple edit of yours brings it up to to 102 bytes but keeps track of previous move:

Code:
import random as r;s="";
for i in range(25):p=r.choice('UDRLFB'.replace(p,''));s+=p+r.choice('-2+')+' ';

Where p is the previous move. Replaces the previous move with '' removes the previous move as a possible choice.

Ahh, thanks alot, it has been a while sine I have been programming, that edit is nice, I was about to start work on the exact same thing but qqTimer LSLL.
BTW, does having a newline increase the size of the file?
 
I actually have a̶l̶m̶o̶s̶t̶ no idea how random-state scramblers work,̶ ̶o̶t̶h̶e̶r̶ ̶t̶h̶a̶n̶ ̶y̶o̶u̶ ̶g̶e̶n̶e̶r̶a̶t̶e̶ ̶a̶ ̶2̶5̶-̶m̶o̶v̶e̶ ̶s̶c̶r̶a̶m̶b̶l̶e̶ ̶r̶a̶n̶d̶o̶m̶l̶y̶,̶ ̶t̶h̶e̶n̶ ̶s̶o̶l̶v̶e̶ ̶i̶t̶ ̶i̶n̶ ̶f̶e̶w̶e̶r̶ ̶m̶o̶v̶e̶s̶.̶

Fixed that for you.

Your idea would produce poor quality scrambles and hide the fact that they're poor quality! That's possibly the worst scrambler idea ever.

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?

I just ran it, first scramble contained L' R L' R', which trivially cancels to L2.

Try again.

Same goes for everybody else who doesn't prevent such stuff. Please don't try to weaken what has been the random-moves standard for many years.

Code:
import random as r;s="";
for i in range(25):s+=(r.choice('UDRLFB')+r.choice('-2+')+" ");

Um... end-of-line semi-colons?

Also:
for _ in' '*25
c=__import__('random').choice

Here's 65 in a few versions:
Code:
>>> import random;''.join(map(random.choice,['UDLRFB'," 2'",' ']*25))
"F' R  B' B' R  U' U2 U' B' D  B2 R  U  F  B' L  F2 U' D  D2 R' L' D2 B  R' "

>>> import random as r;''.join(map(r.choice,['UDLRFB'," 2'",' ']*25))
"U' B2 F  U' L2 D' R  B2 B' F  R  B  L2 B  U2 R' R' L  B2 D2 B2 D' D' R' U  "

>>> ''.join(map(__import__('random').choice,['UDLRFB'," 2'",' ']*25))
"D' U2 U' R  D2 U' U2 L' R  F' U2 F' B' R2 U  F' D  F  B' F' R2 B' F2 F2 R  "

But remember this is invalid anyway because of the trivial cancellations.
 
Here's 67 in a few versions:
Code:
import random;s=''.join(map(random.choice,['UDLRFB'," 2'",' ']*25))
import random as r;s=''.join(map(r.choice,['UDLRFB'," 2'",' ']*25))
s=''.join(map(__import__('random').choice,['UDLRFB'," 2'",' ']*25))

You can save one char by importing everything from the random-module.

Code:
from random import*;s=''.join(map(choice,['UDLRFB'," 2'",' ']*25))
 
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.
 
Back
Top