# My python one-liner scramble generator



## towwdso (Nov 11, 2010)

274 bytes
its a world record, I guess. (until someone do it on perl...)

```
c,a,l,f,g=__import__('random').choice,['FB','UD','RL'],[],lambda y:lambda x:x in y,lambda y:lambda x:x not in y;h=[l.append(c(filter(g(l[-1:]),c(filter(g(filter(f(a),[''.join(l[-2:][::x]) for x in [-1,1]])),a))))) for u in range(25)];print ' '.join([h+c(" '2",) for h in l])
```

It works. copy and paste onto python interpreter. Or save it on a .py file.
You could also tweak it to generate a lot of scrambles:

just put a

```
for n in range(12):
```
in the beggining,
and in DOS do:


```
scramble.py > output.txt
```

result:



> file: output.txt
> ---------------------------------------------------------------------------------------
> R' U2 D' L' R' D2 R2 U F B L' U B D F U' F' L' B' F' L' U2 D' R D2
> B' U2 D2 F L R B' U2 D F' R F' R' B' F' R L2 D' B F D U R2 L2 U2
> ...





EDIT:

```
from random import*;c=choice;w,l=['RL','UD','BF'],[]
for u in range(25):
 a=c(w)
 if sum([ord(x) for x in l[-2:]]) in [158,153,136]:
  while l[-1] in a:a=c(w)
 t=c(a)
 while l[-1:]==[t]:t=c(a)
 l=l+[t]

print ' '.join([x+c(" '2") for x in l])
============================================

from random import*;c=choice;w,l=['RL','UD','BF'],[]
def k():
 m=[y.replace(l[-1],'') for y in w] if l[-1:] else w
 h=[int(''.join(l[-2:]) in [x,x[::-1]]) for x in w]
 if 1 in h:m.pop(h.index(1))
 return m

for u in range(25):
 l.append(c(c(k())))

print ' '.join([x+c(" '2") for x in l])
```
first is 242 chars but is brute force.
second is 288 chars but is more pythonic.


----------



## Zarxrax (Nov 11, 2010)

Technically, looks like about 3 lines 
I need to learn some python so I can understand this.


----------



## qqwref (Nov 11, 2010)

That's kind of horrible looking, and I'm not sure 275ish characters counts as a single line, but nice job.

EDIT: hey, since you're only using d once, could you do this?:

```
c,a,l,f,g=__import__('random').choice,['FB','UD','RL'],[],lambda y:lambda x:x in y,lambda y:lambda x:x not in y;h=[l.append(c(filter(g(l[-1:]),c(filter(g(filter(f(a),[''.join(l[-2:][::x]) for x in [-1,1]])),a))))) for u in range(25)];print ' '.join([h+c(" '2") for h in l])
```


----------



## Lucas Garron (Nov 11, 2010)

towwdso said:


> its a world record, I guess. (until someone do it on perl...)


 
Or Mathematica.


```
StringJoin[ToString[#]<>(c=RandomChoice)[{" ","' ","2 "}]&/@(Flatten[RandomSample[#,Ceiling[Random[]*5/4]]&/@c[{{U,R},{F,B},{L,D}},25]]//.{a___,b_,b_,c___}:>{a,b,c})]
```

167 chars, still not tweetable. But I'm sure I could get it there, and coders like Stefan, Kociemba, or Rokicki could probably destroy us.


----------



## PatrickJameson (Nov 11, 2010)

towwdso said:


> until someone do it on perl...



120 chars.


```
map{while(($m=int rand 6)==$b||($c+$b==5&&$c==$m)){}print qw(U R F B L D)[$m].qw( ' 2)[int rand 3]." ";$c=$b;$b=$m}1..25
```

But yeah, like Lucas said, I'm sure there's a bunch of people who could destroy this, since mine is pretty basic.

Edit: 115


```
map{$m=int rand 6while$c+$b==5&&$c==$m||$m==$b;print qw(U R F B L D)[$m].qw( ' 2)[int rand 3]." ";$c=$b;$b=$m}1..25
```

>_>


----------



## VP7 (Nov 11, 2010)

Is this open to all languages ?


----------



## towwdso (Nov 11, 2010)

@qqref: yes, It was late night, I'll try to optmise the code some more now. mwahaha >
@VP7: yeah, it would be great too se your code.

ok guys, I don't have mathemathica or perl installed here (i had), but you know that the code should output a legal scramble right?
legal scramble: R L' U B U R2 L2 U B' L F2 ... etc
illegal scramble: U U' U2 U' U B F B F B F B F B2 B' B B B U R L' R R2 R R R R R R R R R R R ... you see what I mean


----------



## aronpm (Nov 11, 2010)

towwdso said:


> you know that the code should output a legal scramble right?


 
I asked Patrick earlier; he said the scrambles are valid.


----------



## joey (Nov 11, 2010)

I'm pretty sure Lucas and Patrick know what valid scrambles are


----------



## tim (Nov 11, 2010)

Ruby (1.9):

```
turns = [%w{R L}, %w{F B}, %w{D U}]
axis = rand t.size
output = (1..25).map do
  axis = (axis + rand(turns.size - 1) + 1) % turns.size
  t[axis].sample + ['', "'", '2'].sample
end.join(' ')

p output
```

That's 199 characters and it's still readable and pretty generic (it can be used to generate scrambles for cubes of all sizes).

After obfuscating it a bit, i got it down to 111 characters:

```
a=rand 3
o=(1..25).map{a=(a+rand(2)+1)%3;[%w{R L},%w{F B},%w{D U}][a].sample+['',"'",'2'].sample}.join(' ')
p o
```

Using p instead of puts is probably cheating, though. And i'm sure anyone who really knows Ruby can get this sub-100. I didn't spend more than a few minutes on it and i only used the most obvious shortcuts (like replacing variable names with its content).



towwdso said:


> sub 277 bytes



So you mean 276 bytes? Or 275 bytes? Or 200 bytes? Or 142 bytes? WTF? "sub" is really the most overused term amongst cubers. It's so annoying...


----------



## Lucas Garron (Nov 11, 2010)

tim:

Doesn't run for me on Ruby 1.8.7, not sure what the exact problem is. But I can't run your code to verify?
Is your program capable of generating U2 D2 in a row? Ours are, and that's a good part of the challenge. ;-)


----------



## tim (Nov 11, 2010)

Lucas Garron said:


> Doesn't run for me on Ruby 1.8.7, not sure what the exact problem is.


Yep, you need 1.9 for Array#sample



Lucas Garron said:


> 1) Does a actually need to be initialized randomly? Can save 5 bytes that way.


Yes, otherwise your first move will always be U/D.



Lucas Garron said:


> 2) Is your program capable of generating U2 D2 in a row? Ours are, and that's a good part of the challenge. ;-)



Of course not, since that would make the code longer . Actually i didn't know that's necessary. I might give it another try then.


----------



## towwdso (Nov 11, 2010)

tim said:


> So you mean 276 bytes? Or 275 bytes? Or 200 bytes? Or 142 bytes? WTF? "sub" is really the most overused term amongst cubers. It's so annoying...


 
I meant exactly 277 bytes. I wrote sub 1kb then I checked the atual size and just replaced 1KB with 277 bytes. my fault
now its 274 bytes actually. it's optimal, from the aproach I did.


----------



## joey (Nov 11, 2010)

Tim is a noob at ruby.


----------



## tim (Nov 11, 2010)

joey said:


> Tim is a noob at ruby.


----------



## towwdso (Nov 11, 2010)

I found that generating a single scramble of lenght 25000 is faster than generating 1000 scrambles of lenght 25 by 0.0092000007629999825 seconds. =P

generating 1000 scrambles times:


> 0.766000032425,
> 0.781000137329,
> *0.75,*
> 0.81200003624,
> ...



generating a single scramble of lenght 25000:



> 0.75,
> 0.765000104904,
> 0.780999898911,
> *0.719000101089,*
> ...



*obs:* generating a single scramble and divide it later would not allow the second scramble to start with the last face turn.
example:
1) U R2 F *B*
2) *B* L2 U' F
woudn't happen


----------



## qqwref (Nov 11, 2010)

I tried my hand at JavaScript.

224 (EDIT: 216) chars:

```
function r(x){return Math.floor(Math.random()*x)}function s(){var d=b=j=c=a=4;for(;j<29;j++){while(a==c&&(d&b)>0){a=r(3);b=r(2)+1}if(a!=c)d=0;c=a;d+=b;document.write("RLFBUD".charAt(a*2+b-1)+" 2'".charAt(r(3))+" ")}}
```


----------



## vcuber13 (Nov 11, 2010)

i tried to make a scrambler and im doing it in batch because its the only thing i know. so what i have now is it generate a random number and depending on the number assign the move. and i know how to stop it from repeating moves, but i dont know how to repeat it for the 25 moves (i know about goto start, but i need different variables). the only thing i can think of is to repeat the code and change say %move1% to %move2% and so on. then display the scramble as: %move1% %move2% ...
how can i make it loop but use different variables?


----------



## Lucas Garron (Nov 11, 2010)

tim said:


> Of course not, since that would make the code longer . Actually i didn't know that's necessary. I might give it another try then.


I think the distribution actually becomes better if you constantly change axes, but don't quote me on that. And it doesn't change the standard we're trying to match with programs here..


----------



## joey (Nov 11, 2010)

```
a=rand 3
p (1..25).map{a=(a+rand(2)+1)%3;[%w{R L},%w{F B},%w{D U}][a].sample+["","'","2"].sample}*" "
```
Just a few changes to tim's. Should work fine.


----------



## dbax0999 (Nov 11, 2010)

Haha I made a scrambler in TI-basic last year so we could get scrambles on our calculators.


----------



## StachuK1992 (Nov 11, 2010)

Why would a scramble not be allowed to always start with U/D?

I mean, when rotated, R = U, and moves relative to that are the same relative to those faces/axis.


----------



## qqwref (Nov 11, 2010)

What if you aren't color neutral? Wouldn't it matter then?


----------



## StachuK1992 (Nov 11, 2010)

qqwref said:


> What if you aren't color neutral? Wouldn't it matter then?


 How is that relevant?
If 10 peoples' cubes are scrambled with the same algorithm, and any state can be reached in 25 moves, then everyone's puzzle should be well-scrambled just as if they were scrambled with a random first face. No?


----------



## Lucas Garron (Nov 11, 2010)

StachuK1992 said:


> How is that relevant?
> If 10 peoples' cubes are scrambled with the same algorithm, and any state can be reached in 25 moves, then everyone's puzzle should be well-scrambled just as if they were scrambled with a random first face. No?



That's about good as reasoning that since a 20 random-move scramble can reach any state, it should be a random state.

But in practice, doesn't really matter. We use random-state where it counts, and I hope someday it will be no problem to use MRSS everywhere.


----------



## mr. giggums (Nov 11, 2010)

dbax0999 said:


> Haha I made a scrambler in TI-basic last year so we could get scrambles on our calculators.


 
Haha I did that this too. Mine was probably horribly inefficent though.


----------



## VP7 (Nov 12, 2010)

Here is one written in Qbasic.
How to add output to a file, I leave as an exercise.

RANDOMIZE TIMER
CLS
A$ = "UDFBLR '2"
FOR T = 1 TO 10
V$ = ""
FOR S = 1 TO 25
BAD:
M$ = MID$(A$, INT(RND * 6) + 1, 1)
T$ = MID$(A$, ((INT(RND * 3) + 1) + 6), 1)
IF M$ = N$ THEN GOTO BAD
V$ = V$ + M$ + T$ + " "
N$ = M$
NEXT S
PRINT V$: PRINT
NEXT T

Download for qbasic here:
http://www.qbcafe.net/qbc/english/download/compiler/qbasic_compiler.shtml

A download for a up todate qbasic:
http://www.qb64.net/forum/index.php?PHPSESSID=03qhel8uvgmucik5jqcgd391r5&


----------



## towwdso (Nov 12, 2010)

Anyone want to make a nice scrambler for the motorola w180?
I can't find any documentations for it. Its java I guess...


----------



## dbax0999 (Nov 12, 2010)

Anyone who writes a scrambler with Brainf*** wins my respect.


----------



## Baian Liu (Nov 12, 2010)

My code for a 5x5 scrambler for TI-83/84:

```
:ClrHome
:Clrdraw
:AxesOff
:FnOff
:"URFDLBw '2→Str0
:0→C
:0→E
:0→F
:For(A,1,60,6
:For(B,1,97,14
:D→E
:C→D
:F→G
:randInt(7,8→F
:While C=D
:randInt(1,6→C
:While C=E and C-3=D or C+3=D
:randInt(1,6→C
:End
:If C=D and F≠G and C≠E
:10→C
:End
:If C=10
:D→C
:Text(A,B,sub(Str0,C,1)+sub(Str0,F,1)+sub(Str0,randInt(8,10),1
:End
:End
```

I'll try to find mine for 3x3.


----------



## qqwref (Nov 12, 2010)

Down to 212 characters thanks to Devin:

```
function r(x){return Math.floor(Math.random()*x)}function s(){var d=b=j=c=a=4;for(;j<29;j++){while(a==c&&d&b){a=r(3);b=r(2)+1}if(a!=c)d=0;c=a;d+=b;document.write("RLFBUD".charAt(a*2+b-1)+" 2'".charAt(r(3))+" ")}}
```

(JavaScript isn't the greatest for code golf, but it's pretty cool to make a tiny webpage that scrambles.)


----------



## Rinfiyks (Nov 12, 2010)

Just downloaded J. I think it can sub 100 once I know how to write in it.


----------



## mrCage (Nov 12, 2010)

PatrickJameson said:


> 120 chars.
> 
> 
> ```
> ...


 
Stefan. I'm sure you made a short one earlier, but i forgot where ... Enlighten us pls!!

Per


----------



## towwdso (Nov 12, 2010)

mrCage said:


> Stefan. I'm sure you made a short one earlier, but i forgot where ... Enlighten us pls!!
> 
> Per


 
hmmm.. http://www.stefan-pochmann.de/spocc/scramblers/

EDIT: I generated some scrambles using 3 dices, pen and paper. I took a while. If you hate hand scrambles that much...


----------



## tim (Nov 12, 2010)

mrCage said:


> Stefan. I'm sure you made a short one earlier, but i forgot where ... Enlighten us pls!!
> 
> Per


 
Maybe you mix it up with his short 3x3x3 solver (528 bytes in Perl): http://www.stefan-pochmann.info/spocc/other_stuff/tools/


----------



## guzman (Nov 12, 2010)

qqwref said:


> Down to 212 characters thanks to Devin:
> 
> ```
> function r(x){return Math.floor(Math.random()*x)}function s(){var d=b=j=c=a=4;for(;j<29;j++){while(a==c&&d&b){a=r(3);b=r(2)+1}if(a!=c)d=0;c=a;d+=b;document.write("RLFBUD".charAt(a*2+b-1)+" 2'".charAt(r(3))+" ")}}
> ```



Some improvements ...
179 characters.


```
function r(n){if(n)return Math.floor(Math.random()*n);for(d=b=j=c=a=4;j++<29;){for(;a==c&&d&b;b=r(2)+1,a=r(3));d=a!=c?b:d+b;c=a;document.write(" RLFBUD"[a*2+b]+" 2'"[r(3)]+" ")}}
```

just call r() with no args.


----------



## mr. giggums (Nov 12, 2010)

I have improved my TI 83/84 3x3 Scrambler and here it is...

183 characters

```
:ClrHome
:"URFBLD '2→Str0
:6→S
:0→F
:For(A,1,5,1
:For(B,1,14,3
:P→L
:F→P
:randInt(1,S→F
:randInt(7,9→T
:While F=P
:randInt(1,S→F
:End
:If L=T
:6→T
:If F+P=7
:Then
:5→S
:Else
:6→S
:End
:Output(A,B,sub(Str0,F,1)+sub(Str0,T,1
:End
:End
```

EDIT: 181 characters

```
:ClrHome
:"URFBLD '2→Str0
:6→S
:0→F
:For(A,1,5,1
:For(B,1,14,3
:P→L
:F→P
:randInt(1,S→F
:While F=P
:randInt(1,S→F
:End
:If L=T
:6→T
:If F+P=7
:Then
:5→S
:Else
:6→S
:End
:Output(A,B,sub(Str0,F,1)+sub(Str0,randInt(7,9),1
:End
:End
```


----------



## vcuber13 (Nov 12, 2010)

ya is end twice


----------



## guzman (Nov 13, 2010)

guzman said:


> Some improvements ...
> 179 characters.
> 
> 
> ...


 
I've changed the structure but gained only 3 characters more:
176 characters:

```
function r(n){if(n)return Math.floor(Math.random()*n);for(d=b=j=c=a=4;j<29;)b=r(2)+1,a=r(3),a==c&&d&b||(d=a!=c?b:d+b,c=a,j++,document.write(" RLFBUD"[a*2+b]+" 2'"[r(3)]+" "))}
```

there are some long names in javascript that can't be avoided such as:
function(){} return Math.floor() Math.random() document.write()
and they account for 64 characters.


----------



## PatrickJameson (Nov 13, 2010)

guzman said:


> there are some long names in javascript that can't be avoided such as:
> function(){} return Math.floor() Math.random() document.write()
> and they account for 64 characters.



You can use parseInt() instead of Math.floor(). Saves two whole chars .


----------



## vcuber13 (Nov 13, 2010)

does anyone know what to do here: http://www.speedsolving.com/forum/s...uestion-thread&p=484140&viewfull=1#post484140


----------



## Baian Liu (Nov 13, 2010)

For TI83/84:


```
:ClrHome
:URFDLB '2→Str0
:0→E
:7→D
:For(A,1,6
:For(B,2,14,3
:E→F
:D→E
:Repeat D≠E and (D-3≠E or D+3≠E and D≠F
:randInt(1,6→D
:End
:Output(A,B,sub(Str0,D,1)+sub(Str0,randInt(7,9),1
:End
:End
```


----------



## joey (Nov 13, 2010)

That's awesome Baian.


----------



## keemy (Nov 13, 2010)

Baian Liu said:


> For TI83/84:
> 
> 
> ```
> ...



this didn't work for me.

I changed it to,


```
:ClrHome
:"URFDLB '2"→Str0
:0→E
:7→D
:For(A,2,7
:For(B,2,14,3
:E→F
:D→E
:While D=E or D=F
:randInt(1,6→D
:End
:Output(A,B,sub(Str0,D,1)+sub(Str0,randInt(7,9),1
:End
:End
```

I guess you need "" around a string? also made it print from line 2 to 7 as the "Done" was overwriting the last move on the first line hah. Good job though I really like this.


----------



## qqwref (Nov 13, 2010)

Correct me if I'm wrong, but wouldn't Baian's 3x3 scrambler also prevent sequences like B L B'?


----------



## Baian Liu (Nov 13, 2010)

keemy said:


> this didn't work for me.
> 
> I changed it to,
> 
> ...



On my TI83, the "" aren't required to end the string, nor does "Done" displays.



qqwref said:


> Correct me if I'm wrong, but wouldn't Baian's 3x3 scrambler also prevent sequences like B L B'?



Thanks for pointing that out. I changed line 9(the "While" line) to:


```
:Repeat D≠E and (D-3≠E or D+3≠E and D≠F
```


----------



## keemy (Nov 13, 2010)

Ah ok i am using the 84+ keypad on my nspire so maybe that's why.


----------



## guzman (Nov 13, 2010)

PatrickJameson said:


> You can use parseInt() instead of Math.floor(). Saves two whole chars .


 
Right, so 177 characters in javascript.
I've tried other solutions and all kind of tricks but this is the best I could reach.


----------



## Johannes91 (Nov 13, 2010)

JavaScript: You can use 0|x instead of Math.floor(x) and a-c instead of a!=c.

Old Perl golf, 126 chars:

```
perl -le 'for(0..24){$a=int rand 6 until$a-$a[0]&&$a+$a[0]-5|$a-$a[1];@a=($a,@a)}print"@{[map{(U,F,R,L,B,D)[$_].(v39,2)[rand 3]}@a]}"'
```


----------



## joey (Nov 13, 2010)

Johannes91: Remember this? 
http://www.speedsolving.com/forum/showthread.php?920-Restrictive-Move-Solving

Seems like some formatting got ruined along the way though.


----------



## abctoshiro (Nov 13, 2010)

Imma start trying a scrambler in C. But it may take a while. I'm pretty busy here. (But did anyone do it already?)


----------



## Johannes91 (Nov 13, 2010)

joey: Yeah, I remember that ^^



[abc]toshiro;484589 said:


> Imma start trying a scrambler in C. But it may take a while. I'm pretty busy here. (But did anyone do it already?)


A basic scrambler isn't much harder to write than a hello world. So yeah, it's been done in C and probably in <insert programming language here>.


----------



## guzman (Nov 13, 2010)

Johannes91 said:


> JavaScript: You can use 0|x instead of Math.floor(x)



thanks, I didn't know that.



Johannes91 said:


> ... and a-c instead of a!=c.



yes, you're right, I know that but I forgot to substitute it.

So now, 166 characters in javascript:

```
function r(n){if(n)return 0|Math.random()*n;for(d=b=j=c=a=4;j<29;)b=r(2)+1,a=r(3),a==c&&d&b||(d=a-c?b:d+b,c=a,j++,document.write(" RLFBUD"[a*2+b]+" 2'"[r(3)]+" " ))}
```

There's only 1 function:
r() with no args prints a scramble,
r(n) instead gives a random number in the range [0,n).
It would be nice to avoid the if and use instead the ternary operator (?) but I couldn't make it fit.


----------



## Johannes91 (Nov 13, 2010)

Getting rid of the function altogether gets us down to 142 characters:

```
r=Math.random;for(d=b=j=c=a=4;j<29;)b=0|r()*2+1,a=0|r()*3,a==c&&d&b||(d=a-c?b:d+b,c=a,j++,document.write(" RLFBUD"[a*2+b]+" 2'"[0|r()*3]+" "))
```


----------



## guzman (Nov 13, 2010)

Johannes91 said:


> Getting rid of the function altogether gets us down to 142 characters:
> 
> ```
> r=Math.random;for(d=b=j=c=a=4;j<29;)b=0|r()*2+1,a=0|r()*3,a==c&&d&b||(d=a-c?b:d+b,c=a,j++,document.write(" RLFBUD"[a*2+b]+" 2'"[0|r()*3]+" "))
> ```



Nice and easy !!! :tu


----------



## StachuK1992 (Nov 13, 2010)

Just 5 more (less) characters and it's down to half of OP.


----------



## guzman (Nov 13, 2010)

StachuK1992 said:


> Just 5 more (less) characters and it's down to half of OP.



If we accept that spaces in the output are not evenly distributed than we can get rid of 4 characters more
(reaching 138):


```
r=Math.random;for(d=b=j=c=a=4;j<29;)b=0|r()*2+1,a=0|r()*3,a==c&&d&b||(d=a-c?b:d+b,c=a,j++,document.write(" RLFBUD"[a*2+b]+" 2'"[0|r()*3]))
```

output example:
B R'D U B2U'B F D2R2D2L R'B L2R'D'F R2L'B R'D'U2R'


----------



## Baian Liu (Nov 14, 2010)

```
:ClrHome
:URFBLD '2→Str0
:0→E
:7→D
:For(A,1,6
:For(B,2,14,3
:E→F
:D→E
:While D=E or D+E=7 and D=F
:randInt(1,6→D
:End
:Output(A,B,sub(Str0,D,1)+sub(Str0,randInt(7,9),1
:End
:End
```


----------



## qqwref (Nov 14, 2010)

Changed around the code for a more efficient setup. *122* characters.


```
r=Math.random;for(c=b=j=0;j<25;)m=0|r()*6,c+b==5&&c==m||b==m|(c=b,b=m,j++,document.write("URFBLD"[m]+" 2'"[0|r()*3]+" "))
```

EDIT: HTML scrambler (as in, working webpage) in 142 characters. Works in my Firefox, anyway. Even uses normal notation thanks to ' (is there a nicer way to do that?).

```
<body onload='r=Math.random;for(c=b=j=0;j<25;)m=0|r()*6,c+b==5&&c==m||b==m||(c=b,b=m,j++,document.write("URFBLD"[m]+" 2'"[0|r()*3]+" "))'>
```


----------



## guzman (Nov 14, 2010)

qqwref said:


> Changed around the code for a more efficient setup. *122* characters.
> 
> 
> ```
> ...



I woke up thinking about the very same possibility and found out you already made it.
Nice job.
(the number of characters is close to perl, so far, and the main differences are 
document.write vs print and Math.random vs rand).


For a working html page, why not just:

```
<script>r=Math.random;for(c=b=j=0;j<25;)m=0|r()*6,c+b==5&&c==m||b==m||(c=b,b=m,j++,document.write("URFBLD"[m]+" 2'"[0|r()*3]+" "))</script>
```
(138 characters).


----------



## guzman (Nov 14, 2010)

Using the empty space of the 'for' we can gain one character more:
121 characters:


```
r=Math.random;for(c=b=j=0;j<25;c+b==5&&c==m||b==m||(c=b,b=m,j++,document.write("URFBLD"[m]+" 2'"[0|r()*3]+" ")))m=0|r()*6
```

It is interesting that the code is not so involved, it is still quite readable and linear in its structure.


EDIT: *115* characters

```
r=Math.random;for(c=b=j=0;j<25;c+b==5&c==m|b==m||document.write("URFBLD"[j++,c=b,b=m]+" 2'"[0|r()*3]+" "))m=0|r()*6
```

(111 if we accept uneven spaces in the output).


----------



## Johannes91 (Nov 14, 2010)

qqwref, guzman: Nice!

Shortened Patrick's Perl a bit, from 115 to 107:

```
map{$m=int rand 6until($c+$b-5||$c-$m)&&$m-$b;print+(U,R,F,B,L,D)[$m].qw(' 2)[rand 3]." ";$c=$b;$b=$m}1..25
```


----------



## Marcell (Nov 14, 2010)

My try:
127 characters in php.

```
<?$M="URFBLD 2'";for($i=$b=$c=0;$i<25;)if(($b!=$m=rand(0,5))&($c+$b!=5||$c!=$m)){$c=$b;echo$M[$b=$m].$M[rand(6,8)].' ';$i++;}?>
```

Edit: 123.

```
<?$M="URFBLD 2'";for($i=$b=$c=0;$i<25;)if(($b-$m=rand(0,5))&($c+$b-5|$c-$m)){$c=$b;echo$M[$b=$m].$M[rand(6,8)].' ';$i++;}?>
```


----------



## Carrot (Nov 14, 2010)

Edit: nothing in this post worked... xD


----------



## Marcell (Nov 14, 2010)

122 characters.

```
<?$M="URFBLD 2'";for($i=$b=$c=0;$i<25;)if(($b-$m=rand()%6)&($c+$b-5|$c-$m)){$c=$b;echo$M[$b=$m].$M[rand(6,8)].' ';$i++;}?>
```

Also, here's Johannes' code implemented in php - ironically enough this one is 122 chars as well.

```
<?$M="URFBLD 2'";$b=$c=0;for($i=0;$i<25;$i++){while(!($b-$m=rand()%6&($c+$b-5|$c-$m)));echo$M[$b=$m].$M[rand(6,8)].' ';}?>
```


----------



## guzman (Nov 14, 2010)

Marcell said:


> 122 characters.
> 
> ```
> <?$M="URFBLD 2'";for($i=$b=$c=0;$i<25;)if(($b-$m=rand()%6)&($c+$b-5|$c-$m)){$c=$b;echo$M[$b=$m].$M[rand(6,8)].' ';$i++;}?>
> ...



Nice.
I think there's place for improvement in your php code.
I dont have a php compiler installed but I think that you can use many of the ideas in our javascript code:
you can initiate b and c inside the for: for($b=$c=$i=0;....),
you can avoid the while by justing letting the for cicle until it finds a good case (and increase i only in that case).

You should get very close to 110.


----------



## Marcell (Nov 14, 2010)

Smart!
114 now:

```
<?$M="URFBLD 2'";for($i=$b=$c=0;$i<25;)if($b-$m=rand()%6&($c+$b-5|$c-$m)){echo$M[$b=$m].$M[rand(6,8)].' ';$i++;}?>
```

I'm afraid php doesn't let me leave out the "if" statement.
Also, I did initiate b and c in the for loop in my first code, but forgot to do so in the second. Thanks for pointing that out.
BTW I don't have a php compiler either, I let it run on a web server.


----------



## guzman (Nov 14, 2010)

Marcell said:


> 114 now


Nice



Marcell said:


> I'm afraid php doesn't let me leave out the "if" statement.



I realized that and edited my post but you had already read.


----------



## guzman (Nov 14, 2010)

Marcell said:


> Smart!
> 114 now:
> 
> ```
> ...



Some improvements:
112 considering tags or 108 not considering them (as in javascript):

```
for($i=$b=$c=0;$i<25;$M="URFBLD 2'")if($b-$m=rand()%6&($c+$b-5|$c-$m))echo$M[$b=$m].$M[rand(6,8+0*$i++)].' '
```

Edit: but there was an error in your code and I copied it. So this isn't valid.


----------



## Marcell (Nov 14, 2010)

Uhh, I'm confused. At some point I somehow left out the "c=b" part, which I now think I shouldn't have done. Still, I see no corrupt scrambles in the output: http://kerulo.uw.hu/miniscr/miniscr2.php
Thoughts?


----------



## guzman (Nov 14, 2010)

You're right !!

my code also has the same error cause I started from yours and I didn't check,
but actually I don't see wrong scrambles ...

EDIT: wrong scrambles do appear.


----------



## Stefan (Nov 14, 2010)

75 bytes:


Spoiler



print "F' D B2 L B R2 B2 L R' D' R' F L' R' U2 L B2 U2 D' F' R' U2 B L2 F'"
// chosen by fair dice rolls. guaranteed to be random.


----------



## StachuK1992 (Nov 14, 2010)

Spoiler



Haha, Stefan! I was expecting some crazy cryptic code, then was all "...wait..that's not.."


----------



## Marcell (Nov 14, 2010)

@StachuK1992: Just like me.

@guzman: Oh well. 120 characters then. *sigh*

```
<?$M="URFBLD 2'";for($i=$b=$c=0;$i<25;)if($b-$m=rand()%6&($c+$b-5|$c-$m)){$c=$b;echo$M[$b=$m].$M[rand(6,8)].' ';$i++;}?>
```

Edit: but I'm not sure whether I'm obliged to count the <? and ?> tags as well. I mean, you didn't count the <script></script> either.
Edit2: never mind. I didn't see your edited post. So I think it's fair to say that it's 116 characters long:

```
$M="URFBLD 2'";for($i=$b=$c=0;$i<25;)if($b-$m=rand()%6&($c+$b-5|$c-$m)){$c=$b;echo$M[$b=$m].$M[rand(6,8)].' ';$i++;}
```

Edit3: however your idea about nesting the $M="..." into the for statement was actually valid. 115 characters:

```
for($i=$b=$c=0;$i<25;$M="URFBLD 2'")if($b-$m=rand()%6&($c+$b-5|$c-$m)){$c=$b;echo$M[$b=$m].$M[rand(6,8)].' ';$i++;}
```


----------



## Marcell (Nov 14, 2010)

Crap, I'm still getting wrong scrambles. No idea why.

Edit: I see. I lost track of those brackets. It should be fine now. But now it's 121.

```
<?for($i=$b=$c=0;$i<25;$M="URFBLD 2'")if(($b-$m=rand()%6)&($c+$b-5|$c-$m)){$c=$b;echo$M[$b=$m].$M[rand(6,8)].' ';$i++;}?>
```


----------



## CharlesOBlack (Nov 14, 2010)

anyone care to explain what the general idea is behind a scramble program?


----------



## StachuK1992 (Nov 14, 2010)

CharlesOBlack said:


> anyone care to explain what the general idea is behind a scramble program?


 
Really?


----------



## EricReese (Nov 14, 2010)

StachuK1992 said:


> Really?


 
R O F L 
O
F
L


----------



## CharlesOBlack (Nov 14, 2010)

StachuK1992 said:


> Really?


 
yes, really.


----------



## guzman (Nov 14, 2010)

CharlesOBlack said:


> anyone care to explain what the general idea is behind a scramble program?


 
- the program has to give a sequence of 25 letters chosen randomly amogn RFULBD,
- each letter appears with an "exponent" chosen ramdomly among 1,2,3: that is, for instance R may appear as R, R2 or R',
- consecutive letters cannot be the same
- if R follows L, the next letter cannot be L again (and similarly for the other opposite letters in whatever form they appear).

that's it.


----------



## StachuK1992 (Nov 14, 2010)

A scramble program
is a program that produces scrambles as allowed by the standard rules.

I guess guzman gives a nicer explanation, but I thought you were asking the general idea, not just the specifics.


----------



## CharlesOBlack (Nov 14, 2010)

guzman said:


> - the program has to give a sequence of 25 letters chosen randomly amogn RFULBD,
> - each letter appears with an "exponent" chosen ramdomly among 1,2,3: that is, for instance R may appear as R, R2 or R',
> - consecutive letters cannot be the same
> - if R follows L, the next letter cannot be L again (and similarly for the other opposite letters in whatever form they appear).
> ...


 
and how does the computer find out how the cube looks like after any given scramble?


----------



## StachuK1992 (Nov 14, 2010)

CharlesOBlack said:


> and how does the computer find out how the cube looks like after any given scramble?


 All you need to worry about is the output string.
Don't pay attention to the cube appearance.


----------



## Marcell (Nov 14, 2010)

> and how does the computer find out how the cube looks like after any given scramble?


It doesn't.


----------



## CharlesOBlack (Nov 14, 2010)

Marcell said:


> It doesn't.


 
CCT does...

edit: to guzman below me: how?


----------



## guzman (Nov 14, 2010)

CharlesOBlack said:


> and how does the computer find out how the cube looks like after any given scramble?


 
Well, that's not part of the scrambler program, but can easily be done.


----------



## Marcell (Nov 14, 2010)

@Charles: I think you might've missed the point. We're not trying to write a nice or practical scrambler, we're just trying to minimize the code.

@guzman: note that the bold brackets are in fact neccessary.

```
if([B]([/B]$b-$m=rand()%6[B])[/B]&($c+$b-5|$c-$m))
```
I was kind of puzzled why my code didn't work until I found out that you were the one who removed those.


----------



## guzman (Nov 14, 2010)

Marcell said:


> guzman: note that the bold brackets are in fact neccessary.



You're right, I did that inadvertedly. Sorry.


----------



## CharlesOBlack (Nov 14, 2010)

Marcell said:


> @Charles: I think you might've missed the point. We're not trying to write a nice or practical scrambler, we're just trying to minimize the code.


 
I understand that, but I'd still like to know how I'd make the PC find the current state of the cube, and... well, this thread has quite a few programmers who could probably explain it.


----------



## joey (Nov 14, 2010)

Just write a simulator.. and have it follow the moves...
It's really not hard >_>


----------



## PatrickJameson (Nov 14, 2010)

CharlesOBlack said:


> I understand that, but I'd still like to know how I'd make the PC find the current state of the cube, and... well, this thread has quite a few programmers who could probably explain it.


 
You would have a set of data representing the current state of the cube, starting with a solved state. You can then manipulate the set of data going through every move and modifying the cube's current position based on what you have defined each move to do. Then you can just take that data set and translate it to a graphical thingy if you want.


----------



## guzman (Nov 14, 2010)

Marcell said:


> ... now it's 121.
> 
> ```
> <?for($i=$b=$c=0;$i<25;$M="URFBLD 2'")if(($b-$m=rand()%6)&($c+$b-5|$c-$m)){$c=$b;echo$M[$b=$m].$M[rand(6,8)].' ';$i++;}?>
> ```



One last dirty trick and we can reach 120 or 116 without tags:

```
for($i=$b=$c=0;$i<25;$M="URFBLD 2'")if(($b-$m=rand()%6)&($c+$b-5|$c-$m))echo$M[$b=$m].$M[rand(6,8+0*$i++*$c=$b)].' '
```

the trick is:
rand(6,8+0*$i++*$c=$b)

this removes the need for {} in the 'if' and some commas

Edit: we fixed an error in the next posts


----------



## CharlesOBlack (Nov 14, 2010)

PatrickJameson said:


> You would have a set of data representing the current state of the cube, starting with a solved state. You can then manipulate the set of data going through every move and modifying the cube's current position based on what you have defined each move to do. Then you can just take that data set and translate it to a graphical thingy if you want.


 
this sounds memory/processor hungry. XD

edit: rethinking this; would it be possible to generate a random position for the cube and then find an "algorithm" to solve it, and display the inverse? if so, how do you fix parity?


----------



## Marcell (Nov 14, 2010)

> the trick is: rand(6,8+0*$i++*$c=$b)


Wow, man, amazing. It's quite a dirty piace of code alright, but it is shorter and it works.
And this way you could leave out the semicolon at the end, too! php is a real b**** when it comes to semicolons.


----------



## guzman (Nov 14, 2010)

CharlesOBlack said:


> this sounds memory/processor hungry. XD


 
not at all, it requires only a hundred bytes to represent the state of the cube.
and it is very easy to update after each move

@Marcell:


----------



## Marcell (Nov 14, 2010)

guzman said:


> and it is very easy to update after each move


I'd be interested in that. For me it's not trivial how you'd apply the moves to your model.


----------



## Marcell (Nov 14, 2010)

CharlesOBlack said:


> edit: rethinking this; would it be possible to generate a random position for the cube and then find an "algorithm" to solve it, and display the inverse? if so, how do you fix parity?


 
Sure, it's possible. Way harder then just generating a scamble and visualizing it though.


----------



## guzman (Nov 14, 2010)

Marcell said:


> I'd be interested in that. For me it's not trivial how you'd apply the moves to your model.


 
by "easy" I mean that you can just write in your code the effect of each move (no algs, just wirecoded permutations).


----------



## Marcell (Nov 14, 2010)

guzman said:


> by "easy" I mean that you can just write in your code the effect of each move (no algs, just wirecoded permutations).


I see.

But I'm very sorry to say that the latest version doesn't work, it gives invalid scrambles time to time. Dunno why.
The one before your "dirty trick" can be found here: http://kerulo.uw.hu/miniscr/miniscr.php
It does seem to be alright, but who knows? Maybe I just haven't run through enough scrambles.


----------



## guzman (Nov 14, 2010)

Marcell said:


> It does seem to be alright, but who knows? Maybe I just haven't run through enough scrambles.




We pushed it too far ... Could it be the initialization of M inside the for ?
What kind of error does it make ?
letter repetition: as in R' R2
or
opposite letters error: R2 L R'


----------



## guzman (Nov 14, 2010)

I see the error in mine:

c=b; // then
b=m; 

not the inverse.


----------



## Marcell (Nov 14, 2010)

Oh that's right. But that's easily solvable by applying the trick eariler:

```
for($i=$b=$c=0;$i<25;$M="URFBLD 2'")if(($b-$m=rand()%6)&($c+$b-5|$c-$m))echo$M[$c=$b*$i++*0+$b=$m].$M[rand(6,8)].' '
```

116 characters. Up for testing at http://kerulo.uw.hu/miniscr/miniscr2.php


Edit: Ech... still invalid. Opposite letter error.


----------



## guzman (Nov 14, 2010)

Marcell said:


> Oh that's right. But that's easily solvable by applying the trick eariler




Edit: shoudln't it be: $b=$m+0*$i++*$c=$b ?
'cause the righthand side is evaluated first.


----------



## Marcell (Nov 14, 2010)

Oh that's right. Thanks.

```
for($i=$b=$c=0;$i<25;$M="URFBLD 2'")if(($b-$m=rand()%6)&($c+$b-5|$c-$m))echo$M[$b=$m+0*$i++*$c=$b].$M[rand(6,8)].' '
```

116 chars, still longer then your javascript code with all that crazy evaluation stuff. 

Unfortunately I know squad about perl, but I think the perl code could be shortened with some of these tricks too.
Also, php let me use & and | instead of && and ||, maybe perl would do so as well.


----------



## guzman (Nov 14, 2010)

Marcell said:


> 116 chars, still longer then your javascript code with all that crazy evaluation stuff.



Yeah, the if part in php and all those () are cumbersome, and of course all the $'s

The comma (,) is also helpful in javascript


----------



## Marcell (Nov 14, 2010)

Yeah. But as for javascript, "math.random" and "document.write" are just painful.


----------



## guzman (Nov 14, 2010)

Marcell said:


> Yeah. But as for javascript, "math.random" and "document.write" are just painful.


 
Yeah, I thought we could reach shorter versions with php,
I dont know perl either, I would like to.

Edit: That was fun. Thanks.


----------



## Marcell (Nov 14, 2010)

It really was.
But I don't think it's finished yet


----------



## guzman (Nov 14, 2010)

So far. Various authors and contributions:

perl: *107*

```
map{$m=int rand 6until($c+$b-5||$c-$m)&&$m-$b;print+(U,R,F,B,L,D)[$m].qw(' 2)[rand 3]." ";$c=$b;$b=$m}1..25
```

javascript: *115*

```
r=Math.random;for(c=b=j=0;j<25;c+b==5&c==m|b==m||document.write("URFBLD"[j++,c=b,b=m]+" 2'"[0|r()*3]+" "))m=0|r()*6
```

php: *116*

```
for($i=$b=$c=0;$i<25;$M="URFBLD 2'")if(($b-$m=rand()%6)&($c+$b-5|$c-$m))echo$M[$b=$m+0*$i++*$c=$b].$M[rand(6,8)].' '
```

mathematica: *166*

```
StringJoin[ToString[#]<>(c=RandomChoice)[{" ","' ","2 "}]&/@(Flatten[RandomSample[#,Ceiling[Random[]*5/4]]&/@c[{{U,R},{F,B},{L,D}},25]]//.{a___,b_,b_,c___}:>{a,b,c})]
```

pyton: *273*

```
c,a,l,f,g=__import__('random').choice,['FB','UD','RL'],[],lambda y:lambda x:x in y,lambda y:lambda x:x not in y;h=[l.append(c(filter(g(l[-1:]),c(filter(g(filter(f(a),[''.join(l[-2:][::x]) for x in [-1,1]])),a))))) for u in range(25)];print ' '.join([h+c(" '2") for h in l])
```


(it's nice that horizontal scroll bars look as indicators in a graph, at least with my browser and screen)

EDIT: 
I removed the Ruby program since it wasn't able to give all kind of valid scrambles:


----------



## qqwref (Nov 14, 2010)

guzman said:


> - the program has to give a sequence of 25 letters chosen randomly amogn RFULBD,
> - each letter appears with an "exponent" chosen ramdomly among 1,2,3: that is, for instance R may appear as R, R2 or R',
> - consecutive letters cannot be the same
> - if R follows L, the next letter cannot be L again (and similarly for the other opposite letters in whatever form they appear).


More specifically:
- A scramble is a 25-move sequence, where each move is one of the 6 letters URFBLD with one of the 3 suffixes "", "2", "'". The moves should have a single space between them (but I wouldn't say this is required.)
- In a valid scramble, two consecutive moves may not have the same letter, and three consecutive moves may not have the letters X Y X (for X and Y being any two opposite sides, i.e. R/L, U/D, or F/B).
- A scrambling program must be able to print out ALL valid scrambles (assuming of course that the RNG can output all possible sequences of sufficient length), and must not print any non-valid scrambles.


EDIT: By the way, although the above Perl scrambler works (same mechanism as the JS one), the Ruby one does not. Because it switches axis every move, it cannot ever print scrambles containing sequences like R L or F2 B'.


----------



## joey (Nov 14, 2010)

Lame, I didn't know that. I'll try write a Ruby one sometime.


----------



## Stefan (Nov 14, 2010)

guzman said:


> perl: *107*
> 
> ```
> map{$m=int rand 6until($c+$b-5||$c-$m)&&$m-$b;print+(U,R,F,B,L,D)[$m].qw(' 2)[rand 3]." ";$c=$b;$b=$m}1..25
> ```


 
I think you can replace "int " with "0|" there as well.

And here's a longer (currently *111* bytes) but quite simple and straightforward approach very close to Michael's definition:

```
$_.=chr rand 99,/[^URFBLD]|(.)\1|LRL|RLR|UDU|DUD|FBF|BFB/&&chop until/.{25}/;s/./" $&".qw(' 2)[rand 3]/ge;print
```

The following (with *107* bytes) also works, but I'm unsure about the randomness:

```
$_.=chr rand 99,s/[^URFBLD]|(.)\1|LRL|RLR|UDU|DUD|FBF|BFB// until/.{25}/;s/./" $&".qw(' 2)[rand 3]/ge;print
```


----------



## Johannes91 (Nov 15, 2010)

Stefan said:


> The following (with *107* bytes) also works, but I'm unsure about the randomness:
> 
> ```
> $_.=chr rand 99,s/[^URFBLD]|(.)\1|LRL|RLR|UDU|DUD|FBF|BFB// until/.{25}/;s/./" $&".qw(' 2)[rand 3]/ge;print
> ```


 
Sweet! I thought of using regexes but couldn't come up with something so short.

However, if we include in the requirements that every valid scramble must be generated with equal probability - and I think we should* - that code doesn't pass. It discriminates against sequences containing UD or FB or RL.

* Problems with a language implementation's implementation of rand() can be ignored, of course.


----------



## qqwref (Nov 15, 2010)

Johannes91 said:


> However, if we include in the requirements that every valid scramble must be generated with equal probability


I'm not actually sure the normal scrambling algorithm does this. Consider 3-move scrambles, ignoring suffixes. The probability of the first two moves being R B and R L are exactly the same (1/30). However, R B can be followed by 5 moves (so each 3-move scramble starting with R B has probability 1/150) but R L can be followed by only 4 moves (so each 3-move scramble starting with R L has probability 1/120). The same thing should happen with more moves.

Can anyone write an efficient adaptation of this algorithm (here in pseudocode)?:

```
do {
 generate random sequence of 25 faces
} until the sequence contains no duplicates or sequences such as RLR
randomly add a suffix after each face
```


----------



## PatrickJameson (Nov 15, 2010)

qqwref said:


> Can anyone write an efficient adaptation of this algorithm (here in pseudocode)?:
> 
> ```
> do {
> ...



Here's my implementation at 122 chars(stealing Stefan's RLR etc part of the regex). There must be a better way to do the regex.


```
do{@a=map{(U,R,F,B,L,D)[rand 6]}1..25}while(join'',@a)=~/((.)\2|LRL|RLR|UDU|DUD|FBF|BFB)/;print$_.qw(' 2)[rand 3]." "[email protected]
```

Edit: And another, mixing around a few things. Still 122.


```
do{$_=join'',map{(U,R,F,B,L,D)[rand 6]}1..25}while/((.)\2|LRL|RLR|UDU|DUD|FBF|BFB)/;print$_.qw(' 2)[rand 3]." "for split//
```


----------



## keemy (Nov 15, 2010)

C++ 115 (111 w/o needing spaces between)


```
for(int c=0,b=0,j=0;j<25;){int m=rand()%18;c+b==5&c==m%6|b==m%6||(cout<<"URFBLD"[j++,c=b,b=m%6]<<" 2'"[m/6]<<" ");}
```

Basically the same as the JS one with few modifications(to make it work), also I don't really program so changes are probably nub. Though I liked my idea of only using 1 random number a loop and just *6 and /6 for the choices(and it was the first way I got it to work lol).


----------



## guzman (Nov 15, 2010)

Consider a scramble.
What is the probability of having two consecutive letters on the same axis (such as R and L).

Say we begin with an R, the following letter is any letter chosen randomly among UFLBD, so the probability of having L is 1/5.

We may therefore think of a scrambler with the following structure:

choose a random axis (say we choose the RL axis),
choose randomly if there's gonna be two moves (with probability 1/5) or one move (with probability 4/5) on that axis:
if we need two moves: choose randomly between R L and L R 
if we need one move: choose randomly between R and L,
apply random suffixes to each letter (none, 2, or ')
repeat process changing axis.

Does that make sense ?
I think it gives each scramble the same probability 
that we get by choosing random moves at each turn.

So, the idea is to decompose a scramble as follows:
B - R' - DU - B2 - U' - BF - D2 - R2 - D2 - LR' - B - L2R' - D' - F - R2L' - B - R' - D'U2 - R' 
where moves on the same axis are coupled together.
The probability of having a couple should be 1/5.

Dunno if it can be implemented more efficiently than what we have.
__________________

I'll update the list of algs later 
(and I'll remove the wrong Ruby program).


----------



## PatrickJameson (Nov 15, 2010)

PatrickJameson said:


> Edit: And another, mixing around a few things. Still 122.
> 
> 
> ```
> ...



Using /./g instead of split// and removing the outer parentheses in the regex we get 116 chars. 


```
do{$_=join'',map{(U,R,F,B,L,D)[rand 6]}1..25}while/(.)\1|LRL|RLR|UDU|DUD|FBF|BFB/;print$_.qw(' 2)[rand 3]." "for/./g
```


----------



## joey (Nov 15, 2010)

115 my first attempt with ruby.

```
(x=y=0..25).map{z=x;x=(w=[%(UD),%(FB),%(RL)][rand 3])[rand 2] until x!=y||(z!=x&&w[y]);(y=x)+"2'"[rand 3].to_s}*" "
```

edit:

115 again, but this time it prints. (I was working in IRB so it printed automatically)


```
p (x=y=0..25).map{z=x;x=(w=[%(UD),%(FB),%(RL)][rand 3])[rand 2] until x!=y||z!=x&&w[y];(y=x)+"2'"[rand 3].to_s}*" "
```


----------



## Stefan (Nov 15, 2010)

PatrickJameson said:


> Using /./g instead of split// and removing the outer parentheses in the regex we get 116 chars.
> 
> 
> ```
> ...


 
115 with spaces before turns instead of after:


```
do{$_=join'',map{(U,R,F,B,L,D)[rand 6]}1..25}while/(.)\1|LRL|RLR|UDU|DUD|FBF|BFB/;print" $_".qw(' 2)[rand 3]for/./g
```


----------



## Marcell (Nov 15, 2010)

Shortened C++ version, 113 chars (not my work) EDIT: wrong.

```
for(int c=0,b=0,j=0,m;j++<25;){m=rand()%18;c+b==5&&c==m%6||b==m%6||(cout<<"URFBLD"[c=b,b=m%6]<<" 2'"[m/6]<<" ");}
```

Wow, 104! And that's my work  EDIT: wrong.

```
for(int c=0,b=0,j=0,m;j++<25;)c==(m=rand()%6)&&c+b==5||b==m||(cout<<"URFBLD"[c=b,b=m]<<" 2'"[m%3]<<" ");
```

102. So close... EDIT: wrong.

```
for(int c=0,b=0,j=0,m;j++<25;)c==(m=rand()%6)&c+b==5|b==m||(cout<<"URFBLD"[c=b,b=m]<<" 2'"[m%3]<<" ");
```


----------



## Marcell (Nov 15, 2010)

And... 100. 

```
for(int c=0,b=0,j=0,m;j++<25;)c-(m=rand()%6)|c+b-5&&b-m&&(cout<<"URFBLD"[c=b,b=m]<<" 2'"[m%3]<<" ");
```

EDIT: wrong.


----------



## guzman (Nov 15, 2010)

Marcell said:


> And... 100.



Nice job.
Two observations: 
shouldn't the c++ code include void main(){} in the counting characters ?
it only gives 21 moves. EDIT: 'cause j is incremented everytime.


----------



## Marcell (Nov 15, 2010)

1. frankly, I don't know.
2. haha, you're right

So 102. EDIT: wrong again.

```
for(int c=0,b=0,j=0,m;j<25;c-(m=rand()%6)|c+b-5&&b-m&&(cout<<"URFBLD"[j++,c=b,b=m]<<" 2'"[m%3]<<" "));
```


----------



## guzman (Nov 15, 2010)

There's a mistake:
by setting m=rand()%6 
and choosing the suffix for the move with m%3

you'll always have D' or L2 or U or B etc ... but never D or L' or U' or B' ...
'cause it creates a dependence of the suffix on the move


----------



## Marcell (Nov 15, 2010)

How about

```
for(int c=0,b=0,j=0,m;j<25;(m=rand()%6)-c|c+b-5&&b-m&&(cout<<"URFBLD"[j++,c=b,b=m]<<" 2'"[rand()%3]<<" "));
```

I don't get it. (m=rand()%6)-c works, but (m=rand())%6-c doesn't.


----------



## joey (Nov 15, 2010)

Because it's doing

(m=rand())%(6-c)


----------



## Marcell (Nov 15, 2010)

Well, ((m=rand())%6)-c doesn't work either.


----------



## qqwref (Nov 15, 2010)

I think I saved a character in the JS one.


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


----------



## guzman (Nov 15, 2010)

qqwref said:


> I think I saved a character in the JS one.



Nice Job.
After trying variuos combinations I thought we've reached the limit
but you've saved one more.


----------



## Marcell (Nov 15, 2010)

Nice, qqwref!

This is 106, guzman check please 

```
for(int c=0,b=0,j=0,m;j<25;m=rand(),c-m%6|c+b-5&&b-m%6&&(cout<<"URFBLD"[j++,c=b,b=m%6]<<" 2'"[m%3]<<" "));
```


----------



## guzman (Nov 15, 2010)

Marcell said:


> This is 106, guzman check please



I was also trying that
but it creates the same dependence


----------



## joey (Nov 15, 2010)

Marcell said:


> Well, ((m=rand())%6)-c doesn't work either.


 
((m=rand())%6)-c =
m = rand()
m%6 - c

(m=rand()%6)-c =
m = rand() % 6
m - c


----------



## Marcell (Nov 15, 2010)

@joey: theoritically, yes. But ((m=rand())%6)-c gives me Acces violation error message (although INT_MAX is way bigger then RAND_MAX).

@guzman: You're right.
But at least I realised that the ( ) are unneccessary. 105.

```
for(int c=0,b=0,j=0,m;j<25;(m=rand()%6)-c|c+b-5&&b-m&&cout<<"URFBLD"[j++,c=b,b=m]<<" 2'"[rand()%3]<<" ");
```


----------



## guzman (Nov 15, 2010)

Nice Job,
this is one of the shortest so far if we're allowed not to count void main(){}


----------



## keemy (Nov 15, 2010)

hah I noticed the j++ being incremented everytime too and was like whoops and moved it to inside the [] a hour before marcell posted in all fairness XP.


I think we may be able to save a char or 2 more if instead of initializing c and b we just check if j<2.

also you can't use m%6 and m%3 as if (m%6==0) then(m%3==0) so any time got a U it would have no suffix (same for other 5 letters would all appear with same sufix) so maybe if m can be chosen between the 2?


----------



## CharlesOBlack (Nov 15, 2010)

Marcell said:


> 105.
> 
> ```
> for(int c=0,b=0,j=0,m;j<25;(m=rand()%6)-c|c+b-5&&b-m&&cout<<"URFBLD"[size=5][j++,c=b,b=m][/size]<<" 2'"[rand()%3]<<" ");
> ```


 
I'd like an explanation for this... I don't understand the resized part.


----------



## Marcell (Nov 15, 2010)

In any normal program that part would be written as:

```
cout<<"URFBLD"[m];
j++; c=b; b=m;
```
Does the same. The expressions separated by commas are evaluated from left to right, and only the last one passes a value about which element of the array to return.
(But correct me if I'm wrong, honestly I never learned C++.)


----------



## CharlesOBlack (Nov 15, 2010)

Marcell said:


> In any normal program that part would be written as:
> 
> ```
> cout<<"URFBLD"[m];
> ...


 
OOH ok, now I get it. but wouldn't it do the inverse of what you said, like:


```
j++; c=b; b=m;
cout<<"URFBLD"[m];
```

?


----------



## Marcell (Nov 15, 2010)

Right, it would. Though in this case it doesn't matter (we never change the value of m).


----------



## CharlesOBlack (Nov 15, 2010)

Marcell said:


> Right, it would. Though in this case it doesn't matter (we never change the value of m).


 
As I thought.

I'm getting good at this!


----------



## guzman (Nov 15, 2010)

*Recap*

Recap. So far: 
(I've included some of the variations that use different ideas):

perl: *104* (edit)

```
map{$m=0|rand 6until($c+$b-5||$c-$m)&&$m-$b;print" ".(U,R,F,B,L,D)[$m].qw(' 2)[rand 3];$c=$b;$b=$m}1..25
```

C++: *105 (113 with main(){} )*

```
for(int c=0,b=0,j=0,m;j<25;(m=rand()%6)-c|c+b-5&&b-m&&cout<<"URFBLD"[j++,c=b,b=m]<<" 2'"[rand()%3]<<" ");
```

perl: *111*

```
$_.=chr rand 99,/[^URFBLD]|(.)\1|LRL|RLR|UDU|DUD|FBF|BFB/&&chop until/.{25}/;s/./" $&".qw(' 2)[rand 3]/ge;print
```

javascript: *114*

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

Ruby: *115*

```
p (x=y=0..25).map{z=x;x=(w=[%(UD),%(FB),%(RL)][rand 3])[rand 2] until x!=y||z!=x&&w[y];(y=x)+"2'"[rand 3].to_s}*" "
```

perl: *115*

```
do{$_=join'',map{(U,R,F,B,L,D)[rand 6]}1..25}while/(.)\1|LRL|RLR|UDU|DUD|FBF|BFB/;print" $_".qw(' 2)[rand 3]for/./g
```

php: *116*

```
for($i=$b=$c=0;$i<25;$M="URFBLD 2'")if(($b-$m=rand()%6)&($c+$b-5|$c-$m))echo$M[$b=$m+0*$i++*$c=$b].$M[rand(6,8)].' '
```

pyton: *149* (edit)

```
from random import randint as r;c=b=0
for i in range(25):
 m=r(0,5)
 while (c+b-4 or c==m)and m==b:m=r(0,5)
 print "URFBLD"[m]+" '2"[r(0,2)],;c=b;b=m
```

mathematica: *166*

```
StringJoin[ToString[#]<>(c=RandomChoice)[{" ","' ","2 "}]&/@(Flatten[RandomSample[#,Ceiling[Random[]*5/4]]&/@c[{{U,R},{F,B},{L,D}},25]]//.{a___,b_,b_,c___}:>{a,b,c})]
```


----------



## guzman (Nov 15, 2010)

Just fantasies: If we had rand(n) and print() in javascript we would reach 91:


```
for(c=b=j=0;j<25;c+b-5|c-m&&b-m?print("URFBLD"[j++,c=b,b=m]+" 2'"[rand(3)]+" "):0)m=rand(6)
```


----------



## masterofthebass (Nov 16, 2010)

just because the OP's program sucks (no offense), I did a very basic one in python 

151 chars (including newlines)

```
from random import randint as r;c=b=0
for i in range(25):
 m=r(0,5)
 while not((c+b-5 or c-m)and m-b):m=r(0,5)
 print "URFBLD"[m]+"'2"[r(0,1)],;c=b;b=m
```


----------



## CharlesOBlack (Nov 16, 2010)

C++:


```
for(int c=b=j=0,m;j<25;(m=rand()%6)-c|c+b-5&&b-m&&cout<<"URFBLD"[j++,c=b,b=m]<<" 2'"[rand()%3]<<" ");
```

possible? makes it 101 chars.


----------



## qqwref (Nov 16, 2010)

masterofthebass said:


> 151 chars (including newlines)
> 
> ```
> from random import randint as r;c=b=0
> ...


I don't speak Python, but wouldn't this part cause the code to never produce clockwise turns?


----------



## guzman (Nov 16, 2010)

CharlesOBlack said:


> C++:
> 
> possible? makes it 101 chars.



Nope:
int c=b=j=0

if that was allowed we would have done it.
You can't do that in C++,
you have to declare a variable before you use it.

You need a C++ compiler if you want to start experimenting.


----------



## masterofthebass (Nov 16, 2010)

qqwref said:


> I don't speak Python, but wouldn't this part cause the code to never produce clockwise turns?



you're right :/ I had some other "optimizations" that cut it down to 149 now:

```
from random import randint as r;c=b=0
for i in range(25):
 m=r(0,5)
 while (c+b-4 or c==m)and m==b:m=r(0,5)
 print "URFBLD"[m]+" '2"[r(0,2)],;c=b;b=m
```

the "problem" is that it prints 2 spaces after a clockwise turn.


----------



## PatrickJameson (Nov 16, 2010)

guzman said:


> perl: *107*
> 
> ```
> map{$m=int rand 6until($c+$b-5||$c-$m)&&$m-$b;print+(U,R,F,B,L,D)[$m].qw(' 2)[rand 3]." ";$c=$b;$b=$m}1..25
> ```


 
As pointed out by Stefan before, 0| can be used instead of int making it 105 chars:


```
map{$m=0|rand 6until($c+$b-5||$c-$m)&&$m-$b;print+(U,R,F,B,L,D)[$m].qw(' 2)[rand 3]." ";$c=$b;$b=$m}1..25
```


----------



## Stefan (Nov 16, 2010)

Or 104 with the space before instead of after the turn:
map{$m=0|rand 6until($c+$b-5||$c-$m)&&$m-$b;print" ".(U,R,F,B,L,D)[$m].qw(' 2)[rand 3];$c=$b;$b=$m}1..25

101:
map{$m=0|rand 6until($c+$b-5||$c-$m)&&$m-$b;$c=$b;print" ".(U,R,F,B,L,D)[$b=$m].qw(' 2)[rand 3]}1..25


----------



## CharlesOBlack (Nov 16, 2010)

guzman said:


> Nope:
> int c=b=j=0
> 
> if that was allowed we would have done it.
> ...


 
I have Visual Studio already, I just wanted to know. >.>


----------



## qqwref (Nov 16, 2010)

Found an awesome way to test these things out yourself:

http://codepad.org/

EDIT: 100?
map{$m=0|rand 6until($c+$b-5|$c-$m)&&$m-$b;$c=$b;print" ".(U,R,F,B,L,D)[$b=$m].qw(' 2)[rand 3]}1..25


----------



## StachuK1992 (Nov 16, 2010)

qqwref said:


> Found an awesome way to test these things out yourself:
> 
> http://codepad.org/


 Ah, sorry. I should have mentioned this earlier; I thought the site was common knowledge. :/


----------



## Stefan (Nov 16, 2010)

qqwref said:


> EDIT: 100?
> map{$m=0|rand 6until($c+$b-5|$c-$m)&&$m-$b;$c=$b;print" ".(U,R,F,B,L,D)[$b=$m].qw(' 2)[rand 3]}1..25



That seems to work, but if it does, I'm surprised.

Here's another way to reach 100:
map{$m=0|rand 6while$c+$b==5&$c==$m|$m==$b;$c=$b;print" ".(U,R,F,B,L,D)[$b=$m].qw(' 2)[rand 3]}1..25


----------



## PatrickJameson (Nov 16, 2010)

qqwref said:


> EDIT: 100?
> map{$m=0|rand 6until($c+$b-5|$c-$m)&&$m-$b;$c=$b;print" ".(U,R,F,B,L,D)[$b=$m].qw(' 2)[rand 3]}1..25


 
ahhh, I was just about to post that. One more char to go!



Stefan said:


> That seems to work, but if it does, I'm surprised.



I tested it by doing this a few times: 


```
for(1..10000){map{$m=0|rand 6until($c+$b-5|$c-$m)&&$m-$b;if ($c+$b==5&&$m==$c) {print "NOPE"}$c=$b;" ".(U,R,F,B,L,D)[$b=$m].qw(' 2)[rand 3]}1..25}
```

So I guess it does.


----------



## PatrickJameson (Nov 16, 2010)

```
map{$m=0|rand 6until$c+$b-5|$c-$m&&$m-$b;$c=$b;print" ".(U,R,F,B,L,D)[$b=$m].qw(' 2)[rand 3]}1..25
```

Someone tell me why this is wrong. It couldn't have been that easy. It passes my testing(below), though. (98 chars).


```
for(1..10000){map{$m=0|rand 6until$c+$b-5|$c-$m&&$m-$b;if ($c+$b==5&&$c==$m||$m==$b) {print "NOPE"}$c=$b;" ".(U,R,F,B,L,D)[$b=$m].qw(' 2)[rand 3]}1..25}
```


----------



## Stefan (Nov 16, 2010)

You shouldn't do $b=$m before $c=$b.


----------



## PatrickJameson (Nov 16, 2010)

Stefan said:


> You shouldn't do $b=$m before $c=$b.


 
Ah, right. Edited.


----------



## Stefan (Nov 16, 2010)

Now prove that it works, I don't trust tests 

If you *compared* the values, you'd have 0 or 1 which you could safely "|" and "&". But since you subtract, you just get integers, and I don't see why it works. For example, 2&&1 is true while 2&1 is false.


----------



## Stefan (Nov 16, 2010)

Sorry, I misread, thought Michael and you used both "|" and "&", but you only use "|".


----------



## qqwref (Nov 16, 2010)

Yep, I tried using & but that broke it. I think it's because to get x|y=0 you need each bit of x and y to be 0 (i.e. x=y=0), whereas & does not work out as nicely.


----------



## PatrickJameson (Nov 16, 2010)

Ahh, I understand what's going on now with bitwise operations. At least for OR. When I did it I had no idea what was going to happen, I was just hoping for the best


----------



## cuBerBruce (Nov 16, 2010)

A GAP random-state scramble generator(169 characters, only the last 37 characters needed for additional scrambles - the text in red is one-time initialization code):



Spoiler



G:=Group(U,D,L,R,F,B);;P:=FreeGroup("U","D","L","R","F","B");;h:=GroupHomomorphismByImages(P,G,GeneratorsOfGroup(P),[noparse][U,D,L,R,F,B]);;[/noparse]
PreImagesRepresentative(h,Random(G));



OK, I cheated a bit. The output doesn't look exactly what it should look like. I also omitted initialization of globals U,D,L,R,F, and B (which also only needs to be done once, of course). (Note: The "forum" put in a couple of unwanted space characters, should be fairly obvious.)


----------



## qqwref (Nov 16, 2010)

cuBerBruce said:


> OK, I cheated a bit.


I'd say you cheated a lot :| The permutations U,F,R,D,L,B take a significant amount of space to define. If you don't count variable initializations then I claim the following JS code:


```
for(;j<k;c+b-5|c-m&&b-m?w(x[j++,c=b,b=m]+y[s()]+z):0)m=t()
```


----------



## guzman (Nov 16, 2010)

*Recap 2:*

Recap 2:

perl: *98* (edit, sub100!!) (Patrick, Johannes91, Stefan, qqwref)

```
map{$m=0|rand 6until$c+$b-5|$c-$m&&$m-$b;$c=$b;print" ".(U,R,F,B,L,D)[$b=$m].qw(' 2)[rand 3]}1..25
```


C++: *103 (111 with main(){} )* (edit) (keemy, Marcell, guzman, Portponky)

```
for(int c=0,b=6,j=25,m;j;(m=rand()%6)-c|c+b-5&&b-m&&cout<<"URFBLD"[j--,c=b,b=m]<<" 2'"[rand()%3]<<" ");
```

perl: *111* (Stefan)

```
$_.=chr rand 99,/[^URFBLD]|(.)\1|LRL|RLR|UDU|DUD|FBF|BFB/&&chop until/.{25}/;s/./" $&".qw(' 2)[rand 3]/ge;print
```

javascript: *114* (qqwref, Devin, guzman, Johannes91, Patrick, Stefan)

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

Ruby: *115* (joey)

```
p (x=y=0..25).map{z=x;x=(w=[%(UD),%(FB),%(RL)][rand 3])[rand 2] until x!=y||z!=x&&w[y];(y=x)+"2'"[rand 3].to_s}*" "
```

perl: *115* (Patrick, Stefan)

```
do{$_=join'',map{(U,R,F,B,L,D)[rand 6]}1..25}while/(.)\1|LRL|RLR|UDU|DUD|FBF|BFB/;print" $_".qw(' 2)[rand 3]for/./g
```

php: *116* (Marcell, guzman)

```
for($i=$b=$c=0;$i<25;$M="URFBLD 2'")if(($b-$m=rand()%6)&($c+$b-5|$c-$m))echo$M[$b=$m+0*$i++*$c=$b].$M[rand(6,8)].' '
```

python: *149* (masterofthebass)

```
from random import randint as r;c=b=0
for i in range(25):
 m=r(0,5)
 while (c+b-4 or c==m)and m==b:m=r(0,5)
 print "URFBLD"[m]+" '2"[r(0,2)],;c=b;b=m
```

mathematica: *166* (Lucas)

```
StringJoin[ToString[#]<>(c=RandomChoice)[{" ","' ","2 "}]&/@(Flatten[RandomSample[#,Ceiling[Random[]*5/4]]&/@c[{{U,R},{F,B},{L,D}},25]]//.{a___,b_,b_,c___}:>{a,b,c})]
```


----------



## guzman (Nov 16, 2010)

qqwref said:


> I'd say you cheated a lot :| The permutations U,F,R,D,L,B take a significant amount of space to define. If you don't count variable initializations then I claim the following JS code:
> 
> 
> ```
> ...


 


Or, just

p()

where the initialization of p is omitted.


----------



## Portponky (Nov 16, 2010)

The c++ algorithm can't produce a U turn on its first move. Initialize b to something like 6 to allow this.


----------



## guzman (Nov 16, 2010)

Portponky said:


> The c++ algorithm can't produce a U turn on its first move. Initialize b to something like 6 to allow this.


 
You're right, thanks, I'll update it.

Edit: same goes for javascript, php, python.
I think we should set b=c=j=6 and j<31.


----------



## PatrickJameson (Nov 16, 2010)

guzman said:


> Recap 2:
> (the 98 chars perl code was wrong, right?)


 
Nope, the 98 perl works . There was just some doubt for a bit.(aha, bit, get it? because the doubt was over bitwise operations. I'm hilarious).


----------



## guzman (Nov 16, 2010)

Nice!! 
ok, edited.


----------



## Stefan (Nov 16, 2010)

I think the 98er can't start with U turns, either.

guzman: It's python, not pyton.


----------



## guzman (Nov 16, 2010)

Sorry, edited.

I also added authors,
hope I didn't forget anyone.

Edit:



Stefan said:


> I think the 98er can't start with U turns, either.



Let's say we print 27 moves and it's up to the user to skip the first two moves ...


----------



## joey (Nov 16, 2010)

That 115 char ruby one is all mine


----------



## guzman (Nov 16, 2010)

joey said:


> That 115 char ruby one is all mine


Edited

Edit: or were you joking?


----------



## Marcell (Nov 16, 2010)

Damn, the timezones...
Anyway, it's really impressive what you did to that Perl code. And congrats to the sub100.

If only I could understand what the regex one does...


----------



## Portponky (Nov 16, 2010)

```
for(int j=0;j<25;j++)
for(int j=25;j;j--)
```

2 character saving for c++


```
for(int c=0,b=6,j=25,m;j;(m=rand()%6)-c|c+b-5&&b-m&&cout<<"URFBLD"[j--,c=b,b=m]<<" 2'"[rand()%3]<<" ");
```


----------



## guzman (Nov 16, 2010)

Portponky said:


> ```
> for(int j=0;j<25;j++)
> for(int j=25;j;j--)
> ```



Yeah, true. Nice one.


----------



## Marcell (Nov 16, 2010)

Portponky said:


> ```
> for(int j=0;j<25;j++)
> for(int j=25;j;j--)
> ```
> ...


 
Neat!


----------



## Kynit (Nov 16, 2010)

This thread is actually starting to scare me a little. 98 characters to make a legitimate scramble? Incredible.


----------



## Stefan (Nov 16, 2010)

Kynit said:


> 98 characters to make a legitimate scramble? Incredible.


 
And so far, not achieved.

Saving 1 by not wasting the loop body:
for(int c=0,b=6,j=25,m;j;m-c|c+b-5&&b-m&&cout<<"URFBLD"[j--,c=b,b=m]<<" 2'"[rand()%3]<<" ")m=rand()%6;

I guess another could be saved by making the variables global so we don't need the =0, but then we really ought to include and count "main...".


----------



## qqwref (Nov 16, 2010)

guzman said:


> You're right, thanks, I'll update it.
> 
> Edit: same goes for javascript, php, python.
> I think we should set b=c=j=6 and j<31.


Good point. I forgot about this effect when I changed the JS code. (Previously I had all the variables initialized to 4 for a similar reason.)

Using Portponky's idea of for(j=25;j;j--) I think we can save some moves in other ones too, like this for javascript:

```
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
```


----------



## cuBerBruce (Nov 16, 2010)

qqwref said:


> I'd say you cheated a lot :| The permutations U,F,R,D,L,B take a significant amount of space to define. If you don't count variable initializations then I claim the following JS code:
> 
> 
> ```
> ...



OK, I cheated a lot. I also note that the generated scrambles are quite long (on the order of 100 moves typically) and it doesn't necessarily obey the rule forbidding 3 consecutive turns on the same axis.

I hadn't worked on trying to optimize my initialization for U,D,L,R,F, and B yet when I posted previously. I've now come up with this (268 characters) for initializing these variables (although used like constants):



Spoiler



U:=(1,3,8,6)(2,5,7,4)(9,33,25,17)(10,34,26,18)(11,35,27,19);;L:=(9,11,16,14)(10,13,15,12)(1,17,41,40)(4,20,44,37)(6,22,46,35);;R:=(25,27,32,30)(26,29,31,28)(3,38,43,19)(5,36,45,21)(8,33,48,24);;x:=R*(2,39,42,18)(7,34,47,23)*L^-1;;D:=x^2*U*x^2;;F:=x*U*x^3;;B:=x*D*x^3;;



So yes, it's even longer than the rest of the code (posted earlier). Together it's 169 (corrected number for the earlier post) + 268 = 437. I note that the count includes the extra semicolon characters that avoids extraneous stuff from being output prior to the scramble being output.

The entire GAP code (and a sample output) is:


Spoiler





```
U:=(1,3,8,6)(2,5,7,4)(9,33,25,17)(10,34,26,18)(11,35,27,19);;L:=(9,11,16,14)(10,13,15,12)(1,17,41,40)(4,20,44,37)(6,22,46,35);;R:=(25,27,32,30)(26,29,31,28)(3,38,43,19)(5,36,45,21)(8,33,48,24);;x:=R*(2,39,42,18)(7,34,47,23)*L^-1;;D:=x^2*U*x^2;;F:=x*U*x^3;;B:=x*D*x^3;;G:=Group(U,D,L,R,F,B);;P:=FreeGroup("U","D","L","R","F","B");;h:=GroupHomomorphismByImages(P,G,GeneratorsOfGroup(P),[noparse][U,D,L,R,F,B]);;[/noparse]PreImagesRepresentative(h,Random(G));
U*R*B^-1*L*U*R*U^-1*B^-1*R*B*F*U^2*D^-1*B^-1*D*U^-1*F^2*D*F*D^-1*L^-1*R^-1*F^
-1*R*U^2*F*L*F^-1*L^-1*F^-1*B*L^-1*B^-1*U*B^-1*U*B*U^-1*L^-1*F^-1*L*F*L*U^
-1*L^-1*B^-1*U^-1*B*U^-1*L*U^-1*L^-1*U*L*U*F*U^-1*F^-1*U^2*L^-1*U^-1*L*U*L*F^
-1*L^-1*F*L*U^2*L^-1*U^-1*L*U^-1*L^-1*U*F*U*F^-1*L*U*L^-1*F*L*U^-1*L^-1*U^
-1*F^-1*U
```



Given that the shortest solution in Rokicki's cube solver contest was over 500 characters, this is probably still not too bad for a random state scramble generator.

(Since this scramble generator is non-conforming and is a different type of scramble generator, I suggest it should not be added to recaps.)

In addition to main(){...}, doesn't the C++ version (in order to use cout) need a #include line and possibly a "using namespace std;" declaration to make it valid? Of course, a #include would necessarily make it no longer a 1-liner.


----------



## mr. giggums (Nov 20, 2010)

170 in visual basic


```
For s = 1 To 25
c = b
b = a
Do While a = b Or a + b = 7 And a = c
a = Int(Rnd * 6 + 1)
Loop
t.Text = t.Text & Mid("URFBLD", a, 1) & Mid(" '2", Int(Rnd * 3 + 1), 1)
Next s
```

I'm a noob in VB so this can be improved.


----------



## guzman (Nov 20, 2010)

*Recap 3*

Recap 0.
Recap 1.
Recap 2.
Recap 3:

perl: *98* (Patrick, Johannes91, Stefan, qqwref) 
_This one doesn't give scrambles that start with U.
Which one was the last one correct ?_

```
map{$m=0|rand 6until$c+$b-5|$c-$m&&$m-$b;$c=$b;print" ".(U,R,F,B,L,D)[$b=$m].qw(' 2)[rand 3]}1..25
```

perl: *102* (edit: solves above problem) (Patrick, Johannes91, Stefan, qqwref) 

```
map{$m=0|rand 6until$c+$b-5|$c ne$m&&$m ne$b;$c=$b;print" ".(U,R,F,B,L,D)[$b=$m].qw(' 2)[rand 3]}1..25
```


C++: *102 (110 with main(){} )* (keemy, Marcell, guzman, Portponky, Stefan)

```
for(int c=0,b=6,j=25,m;j;m-c|c+b-5&&b-m&&cout<<"URFBLD"[j--,c=b,b=m]<<" 2'"[rand()%3]<<" ")m=rand()%6;
```

perl: *111* (Stefan)

```
$_.=chr rand 99,/[^URFBLD]|(.)\1|LRL|RLR|UDU|DUD|FBF|BFB/&&chop until/.{25}/;s/./" $&".qw(' 2)[rand 3]/ge;print
```

javascript: *112* (qqwref, Devin, guzman, Johannes91, Patrick, Stefan)

```
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
```

perl: *114* (Edit) (Patrick, Stefan, Johannes91)

```
$_=join'',map{(U,R,F,B,L,D)[rand 6]}1..25while/^$|(.)\1|LRL|RLR|UDU|DUD|FBF|BFB/;print" $_".qw(' 2)[rand 3]for/./g
```

Ruby: *115* (joey)

```
p (x=y=0..25).map{z=x;x=(w=[%(UD),%(FB),%(RL)][rand 3])[rand 2] until x!=y||z!=x&&w[y];(y=x)+"2'"[rand 3].to_s}*" "
```

php: *114* (Marcell, guzman)

```
for($i=$b=$c=25;$i;$M="URFBLD 2'")if(($b-$m=rand()%6)&($c+$b-5|$c-$m))echo$M[$b=$m+0*$i--*$c=$b].$M[rand(6,8)].' '
```

python: *149* (masterofthebass)

```
from random import randint as r;c=b=0
for i in range(25):
 m=r(0,5)
 while (c+b-4 or c==m)and m==b:m=r(0,5)
 print "URFBLD"[m]+" '2"[r(0,2)],;c=b;b=m
```

mathematica: *166* (Lucas)

```
StringJoin[ToString[#]<>(c=RandomChoice)[{" ","' ","2 "}]&/@(Flatten[RandomSample[#,Ceiling[Random[]*5/4]]&/@c[{{U,R},{F,B},{L,D}},25]]//.{a___,b_,b_,c___}:>{a,b,c})]
```

Visual Basic: *170* (mr. giggums)

```
For s = 1 To 25
c = b
b = a
Do While a = b Or a + b = 7 And a = c
a = Int(Rnd * 6 + 1)
Loop
t.Text = t.Text & Mid("URFBLD", a, 1) & Mid(" '2", Int(Rnd * 3 + 1), 1)
Next s
```


----------



## Stefan (Nov 20, 2010)

guzman said:


> perl: *98* (Patrick, Johannes91, Stefan, qqwref)
> _This one doesn't give scrambles that start with U.
> Which one was the last one correct ?_


 
Not sure it ever was. But here's one with 100 that fixes that:
map{$m=0|rand 6until$c+$b-5|$c-$m&&$m ne$b;$c=$b;print" ".(U,R,F,B,L,D)[$b=$m].qw(' 2)[rand 3]}1..25

We might need to fix $c-$m as well, though... not sure.

Edit: I thought this would prevent starting with DU, but I tested it and that does work. Why? I thought it would be:
$c is undefined
$b is 5
$m is 0
So $c+$b-5|$c-$m would be 0 and thus false and thus prevent a DU start...

Edit2: Embarrassing... I tested it inside a loop and forgot to undef the variables...


----------



## guzman (Nov 20, 2010)

I thought the same but I dont know perl ...
I thought $c+$b-5|$c-$m was undefined and therefore not true ...

Anyway, I edited the recap.

*Edit:*
I think that c+b-5 is not undef
it is actually evaluated as 0+b-5 since c is treated as zero and this solves the problem.
I found a doc that says: "Perl treats undef as either zero or the empty string"
therefore I think it is consider false in boolean operations
but it is considered as 0 in arithmetical operations.


----------



## Stefan (Nov 20, 2010)

guzman said:


> I think that c+b-5 is not undef
> it is actually evaluated as 0+b-5 since c is treated as zero and this solves the problem.



Yes, undef is treated as zero in such numerical context, but then c+b+5 is 0 and c-m is 0 as well, so or-ing them is again 0 and we have the problem.


----------



## guzman (Nov 20, 2010)

Oops. right ...

So, according to your second edit,
the DU problem actually exists, right ?


----------



## Stefan (Nov 20, 2010)

guzman said:


> the DU problem actually exists, right ?


 
Yes.

I really like
do{$_=join'',map{(U,R,F,B,L,D)[rand 6]}1..25}while/(.)\1|LRL|RLR|UDU|DUD|FBF|BFB/;print" $_".qw(' 2)[rand 3]for/./g
best now... so clean and obvious...


----------



## Johannes91 (Nov 20, 2010)

qqwref said:


> I'm not actually sure the normal scrambling algorithm does this. Consider 3-move scrambles, ignoring suffixes. The probability of the first two moves being R B and R L are exactly the same (1/30). However, R B can be followed by 5 moves (so each 3-move scramble starting with R B has probability 1/150) but R L can be followed by only 4 moves (so each 3-move scramble starting with R L has probability 1/120). The same thing should happen with more moves.



Hmm. Good point.

Removing do{} and adding ^$| to the regex saves on character:

$_=join'',map{(U,R,F,B,L,D)[rand 6]}1..25while/^$|(.)\1|LRL|RLR|UDU|DUD|FBF|BFB/;print" $_".qw(' 2)[rand 3]for/./g


----------



## guzman (Nov 20, 2010)

*Probability of moves on the same axis ...*

May I repost this to ask for further considerations ....
I edited it a bit.



guzman said:


> Consider a scramble.
> What is the probability of having two consecutive letters on the same axis (such as R and L).
> 
> Say we begin with an R, the following letter is any letter chosen randomly among UFLBD, so the probability of having L is 1/5.
> ...


----------



## qqwref (Nov 20, 2010)

guzman: I'm not sure that would provide a uniform scramble setup - so far the only thing I'm sure will work is do{generate scramble}until(valid). And it's definitely more complex than the current approach.

I tried a few things to fix the Perl code but the only way I could see to do it was to plunk in a $b=7; before the map statement. :fp


----------



## Stefan (Nov 20, 2010)

qqwref said:


> I tried a few things to fix the Perl code but the only way I could see to do it was to plunk in a $b=7; before the map statement. :fp


 
Have you seen mine? I replaced *$m-$b* with *$m ne$b*, the ne-operator creates a string context which fixes that problem (because undefined is treated as '', which differs from '0'). But we both still have the problem that $c also starts undefined, preventing a DU start...


----------



## qqwref (Nov 20, 2010)

Stefan said:


> Have you seen mine? I replaced *$m-$b* with *$m ne$b*, the ne-operator creates a string context which fixes that problem (because undefined is treated as '', which differs from '0'). But we both still have the problem that $c also starts undefined, preventing a DU start...


 
I did see yours, yes. But your post made it sound like it didn't work, so I assumed your fix did nothing useful.

Why wouldn't this work?:

```
map{$m=0|rand 6until$c+$b-5|$c ne$m&&$m ne$b;$c=$b;print" ".(U,R,F,B,L,D)[$b=$m].qw(' 2)[rand 3]}1..25
```


----------



## Stefan (Nov 20, 2010)

I think I tried that, don't remember what my result was.

Btw, just putting $b=7; in front makes it worse, then the scrambles *always* start with U.


----------



## Stefan (Nov 20, 2010)

qqwref said:


> Why wouldn't this work?:
> 
> ```
> map{$m=0|rand 6until$c+$b-5|$c ne$m&&$m ne$b;$c=$b;print" ".(U,R,F,B,L,D)[$b=$m].qw(' 2)[rand 3]}1..25
> ```


 
Seems (*) to work, maybe I dismissed it when my testing was flawed.

(*) as in "it passes my test, but I'm unsure about it and too tired to think it through"


----------



## Stefan (Nov 20, 2010)

$= is a special variable that's initialized with 60 on my computer and probably at least a similarly high number elsewhere. Wanna exploit this?


----------



## qqwref (Nov 21, 2010)

$= is "Current page length", whatever that means.

Could this actually save any characters, though?


----------



## Carrot (Nov 21, 2010)

mr. giggums said:


> 170 in visual basic
> 
> 
> ```
> ...



I don't know much VB, but I think this can be applied:

```
t.Text += Mid("URFBLD", a, 1) & Mid(" '2", Int(Rnd * 3 + 1), 1)
```
saving 7 chars


EDIT:

The python one didn't work( AT ALL!!! Couldn't start with U, could do the same move twice and so on...)... so I fixed it and saved some chars 


```
from random import randint as r;c=b=m=9
for i in range(25):
 while c+b-4 and m==c or m==b:m=r(0,5)
 print "URFBLD"[m]+" '2"[r(0,2)],;c=b;b=m
```

instead of calling m=r(0,5) inside the loop, I called it as 9 (because everything that is 5 or below will restrict a beginning move) together with the c and b...
I also fixed an 'and/or' confusion, and I deleting unecessary paranthesis 

this makes it 140 chars... and working 

EDIT2:
with my little neat b=9, I fixed the perl, making it a 103 chars ... EDIT3: it can only start with U now :S

```
$b=9;map{$m=0|rand 6until$c+$b-5|$c-$m&&$m-$b;$c=$b;print" ".(U,R,F,B,L,D)[$b=$m].qw(' 2)[rand 3]}1..25
```


----------



## qqwref (Nov 21, 2010)

Alright, I think the normal 3x3 scrambler is getting too easy. Time to make it a little trickier. Here is a *pyraminx optimal scrambler* to try to code-golf. (Written in JavaScript of course.) It's kinda long, you might wanna copy it to Notepad or something before editing.


```
var j=1,b,mn=11,g=[],f=[],d=[],e=[],k=[],h=[],i=[];function u(){var c,p,q,l,m;for(p=0;p<720;p++){g[p]=-1;d[p]=[];for(m=0;m<4;m++)d[p][m]=w(p,m)}g[0]=0;for(l=0;l<=6;l++)for(p=0;p<720;p++)if(g[p]==l)for(m=0;m<4;m++){q=p;for(c=0;c<2;c++){q=d[q][m];if(g[q]==-1)g[q]=l+1}}for(p=0;p<2592;p++){f[p]=-1;e[p]=[];for(m=0;m<4;m++)e[p][m]=x(p,m)}f[0]=0;for(l=0;l<=5;l++)for(p=0;p<2592;p++)if(f[p]==l)for(m=0;m<4;m++){q=p;for(c=0;c<2;c++){q=e[q][m];if(f[q]==-1)f[q]=l+1}}for(c=0;c<j;c++){k=[];var t=0,s=0;q=0;h=[0,1,2,3,4,5];for(m=0;m<4;m++){p=m+n(6-m);l=h[m];h[m]=h[p];h[p]=l;if(m!=p)s++}if(s%2==1){l=h[4];h[4]=h[5];h[5]=l}s=0;i=[];for(m=0;m<5;m++){i[m]=n(2);s+=i[m]}i[5]=s%2;for(m=6;m<10;m++){i[m]=n(3)}for(m=0;m<6;m++){l=0;for(p=0;p<6;p++){if(h[p]==m)break;if(h[p]>m)l++}q=q*(6-m)+l}for(m=9;m>=6;m--)t=t*3+i[m];for(m=4;m>=0;m--)t=t*2+i[m];if(q!=0||t!=0)for(m=mn;m<99;m++)if(v(q,t,m,-1))break;b="";for(p=0;p<k.length;p++)b+="ULRB"[k[p]&7]+["","'"][(k[p]&8)/8]+" ";var a="lrbu";for(p=0;p<4;p++){q=n(3);if(q<2)b+=a[p]+["","'"][q]+" "}}}function v(q,t,l,c){if(l==0){if(q==0&&t==0)return true}else{if(g[q]>l||f[t]>l)return false;var p,s,a,m;for(m=0;m<4;m++)if(m!=c){p=q;s=t;for(a=0;a<2;a++){p=d[p][m];s=e[s][m];k[k.length]=m+8*a;if(v(p,s,l-1,m))return true;k.length--}}}return false}function w(p,m){var a,l,c,s=[],q=p;for(a=1;a<=6;a++){c=0|(q/a);l=q-a*c;q=c;for(c=a-1;c>=l;c--)s[c+1]=s[c];s[l]=6-a}if(m==0)y(s,0,3,1);if(m==1)y(s,1,5,2);if(m==2)y(s,0,2,4);if(m==3)y(s,3,4,5);q=0;for(a=0;a<6;a++){l=0;for(c=0;c<6;c++){if(s[c]==a)break;if(s[c]>a)l++}q=q*(6-a)+l}return q}function x(p,m){var a,l,c,t=0,s=[],q=p;for(a=0;a<=4;a++){s[a]=q&1;q>>=1;t^=s[a]}s[5]=t;for(a=6;a<=9;a++){c=0|(q/3);l=q-3*c;q=c;s[a]=l}if(m==0){s[6]++;if(s[6]==3)s[6]=0;y(s,0,3,1);s[1]^=1;s[3]^=1}if(m==1){s[7]++;if(s[7]==3)s[7]=0;y(s,1,5,2);s[2]^=1;s[5]^=1}if(m==2){s[8]++;if(s[8]==3)s[8]=0;y(s,0,2,4);s[0]^=1;s[2]^=1}if(m==3){s[9]++;if(s[9]==3)s[9]=0;y(s,3,4,5);s[3]^=1;s[4]^=1}q=0;for(a=9;a>=6;a--)q=q*3+s[a];for(a=4;a>=0;a--)q=q*2+s[a];return q}function y(p,a,c,t){var s=p[a];p[a]=p[c];p[c]=p[t];p[t]=s}function n(c){return 0|Math.random()*c}u();document.write(b);
```


----------



## Carrot (Nov 21, 2010)

qqwref: no, just no xD


----------



## guzman (Nov 21, 2010)

qqwref said:


> Alright, I think the normal 3x3 scrambler is getting too easy. Time to make it a little trickier. Here is a *pyraminx optimal scrambler* to try to code-golf. (Written in JavaScript of course.) It's kinda long, you might wanna copy it to Notepad or something before editing.
> 
> 
> ```
> ...


 

Optimizing that could be total madness (I've counted 34 for cycles nested in various ways) ... but it could also be fun.
The code right now is 2119 characters.
Before we try,
could you explain us how an optimal scrambler for pyraminx works
and how this specfic scrambler works.
In particular: what is ther role of the functions u, v, w, x ?

Or, for those ,like me, who don't know how it works:
we may try to optimize it just by looking at the code ...

Finally, if we ever optimize it, how many characteres do you think it could reach ?
1200 ? 1000 ? less ?


----------



## Carrot (Dec 8, 2010)

Perl: 100 chars 

```
map{$m=0|rand 6until$c+$b-5|$c-$m&&$m ne$b;$c=$b;print" ".(U,R,F,B,L,D)[$b=$m].qw(' 2)[rand 3]}1..25
```

I can't find any bugs in this code.. 

(sorry for bumping, but 100 chars is better than the previous 102 chars PERL )


----------



## guzman (Dec 8, 2010)

the problem is it cannot give scrambles starting with U ...


----------



## Carrot (Dec 8, 2010)

guzman said:


> the problem is it cannot give scrambles starting with U ...


 

Yes it can 

EDIT:


> Perl, pasted just now:
> 1
> map{$m=0|rand 6until$c+$b-5|$c-$m&&$m ne$b;$c=$b;print" ".(U,R,F,B,L,D)[$b=$m].qw(' 2)[rand 3]}1..25
> 
> ...


----------



## guzman (Dec 8, 2010)

Oh, well ... 
I actually dont know perl, that's what I was told ... If I'm not confused.


----------



## Carrot (Dec 8, 2010)

The previous code(98 chars one) didn't work, that's why I fixed the minor bug that made it not work  (based on the idea of the 102 solution ^^)


----------



## guzman (Dec 8, 2010)

*Recap 4*

Recap 0.
Recap 1.
Recap 2.
Recap 3.
Recap 4:


perl: *102* (Patrick, Johannes91, Stefan, qqwref) 

```
map{$m=0|rand 6until$c+$b-5|$c ne$m&&$m ne$b;$c=$b;print" ".(U,R,F,B,L,D)[$b=$m].qw(' 2)[rand 3]}1..25
```


C++: *102 (110 with main(){} )* (keemy, Marcell, guzman, Portponky, Stefan)

```
for(int c=0,b=6,j=25,m;j;m-c|c+b-5&&b-m&&cout<<"URFBLD"[j--,c=b,b=m]<<" 2'"[rand()%3]<<" ")m=rand()%6;
```

perl: *111* (Stefan)

```
$_.=chr rand 99,/[^URFBLD]|(.)\1|LRL|RLR|UDU|DUD|FBF|BFB/&&chop until/.{25}/;s/./" $&".qw(' 2)[rand 3]/ge;print
```

javascript: *112* (qqwref, Devin, guzman, Johannes91, Patrick, Stefan)

```
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
```

perl: *114* (Patrick, Stefan, Johannes91)

```
$_=join'',map{(U,R,F,B,L,D)[rand 6]}1..25while/^$|(.)\1|LRL|RLR|UDU|DUD|FBF|BFB/;print" $_".qw(' 2)[rand 3]for/./g
```

Ruby: *115* (joey)

```
p (x=y=0..25).map{z=x;x=(w=[%(UD),%(FB),%(RL)][rand 3])[rand 2] until x!=y||z!=x&&w[y];(y=x)+"2'"[rand 3].to_s}*" "
```

php: *114* (Marcell, guzman)

```
for($i=$b=$c=25;$i;$M="URFBLD 2'")if(($b-$m=rand()%6)&($c+$b-5|$c-$m))echo$M[$b=$m+0*$i--*$c=$b].$M[rand(6,8)].' '
```

python: *139* (masterofthebass, Odder) (Edited)

```
from random import randint as r
m=b=9
for u in range(25):
 c=b;b=m
 while c+b-4 and m==c or m==b:m=r(0,5)
 print "URFBLD"[m]+" '2"[r(0,2)],
```

mathematica: *166* (Lucas)

```
StringJoin[ToString[#]<>(c=RandomChoice)[{" ","' ","2 "}]&/@(Flatten[RandomSample[#,Ceiling[Random[]*5/4]]&/@c[{{U,R},{F,B},{L,D}},25]]//.{a___,b_,b_,c___}:>{a,b,c})]
```

Visual Basic: *170* (mr. giggums)

```
For s = 1 To 25
c = b
b = a
Do While a = b Or a + b = 7 And a = c
a = Int(Rnd * 6 + 1)
Loop
t.Text = t.Text & Mid("URFBLD", a, 1) & Mid(" '2", Int(Rnd * 3 + 1), 1)
Next s
```


----------



## Carrot (Dec 8, 2010)

You forgot my Python(140 chars):

```
from random import randint as r
c=m=b=9
for u in range(25):
 while c+b-4 and m==c or m==b:m=r(0,5)
 print "URFBLD"[m]+" '2"[r(0,2)],;c=b;b=m
```

it is based on the code from masterofthebass' code

EDIT:
saved 1 char  (and should still be working 

```
from random import randint as r
m=b=9
for u in range(25):
 c=b;b=m
 while c+b-4 and m==c or m==b:m=r(0,5)
 print "URFBLD"[m]+" '2"[r(0,2)],
```


----------



## guzman (Dec 8, 2010)

Odder said:


> You forgot my Python(140 chars):



I'm sorry, edited.


----------



## Carrot (Dec 8, 2010)

I just edited it and shortened it to 139 chars xD (in my prev. post)


----------



## Stefan (Dec 8, 2010)

Odder said:


> Perl: 100 chars
> 
> ```
> map{$m=0|rand 6until$c+$b-5|$c-$m&&$m ne$b;$c=$b;print" ".(U,R,F,B,L,D)[$b=$m].qw(' 2)[rand 3]}1..25
> ```


 
That's the same I posted earlier, and like I said before, this does not create scrambles starting with DU.


----------



## Carrot (Dec 8, 2010)

Stefan said:


> That's the same I posted earlier, and like I said before, this does not create scrambles starting with DU.



My fault :S (I didn't see your bugged 100 char solution) but after reading my own code it's pretty clear that it can never start with DU, so 102 is definitely the shortest yet =)


----------



## qqwref (Dec 8, 2010)

guzman said:


> Before we try,
> could you explain us how an optimal scrambler for pyraminx works
> and how this specfic scrambler works.
> In particular: what is ther role of the functions u, v, w, x ?


I don't really know the details. The general way it works is to set up some permutation/orientation tables (which I guess explain to the program what each move does) and then does a search, starting at 1 move and going up, to see if any sequences of that length solve the position. The "u();document.write(b);" part calls what is necessary for the scrambler.



guzman said:


> Finally, if we ever optimize it, how many characteres do you think it could reach ?
> 1200 ? 1000 ? less ?


Well, it's already condensed and optimized a bit (see the variable names etc.) so I doubt you'll get a huge amount out of it, maybe down to 1500 or so if the approach doesn't change.


----------



## StachuK1992 (Dec 10, 2010)

http://ideone.com
Just thought I'd share this.

I know codepad has been mentioned, but this seems to have a bit more backbone to it.


----------



## bamilan (Feb 3, 2011)

In the python one, part of last line:
+" '2"[r(0,2)]
Why is the space there?
Instead, this is better:
+"'2"[r(0,1)]
Saving 1 char...


----------



## Carrot (Feb 3, 2011)

bamilan said:


> In the python one, part of last line:
> +" '2"[r(0,2)]
> Why is the space there?
> Instead, this is better:
> ...


 
Then you can't get moves such as R L B U D or F, but only R' L' F2 U2 D2 B' etc...


----------



## bamilan (Feb 3, 2011)

Ah, ok.
But I can win few chars, if instead of randint I import randrange.
It takes 1 argument instead of 2:

from random import randrange as r
m=b=9
for u in range(25):
c=b;b=m
while c+b-4 and m==c or m==b:m=r(5)
print "URFBLD"[m]+" '2"[r(2)],

This is 2 chars less.


----------



## sz35 (Jan 2, 2012)

If I do that on C++, does the count have to include the includes and the "void main" or only the line? 

The line is as follows:
for(int i=25;i;i--)printf("%c%c ","UDFBRL"[rand()%6]," 2'"[rand()%3]);

(70 chars)

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


----------



## Owen (Jan 2, 2012)

Someone should start an official scrambler programming contest.

Here's my pathetic attempt in Ruby:


m= ["R", "U", "D", "B", "F", "L"]
x = [" ", "2 ", "' "]
o = 0
d = 0
while d < 26
n = m.at(rand(6))
if n != o
print n
o = n
print x.at(rand(3))
d = d + 1
end
end


148 characters.


----------



## qqwref (Jan 2, 2012)

sz35 said:


> If I do that on C++, does the count have to include the includes and the "void main" or only the line?
> 
> The line is as follows:
> for(int i=25;i;i--)printf("%c%c ","UDFBRL"[rand()%6]," 2'"[rand()%3]);
> ...


This wouldn't quite work. There is no provision in your code to prevent sequences such as R R or R L R.



Owen said:


> Someone should start an official scrambler programming contest.


I don't know about official, but this topic was a pretty decent contest.


----------



## Carrot (Jan 2, 2012)

Owen said:


> Someone should start an official scrambler programming contest.
> 
> Here's my pathetic attempt in Ruby:
> 
> ...


 

Doesn't work..
U' D' F' L2 F2 D2 R' *D2 U2 D'* F D L B D' R B' D' R L B R2 L2* B' F B' *

And it doesn't allow R as the first move if I'm right


----------



## Owen (Jan 2, 2012)

Odder said:


> Doesn't work..
> U' D' F' L2 F2 D2 R' *D2 U2 D'* F D L B D' R B' D' R L B R2 L2* B' F B' *
> 
> And it doesn't allow R as the first move if I'm right


 
Blah. I'm bad at programming. I shortened all the variables, so now I can't even understand how it works.

But it can start with R, I don't know where you got that idea.


EDIT:

http://codepad.org/7EHN0e5M

This appears to solve the problem, but it seems too good to be true...


----------



## Carrot (Jan 2, 2012)

Owen said:


> Blah. I'm bad at programming. I shortened all the variables, *so now I can't even understand how it works*.
> 
> But it can start with R, I don't know where you got that idea.
> 
> ...


 
*True, it didn't solve any problems*

Because of this line:
if n != o
you claim o=0 in the beginning, so n cannot be 0 for the first run, therefore you'll never get position 0 (R move) for the first move

This is my attempt to save your code:


```
m,x=["R","U","B","F","D","L"],[" ","2 ","' "]
a=b=c=d=6
while d<31
a=rand(6)
if a!=b&&!(a==c&&b+a=5)
print m.at(a)+x.at(rand(3))
c,b=b,a
d+=1
end
end
```


----------



## joey (Jan 3, 2012)

```
b=c=d=6
while d<32
if (a=rand 6)!=b&&!(a==c&&b+a==5)
$><<%w[R U B F D L][a]+[" ","2 ","' "][rand 3]
c,b=b,a
d+=1
end
end
```

Just a few mins on it.


----------



## Owen (Jan 3, 2012)

Nice job!


----------



## sz35 (Jan 3, 2012)

qqwref said:


> This wouldn't quite work. There is no provision in your code to prevent sequences such as R R or R L R.


Right.

Fixed solution:
for(int i=25,t=6,k;i;(k=rand()%6)-t&&t%2-1|k-t+1&&printf("%c%c ","UDFBRL"[i--,t=k]," 2'"[rand()%3]));
101 chars.

I wanted to write on pure C, but cout makes it shorter:
for(int i=25,t=6,k;i;(k=rand()%6)-t&&t%2-1|k-t+1&&cout<<"UDFBRL"[i--,t=k]<<" 2'"[rand()%3]<<" ");
97 chars

L after R is possible but R after L isn't. Each L->R move can be shown as R->L, and we avoid R->L->R.


----------



## Carrot (Jan 3, 2012)

joey said:


> ```
> b=c=d=6
> while d<32
> if (a=rand 6)!=b&&!(a==c&&b+a==5)
> ...


 

```
a=b=d=6
while d<32
c,b=b,a
while c+b==5&&a==c||a==b:a=rand 6;end
$><<%w[R U B F D L][a]+[" ","2 ","' "][rand 3]
d+=1
end
```
(120 chars) damn... same number of chars


----------



## qq280833822 (Jan 3, 2012)

```
b=d=6
while d<32
if (a=rand 6)!=b&&a!=b+3
$><<%w[U R F D L B][b=a]+[" ","2 ","' "][rand 3]
d+=1
end
end
```
(103 chars)


----------



## joey (Jan 3, 2012)

Odder.. edit your post =D


----------



## Stefan (Jan 3, 2012)

Odder said:


> Because of this line:
> if n != o
> you claim o=0 in the beginning, so n cannot be 0 for the first run, therefore you'll never get position 0 (R move) for the first move


 
Is that some weird Ruby stuff I don't know? I'd say n=m.at(rand(6)) never gets you n=0, just n="R". So that shouldn't be a problem.


----------



## Owen (Jan 3, 2012)

I think so, because my original code generated scrambles beginning with R on multiple occasions.


----------



## Carrot (Jan 3, 2012)

Stefan said:


> Is that some weird Ruby stuff I don't know? I'd say n=m.at(rand(6)) never gets you n=0, just n="R". So that shouldn't be a problem.


 


Owen said:


> I think so, because my original code generated scrambles beginning with R on multiple occasions.


 
I'm no good at Ruby  Ruby is retarded xD I'was god damn sure that rand(6) generated 0-5 -.-' (I generated like 30 scrambles without a leading R move :O )


EDIT:

Chen Shuang:
Your distribution is wrong..

```
U->L=25%
D->L=20%
```


----------



## Stefan (Jan 3, 2012)

Odder said:


> I'was god damn sure that rand(6) generated 0-5


 
And it probably does. But the code says n=m.at(rand(6)), not n=rand(6).


----------



## Carrot (Jan 3, 2012)

Stefan said:


> And it probably does. But the code says n=m.at(rand(6)), not n=rand(6).



Ohh ****... Can you tell me the best place to hide?  (I had already changed his code inside my head when I commented his code... stupid Odder)


----------



## qq280833822 (Jan 3, 2012)

Its algorithm can be describe as:
generate random moves
and then check it, let RR->R, LL->L, RL->LR, and UD, FB as well
is it really wrong?


----------



## Carrot (Jan 3, 2012)

qq280833822 said:


> Its algorithm can be describe as:
> generate random moves
> and then check it, let RR->R, LL->L, RL->LR, and UD, FB as well
> is it really wrong?


 
I'm pretty confident that I'm right. But I'm already looking for a place to hide when you prove me wrong.

(a=rand 6)!=b&&a!=b+3

same as:
a!=b and a!=b+3

well, a!=b is obvious..

a!=b+3 can be described like this:
setA=[U,R,F], setB=[D,L,B]
then we have: setA[x]+3=setB[x], where x is the position.
but setB[x]+3 != setA[x].... (as 5+3 != 2)
this means that for any moves in setA, there is 4 valid next-moves, but for setB we have 5.

The goal for the scrambles is to have:
5 valid moves, unless we have just had two opposite moves in a row, then it should be 4


----------



## qq280833822 (Jan 3, 2012)

why should valid moves of setA be the same as of setB
if we only allow "LR" but disallow "RL", of course setA is difference to setB...


----------



## Carrot (Jan 3, 2012)

Imagine you have the move R

R doesn't allow the next move to be L, therefore you can't get RL.. (RL is not converted to LR) (which leaves 4 valid moves only)

now take L

L does allow the next move to be R... (5 valid moves, YAY!! )


----------



## Aditya Sriram (Mar 6, 2012)

*Another Python code(prevents R R' or R L)*

I know the code doesn't suit the title and it is slightly long, but my purpose was to avoid Cancellation moves.
Here is my code (Python 2.7)


```
import random                                                                   

no=input("Input the number of scrambles you require.")                          
length=input("Input the length of the scramble.")                               
scramble=''

set_1=[" R"," R'"," R2"," L"," L'"," L2"]                                       
set_2=[" F"," F'"," F2"," B"," B'"," B2"," U"," U'"," U2"," D"," D'"," D2"]     

for i in range(no):                                                             
    scramble= scramble + '('+str(i+1)+')'
    for i in range(length):                                                     
        if i%2 == 0:                                                            
            scramble=scramble + random.choice(set_1)
        else:
            scramble=scramble + random.choice(set_2)
    print scramble                                                              
                                                                                
    print "\n"                                                                  
    scramble=''
```


Output:

```
(1) L2 D' L2 B' L U2 L' U L2 U2 R' F2 R2 D2 R' U2 R U2 L' D' R' F2 R' F2 L'
(2) L2 U' R2 U2 R' B' R2 F2 L' F L' F' L' B L2 U2 L2 B L' F R2 D2 L2 D R2
(3) L F2 L' U' R' B2 L F2 L B2 L U L' D2 R' D' R' U2 R F2 R U R2 F2 R'
(4) R2 F2 R2 U' R U2 L2 B R2 B R B R F2 R' F R2 D' L B' R F2 L B2 R
(5) L F R D R D L D' R' B2 R2 F R2 U2 R D2 L' U' R' U R2 B' R2 U' L'
```


----------



## qqwref (Mar 6, 2012)

Your code seems to make every other move an R or L turn, and the rest are never R or L turns. This does avoid canceling moves but it also provides far less possibilities than a normal scramble. The other scramblers in the thread were made to use all of the moves equally but also avoid cancellations - it's a little trickier, but see if you can make one that does that.


----------



## qq280833822 (Apr 3, 2012)

1429-characters random-state 2x2 scrambler, and 1798-characters random-state pyraminx scrambler 


```
var get2x2optscramble=function(){function o(a,c,b,d,e){if(0!=b){if(j[a]>b||k[c]>b)return!1;var h,g,f,i;for(i=0;3>i;i++)if(i!=d){h=a;g=c;for(f=0;3>f;f++)if(h=m[h][i],g=n[g][i],o(h,g,b-1,i,e))return e.push("URF".charAt(i)+" 2'".charAt(f)),!0}}else return 0==a&&0==c}function p(){p=function(){};var a,c,b,d,e;for(a=0;5040>a;a++){j[a]=-1;m[a]=[];for(c=0;3>c;c++)m[a][c]=q(a,c)}for(a=j[0]=0;7>a;a++)for(c=0;5040>c;c++)if(j[c]==a)for(b=0;3>b;b++){d=c;for(e=0;3>e;e++)d=m[d][b],0>j[d]&&(j[d]=a+1)}for(a=0;729>a;a++){k[a]=-1;n[a]=[];for(c=0;3>c;c++)n[a][c]=r(a,c)}for(a=k[0]=0;6>a;a++)for(c=0;729>c;c++)if(k[c]==a)for(b=0;3>b;b++){d=c;for(e=0;3>e;e++)d=n[d][b],0>k[d]&&(k[d]=a+1)}}function q(a,c){var b,d,e,h=a,g=[];for(b=1;7>=b;b++){d=h%b;h=(h-d)/b;for(e=b-1;e>=d;e--)g[e+1]=g[e];g[d]=7-b}0==c?l(g,1,3,2):1==c?l(g,4,5,1):2==c&&l(g,2,6,4);for(b=h=0;7>b;b++){for(e=d=0;7>e&&!(g[e]==b);e++)g[e]>b&&d++;h=h*(7-b)+d}return h}function r(a,c){var b,d,e,h=0,g=a,f=[];for(b=0;5>=b;b++)e=~~(g/3),d=g-3*e,g=e,f[b]=d,h-=d,0>h&&(h+=3);f[6]=h;0==c?l(f,1,3,2):1==c?(l(f,4,5,1),f[0]+=2,f[1]++,f[5]+=2,f[4]++):2==c&&(l(f,2,6,4),f[2]+=2,f[0]++,f[4]+=2,f[6]++);g=0;for(b=5;0<=b;b--)g=3*g+f[b]%3;return g}function l(a,c,b,d){var e=a[0];a[0]=a[c];a[c]=a[b];a[b]=a[d];a[d]=e}var j=[],k=[],m=[],n=[];return function(a){p();var c=~~(5040*Math.random()),b=~~(729*Math.random()),d=[];if(0!=c||0!=b)for(;99>a&&!o(c,b,a,-1,d);a++);return d.reverse().join(" ")}}();

//1429 chars


var getpyraoptscramble=(function(){function r(){function l(a,c){for(var e=[],j=5517840,f=0,b=0;5>b;b++){var h=k[5-b],d=~~(a/h),a=a-d*h,f=f^d,d=d<<2;e[b]=j>>d&15;h=(1<<d)-1;j=(j&h)+(j>>4&~h)}0==(f&1)?e[5]=j:(e[5]=e[4],e[4]=j);0==c&&g(e,0,3,1);1==c&&g(e,1,5,2);2==c&&g(e,0,2,4);3==c&&g(e,3,4,5);a=0;j=5517840;for(b=0;4>b;b++)d=e[b]<<2,a*=6-b,a+=j>>d&15,j-=1118480<<d;return a}function i(a,c){var e,d,f;d=0;var b=[],h=a;for(e=0;4>=e;e++)b[e]=h&1,h>>=1,d^=b[e];b[5]=d;for(e=6;9>=e;e++)f=~~(h/3),d=h-3*f,h=f,b[e]=d;0==c&&(b[6]++,3==b[6]&&(b[6]=0),g(b,0,3,1),b[1]^=1,b[3]^=1);1==c&&(b[7]++,3==b[7]&&(b[7]=0),g(b,1,5,2),b[2]^=1,b[5]^=1);2==c&&(b[8]++,3==b[8]&&(b[8]=0),g(b,0,2,4),b[0]^=1,b[2]^=1);3==c&&(b[9]++,3==b[9]&&(b[9]=0),g(b,3,4,5),b[3]^=1,b[4]^=1);h=0;for(e=9;6<=e;e--)h=3*h+b[e];for(e=4;0<=e;e--)h=2*h+b[e];return h}function g(a,c,d,f){var g=a[c];a[c]=a[d];a[d]=a[f];a[f]=g}r=function(){};var k=[1,1,1,3,12,60,360],m,a,f,d,c;for(a=0;360>a;a++){n[a]=-1;p[a]=[];for(c=0;4>c;c++)p[a][c]=l(a,c)}for(d=n[0]=0;5>d;d++)for(a=0;360>a;a++)if(n[a]==d)for(c=0;4>c;c++){f=a;for(m=0;2>m;m++)f=p[f][c],-1==n[f]&&(n[f]=d+1)}for(a=0;2592>a;a++){o[a]=-1;q[a]=[];for(c=0;4>c;c++)q[a][c]=i(a,c)}for(d=o[0]=0;5>=d;d++)for(a=0;2592>a;a++)if(o[a]==d)for(c=0;4>c;c++){f=a;for(m=0;2>m;m++)f=q[f][c],-1==o[f]&&(o[f]=d+1)}}function s(l,i,g,k,m){if(0==g)return 0==l&&0==i;if(n[l]>g||o[i]>g)return!1;for(var a=0;4>a;a++)if(a!=k)for(var f=l,d=i,c=0;2>c;c++)if(f=p[f][a],d=q[d][a],s(f,d,g-1,a,m))return m.push("ULRB".charAt(a)+["","'"][c]),!0;return!1}var n=[],o=[],p=[],q=[];return function(l){r();var i=~~(360*Math.random()),g=~~(2592*Math.random()),k=[];if(0!=i||0!=g)for(;99>l&&!s(i,g,l,-1,k);l++);k=k.reverse().join(" ")+" ";for(g=0;4>g;g++)i=~~(3*Math.random()),2>i&&(k+="lrbu".charAt(g)+["","'"][i]+" ");return k}})();

1798 chars
```


----------



## onionhoney (Jun 14, 2012)

```
for(int i=25,a,b=6,c;i;){do c=rand()%6;while(!(c-b&&(c+b!=5||c-a)));cout<<"UFLRBD"[i--,a=b,b=c]<<"'2 "[rand()%3];}
```

My first try with C++. 114 chars. 

EDIT: tried in vain to modify the 102-char solution by saving a rand()....end up getting one more..

```
for(int c=0,b=6,j=25,m;j;m%6-c|c+b-5&&b-m%6&&cout<<"URFBLD"[j--,c=b,b=m%6]<<" 2'"[m/6%3]<<" ")m=rand();
```


----------



## qqwref (Jun 23, 2012)

Hey all, here's a scrambler in K  Yeah, a bit silly, but under 100 characters. Can probably be optimized a ton.


```
,/("FRUDLB"@25#({,/1_'(0,&((x=q)|(:':t)&t:5=x+q: :':x))_x:0,x}/99?6)),'25?(" ";"' ";"2 ")
```

Edit: Typical output:
"U R F2 B' R2 U' D2 L2 U' L B' L2 U2 B' F U' L2 R D' B' D2 F U' R2 L' "
And you can get K from kx.com, they have a Q console and from there you can press \ to switch between Q and K. K is a lot less verbose but is missing several useful functions.


----------



## Baian Liu (Jun 23, 2012)

Even shorter for TI-83/84.


```
:ClrHome
:0→E
:7→D
:For(A,1,6
:For(B,2,14,3
:E→F
:D→E
:While D=E or D+E=7 and D=F
:randInt(1,6→D
:End
:Output(A,B,sub("URFBLD",D,1)+sub(" '2",randInt(1,3),1
:End
:End
```


----------



## qqwref (Jun 27, 2012)

Much better K scrambler:


```
,/("FRUDLB"@{#&(=':x),2_&':5=+':x}{25?6}/2#2),'25?" ","'2",'" "
```

Only 63 characters, by my count. I think that's pretty solid.
(I had one that was 61 before until I realized it didn't allow sequences starting with 5.)

Explanation of what it does:


Spoiler



Let's first dissect this weird-looking function: {#&(=':x),2_&':5=+':x}. The curly braces signify a function, and it's implied that the argument is x, so we don't need to declare it. The expression f':x for a function f basically returns the list you get from applying f to each adjacent pair of elements of x. So +':x gives us a list of sums of two adjacent elements. 5=+':x gives us a list of bits, with a 1 set in every position where the sum of two adjacent elements is equal to 5. &':5=+':x gives us another list of bits, but this time there is a 1 only when there were two sums-to-5 in a row (which in this case represents sequences like LRL or UDU). The 2_ removes the first two elements from that so we don't get problems with sequences that start with 5 or 5 0. So that part gives us a 1 for each LRL-type sequence. The part to the left of the comma, =':x, gives us another list of bits, with a 1 for each repeated number (so, a sequence like RR). The comma joins those two sequences together; and then the #& at the left returns the number of 1s we have. So this whole function ends up giving us the number of bad sequences in our potential scramble.

In the middle we have {stuff}{25?6}/2#2. 2#2 makes a list of two 2's so we have (2 2) there. So what does this thing do? Well, f g/ x (with f and g functions) applies g to x repeatedly until f of the result is zero. Now 25?6 generates a list of 25 random integers from 0 to 5, so we are generating lists of 25 random integers until they pass the first function (i.e. the result is 0). We needed that 2#2 so the function would start off giving a 1; otherwise the expression would immediately terminate. Notice that a list passes the first function if it has zero mistakes - that is, the list of faces to move is valid. So great - now we have generated a random list of 25 faces until it was valid for making a scramble out of. The list will look something like this: 0 3 2 5 3 0 3 5 ...

The rest is more straightforward. Let's call the part we looked at so far X; then we have ,/("FRUDLB"@X),'25?" ","'2",'" ". The "FRUDLB"@X just indexes our list into the string "FRUDLB" so now we have a list of 25 letters, such as "FDUBDFDB...". The " ","'2",'" " at the far right generates a list of the 3 possible suffixes (" ", "' ", and "2 ") and the 25? chooses 25 random suffixes, just like the previous use of it. Finally, the ,' joins each move letter up with one of the suffixes, and the ,/ at the beginning takes the whole list of strings and joins it together into one. Pretty tricky, but it makes sense if you analyze it.


----------



## Escher (Jun 27, 2012)

qqwref said:


> Explanation of what it does:
> 
> 
> Spoiler
> ...



I think this applies to you, too.



qqwref said:


> o_________________________o


----------



## Stefan (Jun 27, 2012)

Your explanation is about 41 times as long as the program. Can't decide whether that's good or bad.


----------



## qqwref (Jul 8, 2012)

Here's a 2x2 random-state scrambler in Q (a more verbose version of K, as above), in 593 characters:

```
system"S ",string 6h$.z.T
e:{m:enlist();x:enlist x;do[y;t:asc each raze each(3*(til 3)except/:(first each m)div 3)+'/:\:til 3;x:raze ({y x/z}./:({x:[email protected]\:0 5 1 3 4 6 2;x[1]:(x[1]+0 2 1 0 0 1 2)mod 3;x};{[email protected]\:1 2 3 0 4 5 6};{x:[email protected]\:4 0 2 3 5 1 6;x[1]:(x[1]+2 1 0 0 1 2 0)mod 3;x})cross 1+til 3)[t]@\:'x;m:raze t,\:'m];(m;x)}
w:{a:e[(til 7;7#0);ceiling y%2];b:e[x;floor y%2];$[(s:(raze a[1]~/:\:b 1)?1b)=(c:count b 0)*count a 0;0N;" "sv ("RUF"cross("";"2";"'"))(reverse b[0;s mod c]),2 1 0 5 4 3 8 7 [email protected][0;s div c]]}
scramble:{i:0;s:0N;h:(-7?7;k,(neg sum k:6?3)mod 3);while[0N~s;i+:1;s:w[h;i]];s}
```
I'm obviously not gonna explain this in full detail, but the general gist is that we get a random position and then try 1, 2, ... moves until we find a solution. We look for solutions of n moves by generating all positions ceiling(n/2) moves away from solved and all positions floor(n/2) moves away from our position, and then matching every pair of elements and seeing if we get any successes. This isn't the fastest possible way to do this but it seems fast enough.

Sample output:
q)scramble[]
"U2 F2 U F2 U R2 U2 F R'"

In K: 513 characters :3

```
."\\S ",$6h$.z.T
e:{m:,();x:,x;do[y;t:.q.asc',/'(3*(!3).q.except/:(*:'m)div 3)+'/:\:!3;x:,/({y x/z}./:,/({x:[email protected]\:.:'"0513462";x[1]:(x[1]+.:'"0210012").q.mod'3;x};{[email protected]\:.:'"1230456"};{x:[email protected]\:.:'"4023516";x[1]:(x[1]+.:'"2100120").q.mod'3;x}),/:\:1+!3)[t]@\:'x;m:,/t,\:'m];(m;x)}
w:{a:e[(!7;7#0);-_-y%2];b:e[x;_y%2];$[(s:(,/a[1]~/:\:b 1)?1b)=(c:#b 0)*#a 0;0N;" "/:(,/"RUF",/:\:("";"2";"'"))(|b[0;s .q.mod'c]),.:'"210543876"@a[0;s div c]]}
scramble:{i:0;s:0N;h:(-7?7;k,(-+/k:6?3).q.mod'3);while[0N~s;i+:1;s:w[h;i]];s}
```


----------



## qqwref (Jul 10, 2012)

506 characters  It's only 479 if you don't count the random seed part (which I didn't count in my 3x3 scrambler in K).

```
system"S ",string 6h$.z.T
e:{m:,:[-1];x:,:[x];do[y;t:!:[9]except/:(3**:'[m]div 3)+'/:\:til 3;x:raze({y x/z}./:({x:[email protected]\:0 5 1 3 4 6 2;x[1]:(x[1]+7#0 2 1 0)mod 3;x};{[email protected]\:1 2 3 0 4 5 6};{x:[email protected]\:4 0 2 3 5 1 6;x[1]:(x[1]+7#2 1 0 0)mod 3;x})cross 1 2 3)[t]@\:'x;m:,/[t,\:'m]];(m;x)}
w:{a:e[(til 7;7#0);o:_:[y%2]];b:e[x;y-o];$[()~s:*:[a[1]inter b 1];0N;" "sv("RUF"cross("";"2";"'"))1_-1_|:[b[0]b[1]?s],2 1 0 5 4 3 8 7 [email protected][0]a[1]?s]}
scramble:{i:0;s:0N;h:(-7?7;k,(neg sum k:6?3)mod 3);while[0N~s;i+:1;s:w[h;i]];s}
```
Cool thing: this is actually significantly faster than the previous one, and the biggest reason is using "a[1]inter b 1" instead of "(raze a[1]~/:\:b 1)?1b". The old one would take about 10 seconds for particularly hard scrambles, but this one can do 100 scrambles in about 10 seconds.


----------



## Baian Liu (May 19, 2013)

python: *138*

```
from random import randint as r;a=b=8
for x in range(25):
 c=b;b=a
 while (a==b)+(a+b-5)*(a==c):a=r(0,5)
 print("URFBLD"[a]+" '2"[r(0,2)])
```

A "one-liner":

```
from random import randint as r;d=[r(0,5) for x in range(50)];print(''.join(["URFBLD"[[d[y] for y in range(2,50) if 1-((d[y]==d[y-1])+(d[y]+d[y-1]-4)*(d[y]==d[y-2]))][z]]+" '2"[r(0,2)] for z in range(2,27)]))
```


----------



## JF1zl3 (May 19, 2013)

Dayum, nice. I don't know much about Python, actually virtually nothing, but it looks extremely condensed.
I think I can pick up on certain things about the code that make sense to me though, so it isn't foreign language or anything, it just seems super cool how little code is required lol.


----------



## JustinJ (Jun 14, 2013)

Bump because I think this is a cool thread and I think I have a kind of cute Ruby (2.0) solution 


```
p (1..1.0/0).lazy.map{(1..25).map{"UDLRFB"[rand 6]}.join}.find{|s|!(/(.)\1|(UDU|DUD|LRL|RLR|FBF|BFB)/=~s)}.each_char.map{|c|c+["","'","2"].sample}.join" "
```

Neated up a bit with explanations:


```
p (1..1.0/0).lazy # infinite lazy enumerator
  .map { (1..25).map { "UDLRFB"[rand 6] }.join } # make each element a list of faces
  .find { |s| !(/(.)\1|(UDU|DUD|LRL|RLR|FBF|BFB)/ =~ s) } # find one that doesn't duplicate moves or RLR etc
  .each_char.map { |c| c + ["","'","2"].sample } # suffixes
  .join " "
```
Not the shortest at 154 chars, I'd like to see some improvements on it though!

I also didn't see any other functional-ish solutions, although I might have missed some. I'd like to see some functional attempts by others.

I think it might be similar to this one, but I'm not familiar enough with Perl to tell.


----------



## Stefan (Jun 15, 2013)

JustinJ said:


> I think it might be similar to this one, but I'm not familiar enough with Perl to tell.



It's somewhat similar, but it appends one move at a time and chops it off if it causes a violation, repeat until 25 moves are built. Those like yours started with Michael's request [post=485880]two posts later[/post].


----------



## JustinJ (Jun 15, 2013)

Stefan said:


> It's somewhat similar, but it appends one move at a time and chops it off if it causes a violation, repeat until 25 moves are built. Those like yours started with Michael's request [post=485880]two posts later[/post].



Ah neat, not sure how I missed those.


----------



## Stefan (Jun 15, 2013)

JustinJ said:


> not sure how I missed those.



Probably you didn't think someone other than me could be that brilliant.


----------



## keemy (Jan 6, 2014)

*133*

```
import random as l;r=l.randint;a=b=8
for x in"a"*25:
 c=b;b=a;
 while(a==b)+(a+b==5)*(a==c):a=r(0,5)
 print"URFBLD"[a]+" '2"[r(0,2)],
```
Last python post had an error, this fixes it and reduces count by 5. Sorry for huge bump though.


----------



## Carrot (Apr 23, 2014)

javascript(110):

```
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]+" "):r=Math.random)m=0|r()*6
```
instead of initialising Math.random in r before the for loop, you could just move it to the c-part of a?b:c as it was unused anyway and we are not using the random function before that.

Python(128):

```
import random as l;r=l.randrange;a=2
for c in"a"*25:
 b=a
 while(a==b)+(a+b==5)*(a==c):a=r(6)
 print"URFBLD"[a]+" '2"[r(3)],;c=b
```
just moved a bit around in Keemy's code.


ohhh, hooray I bumped the thread :3


----------



## ajayd (Apr 23, 2014)

So code that does strange stuff like U2 U2 isn't allowed(and strange spacing)?

If not: 

```
from random import randint as r;m,l="UDLRFB"," '2";print " ".join(m[r(0,5)]+l[r(0,2)]for i in range(25))
[\code]
106 characters
```


----------



## Carrot (Apr 23, 2014)

ajayd said:


> So code that does strange stuff like U2 U2 isn't allowed(and strange spacing)?
> 
> If not:
> 
> ...


----------



## DTCuber (Jun 26, 2015)

Hello,

I am just learning Python and Pygame, and I am trying to create my own puzzle timer. I am struggling with creating a basic scrambler function that does not repeat moves, i.e., U2 U'. So far, I have this:


```
#Import libraries
import random

#Function that creates scrambling sequences
def scramble():
  #Create list with turns 
  turns = ["R ","R' ","R2 ","L ","L' ","L2 ","F ","F' ","F2 ","B ","B' ","B2 ","U ","U' ","U2 ","D ","D' ","D2 "]
  #Initialize empty string for scramble
  random_scramble=""
  #Generate 20 random turns and store in string
  for i in range(0,20):
    random_scramble+=random.choice(turns)
  #Return string
  return random_scramble
```

Any help would be greatly appreciated!

-DTCuber

P.S. Sorry for the massive bump.


----------



## martinss (Jun 26, 2015)

DTCuber said:


> Hello,
> 
> I am just learning Python and Pygame, and I am trying to create my own puzzle timer. I am struggling with creating a basic scrambler function that does not repeat moves, i.e., U2 U'. So far, I have this:
> 
> ...




```
#Import libraries
import random

#Function that creates scrambling sequences
def scramble():
  #Create list with turns 
  turns = [ ["R ","R' ","R2 "],["L ","L' ","L2 "],["F ","F' ","F2 "],["B ","B' ","B2 "],["U ","U' ","U2 "],["D ","D' ","D2 "]]
  #Initialize empty string for scramble
  random_scramble=""
  #Generate 20 random turns and store in string
  for i in range(0,20):
    random_scramble+=turns[random.randrange(6)][random.randrange(3)]
  #Return string
  return random_scramble
```

https://repl.it/uRS


----------



## Stefan (Jun 26, 2015)

martinss said:


> ```
> #Import libraries
> import random
> 
> ...



What is that supposed to be?


----------



## martinss (Jun 26, 2015)

Stefan said:


> What is that supposed to be?


You're right, it does the exact same thing


```
#Import libraries
import random

def nextnumber(currentnumber):
    #return a random number in range(0,6) different than currentnumber
    return (currentnumber+1+random.randrange(5))%6

#Function that creates scrambling sequences
def scramble():
  #Create list with turns 
  turns = [ ["R ","R' ","R2 "],["L ","L' ","L2 "],["F ","F' ","F2 "],["B ","B' ","B2 "],["U ","U' ","U2 "],["D ","D' ","D2 "]]
  #Initialize empty string for scramble
  random_scramble=""
  number=0
  #Generate 20 random turns and store in string
  for i in range(0,20):
    number=nextnumber(number)
    random_scramble+=turns[number][random.randrange(3)]
  #Return string
  return random_scramble
  
print (scramble())
```

https://repl.it/uRS/1


----------



## StachuK1992 (Jun 26, 2015)

Sorry, but 

... U2 D2 U ...

should not be allowed.


----------



## United Thought (Jun 26, 2015)

Absolutely pushing the boundaries of my capability: Python 

Note: Outputs scrambles 1 move at a time, so you'd have to run it in IDLE, etc to see the full sequence.

90 chars (spaces)
82 chars (no spaces)
108 bytes.


```
import random as r
for i in range(0,25):
    print("".join(r.choice("UDLRBF")+r.choice("'2 ")))
```


----------



## TheNextFeliks (Jun 27, 2015)

```
import random as r;s=[];l='';m='';t=['R','R2',"R'",'F','F2',"F'",'U','U2',"U'"];while len(s)<11:m=r.choice(t);if l:if l[0]==m[0]:continue;s.append(m);l=m;print(' '.join(s))
```

172 characters for a simple 2x2 scrambler. Not much of a code golfer personally.


----------



## Calode (Jun 27, 2015)

United Thought said:


> Absolutely pushing the boundaries of my capability: Python
> 
> Note: Outputs scrambles 1 move at a time, so you'd have to run it in IDLE, etc to see the full sequence.
> 
> ...





There needs to be a new line between "import random as r" and "for i in range(0,25): or else it won't compile. It doesn't add another character but does add another byte.


----------



## Stefan (Jun 27, 2015)

TheNextFeliks said:


> ```
> import random as r;s=[];l='';m='';t=['R','R2',"R'",'F','F2',"F'",'U','U2',"U'"];while len(s)<11:m=r.choice(t);if l:if l[0]==m[0]:continue;s.append(m);l=m;print(' '.join(s))
> ```
> 
> 172 characters for a simple 2x2 scrambler. Not much of a code golfer personally.



What language is that supposed to be? Somewhat looks like Python but clearly isn't ("SyntaxError: invalid syntax").


----------



## TheNextFeliks (Jun 27, 2015)

Stefan said:


> What language is that supposed to be? Somewhat looks like Python but clearly isn't ("SyntaxError: invalid syntax").



It is Python. There should be a new line after the import that I forgot. Besides that I think it should work. But I don't have a computer around so can't check.

Edit: There is something else wrong. I'll figure it out.


----------



## United Thought (Jun 27, 2015)

Calode said:


> There needs to be a new line between "import random as r" and "for i in range(0,25): or else it won't compile. It doesn't add another character but does add another byte.



Thanks for pointing this out, this was clearly a pasting error, since the stats are correct for the correct version of the code and the code was written correctly in my .py file. Original post has been updated.


----------



## Stefan (Jun 28, 2015)

TheNextFeliks said:


> I don't have a computer around so can't check.



How did you write that post without a computer? Dictate it to someone over the phone to type it for you? Then you *could* have told them to copy your code into repl.it or any other of these services


----------



## TheNextFeliks (Jun 28, 2015)

Stefan said:


> How did you write that post without a computer? Dictate it to someone over the phone to type it for you? Then you *could* have told them to copy your code into repl.it or any other of these services



I have an iPhone that I used and I can't use repl.it on it.


----------



## DTCuber (Jun 30, 2015)

martinss said:


> You're right, it does the exact same thing
> 
> 
> ```
> ...



Thanks for your help! I really appreciate it!

How did you learn Python? I'm looking for other Python resources. Thanks!

-DTCuber


----------



## Stefan (Jun 30, 2015)

DTCuber said:


> I'm looking for other Python resources.



http://www.checkio.org/ is great. You solve problems in Python there and then you can look at other people's solutions and learn from them.


----------



## Shiv3r (Jul 12, 2016)

hey gyus, I tried the scrambler on the original post(the one with actual newlines, not the insane one), and after commenting out the first part, It worked. Im using it for a times enterer I made in python(I have a computer with no internet in my room, so a lot of my cubing stuff I coded myself in python.)
the problem: it spits out very, very long scrambles, sometimes 100 moves or more. anyone have a solution to this problem?


----------



## AlphaSheep (Jul 12, 2016)

Shiv3r said:


> hey gyus, I tried the scrambler on the original post(the one with actual newlines, not the insane one), and after commenting out the first part, It worked. Im using it for a times enterer I made in python(I have a computer with no internet in my room, so a lot of my cubing stuff I coded myself in python.)
> the problem: it spits out very, very long scrambles, sometimes 100 moves or more. anyone have a solution to this problem?


The program literally just picks 25 random moves where moves on the same axis can't be repeated. If you're not getting 25 moves, you've copied and pasted wrong.

If you haven't already, you should go through the program carefully, and see how he goes from that to the one-liner.

Long term, I recommend writing a random state scrambler. Just follow the instructions on Kociemba's site - he explains the technical details pretty well and even gives some code snippets (in Delphi, but they're easy to follow).


----------



## Shiv3r (Jul 12, 2016)

AlphaSheep said:


> The program literally just picks 25 random moves where moves on the same axis can't be repeated. If you're not getting 25 moves, you've copied and pasted wrong.
> 
> If you haven't already, you should go through the program carefully, and see how he goes from that to the one-liner.
> 
> Long term, I recommend writing a random state scrambler. Just follow the instructions on Kociemba's site - he explains the technical details pretty well and even gives some code snippets (in Delphi, but they're easy to follow).


I did have the whole thing (minus the imports) in a def(), and I changed the print to a return because I was using it in a different program


----------



## AlphaSheep (Jul 13, 2016)

Shiv3r said:


> I did have the whole thing (minus the imports) in a def(), and I changed the print to a return because I was using it in a different program


You only need the part after the ==========, but I assume that's what you mean by commenting out the first part.

There are a couple things that could have gone wrong and they most likely have to do with the first line, which should actually be written more like this (if he wasn't trying to make the code as few characters as possible)


```
from random import choice
c = choice # One letter alias for random.choice function
w = ['RL','UD','BF'] # Available moves on each axis
l = [] # Initialise the scramble
```

If your initialisation is outside your def, you'll keep adding to the same scramble and it will get longer and longer.


----------



## Shiv3r (Jul 13, 2016)

makes sense.


----------



## Shiv3r (Jul 13, 2016)

thanks.
Ill try to fix the version I have, which is the few characters as possible


----------



## campos20 (Jul 30, 2016)

A while ago I created this one. It's very readable.


```
import random

faces = "ULFRBD"
modifiers = ["", "'", "2"]

def random_move():
    """Random face turn."""
   
    face = faces[random.randint(0, 5)]
    modifier = modifiers[random.randint(0, 2)]

    return face+modifier

def same(face1, face2):
    """Checks if two moves are of the same type."""
    return face1[0] == face2[0]

def intersection(face1, face2):
    """Checks if two moves do not intersect.""" # avoiding U D U.

    if face1[0] in "UD": return face2[0] not in "UD"
    if face1[0] in "LR": return face2[0] not in "LR"
    if face1[0] in "FB": return face2[0] not in "FB"

def generate():

    n = 20 # size of the scramble
    scramble = []
    todos = []

    while len(scramble) < n:

        move = random_move()
        flag = True
       
        if len(scramble)>0 and same(move, scramble[-1]): flag = False
        elif len(scramble)>1 and (not intersection(scramble[-2], scramble[-1]) and not intersection(scramble[-1], move)): flag = False
       
        if flag: scramble.append(move)
        todos.append(move)
   
    return " ".join(scramble)

scrambles = 10
for i in range(scrambles):
    print generate()
```

Sample output.

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


----------



## Filipe Teixeira (Jan 24, 2020)

python3 megaminx scrambler:


```
import random as r;pow = lambda a, b: lambda: r.choice([a,b]);RD = pow('++','--');U = pow("","'");print('\n'.join([' '.join(['R%s D%s' % (RD(), RD()) for i in range(5)]) + " U" + U() for ii in range(7)]))
```


----------



## Filipe Teixeira (Jan 5, 2021)

towwdso said:


> 274 bytes
> its a world record, I guess. (until someone do it on perl...)
> 
> ```
> ...


I originally posted here as towwdso.

Today I implemented a python class that generates scrambles or reverse algs, take a look.









GitHub - shuantsu/AlgUtils: Classe simples em python para gerar embaralhamentos do cubo mágico|speedcubing,python


Classe simples em python para gerar embaralhamentos do cubo mágico|speedcubing,python - GitHub - shuantsu/AlgUtils: Classe simples em python para gerar embaralhamentos do cubo mágico|speedcubing,py...




github.com


----------



## Jam88 (Jan 5, 2021)

Filipe Teixeira said:


> I originally posted here as towwdso.
> 
> Today I implemented a python class that generates scrambles or reverse algs, take a look.
> 
> ...


You have an alt?! Shocker...


----------



## Filipe Teixeira (Jan 5, 2021)

Jam88 said:


> You have an alt?! Shocker...


I lost my password. It was a long time ago


----------

