12
\$\begingroup\$

The Task

In this challenge, your task is to write some code which outputs one of its anagrams chosen randomly with uniform distribution but it should never output itself.


Elaboration

Given no input, your program should output any one of the anagrams of its source code. Your program should never output its own source as it is, i.e. it should never be a quine.


Input

Your program must not take any input. However, if your language requires input as a necessity, you may assume that it will be given lowercase a. You must not use the input in any way, though.


Output

Your program can output in any way except writing it to a variable. Writing to file, console, screen, etc. is allowed. Function return is allowed as well.


Additional Rules

  • Your program's source code must have at least 3 chars (not 3 bytes).

  • Your program's source code must have at least 3 possible anagrams (excluding itself). For example, aab does not count as a valid submission as aab only has two anagrams other than aab (baa and aba).

  • Your program must not produce any error.

  • Your program should output its anagrams exactly.

  • Standard Loopholes and Standard quine rules apply.


Example

Suppose your program's source code is abc. It should randomly output any one of the following (with uniform distribution):

  1. acb
  2. bca
  3. bac
  4. cba
  5. cab

And, it should never output abc.


Winning Criterion

This is , so the shortest code in bytes wins! In case of a tie, the solution which was posted earlier wins!

\$\endgroup\$
  • \$\begingroup\$ Related. \$\endgroup\$ – Arjun May 23 '17 at 6:57
  • \$\begingroup\$ Does it need to guarantee that it behaviors correctly, or just with probability 1? \$\endgroup\$ – PyRulez May 23 '17 at 21:13
  • \$\begingroup\$ @PyRulez In this challenge, your task is to write some code which outputs one of its anagram chosen randomly with uniform distribution... (First sentence (below The Task)) \$\endgroup\$ – Arjun May 24 '17 at 5:32
  • \$\begingroup\$ @Arjun so is a 0% chance of failure fine? \$\endgroup\$ – PyRulez May 24 '17 at 14:23
  • \$\begingroup\$ @PyRulez Failure? The code is supposed to output one of it's anagram (except itself) chosen randomly with equal chances of any of its anagram being outputted. I don't know what you mean with "Failure". \$\endgroup\$ – Arjun May 24 '17 at 14:31
5
\$\begingroup\$

Jelly, 15 bytes

“v0¡µṾ;ḢŒ!QḊX”v

Just to get things started; this is almost certainly beatable. This is basically just a combination of a universal quine constructor and a "pick a random permutation other than the input" function; the latter may be improvable, the former almost certainly is.

Explanation

Universal quine constructor

“v0¡µṾ;Ḣ”v
“       ”v   Evaluate the following, given {itself} as argument:
 v0¡µ          No-op (which starts with "v")
     Ṿ         Escape string
      ;Ḣ       Append first character of {the argument}

This can be seen to be a quine if run by itself. It's also a proper quine by most definitions I know of; it doesn't read its own source (rather, it contains a literal that's "eval"ed, and is given a copy of itself as an argument), it can carry a payload (as seen here!), and the v outside the string literal is encoded by the v inside.

Pick a random anagram

Œ!QḊX
Œ!     All permutations
  Q    Discard duplicates
   Ḋ   Discard the first (i.e. the string itself)
    X  Choose random element

This is really inefficient on a string this long, so I haven't been able to test the program as a whole, but I've tested it on shorter strings and it appears to function correctly.

\$\endgroup\$
  • \$\begingroup\$ Why doesn't thins work on TIO? \$\endgroup\$ – Mr. Xcoder May 23 '17 at 11:56
  • \$\begingroup\$ @Mr.Xcoder It probably bypasses the 60s time limit. \$\endgroup\$ – Erik the Outgolfer May 23 '17 at 12:40
  • \$\begingroup\$ Oh, yes, you are right. \$\endgroup\$ – Mr. Xcoder May 23 '17 at 13:20
  • \$\begingroup\$ While you have it in your byte count your code misses the necessary Q. However I think you can change this "all permutations" method with a "shuffle" one using Ẋ⁼¿, saving a byte while also allowing it to work on TIO. \$\endgroup\$ – Jonathan Allan May 23 '17 at 19:03
4
\$\begingroup\$

CJam, 17 bytes

{`"_~"+m!(a-mR}_~

This isn't going to finish any time soon, so no TIO link this time.

As a consolation, here is a 20 byte solution that does terminate very quickly:

{`"_~"+:S{mr_S=}h}_~

Try it online!

Explanation

{`"_~"+   e# Standard quine framework, leaves a string equal to the source
          e# code on the stack.
  m!      e# Get all permutations. The first one will always be the original order.
  (a      e# Remove that copy of the source code and wrap it in a new list.
  -       e# Remove all copies of the source code from the list of permutations.
  mR      e# Pick a random permutation.
}_~

The 20 byte solution instead shuffles the source code until its different from the original.

\$\endgroup\$
4
\$\begingroup\$

Python 2, 117 bytes

Surprisingly this solution is shorter than I expected. Shuffles source code, until it differs from the original.

-2 bytes, thanks to @mbomb007
-3 bytes, thanks to @Wondercricket

Try it online

s=r"""from random import*;R='s=r\"""'+s+'\"""'+';exec s';L=R
while L==R:L=''.join(sample(R,len(R)))
print L""";exec s

This is one of the basic quines in python, that I've modified

s = r"print 's = r\"' + s + '\"' + '\nexec(s)'"
exec(s)

Generating anagram is done by random module

L=R
while L==R:L=''.join(sample(L,len(L)))

Where R contains sourcecode

s=...
R='s=r\"""'+s+'\"""'+'\nexec s'

Triple quotes were needed as I was forced to keep actual lineseparators in code. Anagrams will have 3 lines anyway.

\$\endgroup\$
  • 1
    \$\begingroup\$ exec s instead of exec(s) \$\endgroup\$ – mbomb007 May 23 '17 at 13:41
  • \$\begingroup\$ Since a str is immutable, you can save a bytes by doing L=R and using sample on L rather than using shuffle on the list. repl.it. The idea is taken from this Stackoverflow \$\endgroup\$ – Wondercricket May 23 '17 at 18:31
  • \$\begingroup\$ @Wondercricket Sample returns list of characters, so comparing it's results with R will always returns False. But some rearranging helps, thank s for the idea! \$\endgroup\$ – Dead Possum May 24 '17 at 9:00
3
\$\begingroup\$

Java 7, 376 428 426 428 bytes

import java.util.*;class M{public static void main(String[]a){String s="import java.util.*;class M{public static void main(String[]a){String s=%c%s%1$c,x=s=s.format(s,34,s);for(List l=Arrays.asList(x.split(%1$c%1$c));x.equals(s);s=s.join(%1$c%1$c,l))Collections.shuffle(l);System.out.print(s);}}",x=s=s.format(s,34,s);for(List l=Arrays.asList(x.split(""));x.equals(s);s=s.join("",l))Collections.shuffle(l);System.out.print(s);}}

+52 and +2 bytes for two bug-fixes.. I wasn't checking (correctly) if the randomly generating String was equal to the original source-code. The chances of this are astronomical small considering the amount of characters involved, but I have to validate it regardless to comply to the challenge rules.

My first answer in Java..
Try it here.

You can remove both Collections.shuffle(l) and add ! in front of both the x.equals(s) to verify that the output indeed equals the program:
Try it here.

Explanation:

  • The String s contains the unformatted source code.
  • %s is used to input this String into itself with the s.format(...).
  • %c, %1$c and the 34 are used to format the double-quotes.
  • s.format(s,34,s) puts it all together

And this part of the code is responsible for outputting a random anagram:

// Strings `s` and `x` now both contain the source-code:
x=s=s.format(s,34,s);

// Create a list with the characters of this source-code-String and loop
for(List l=Arrays.asList(x.split(""));
    // as long as String `x` equals String `s`
    x.equals(s);
    // Shuffle the list, and set it to `s` in every iteration of the loop:
    s=s.join("",l))Collections.shuffle(l);
// End of loop (implicit / single-line body)

// And then print the random anagram to STDOUT:
System.out.print(x);
\$\endgroup\$
1
\$\begingroup\$

05AB1E, 22 bytes

"34çìDJœ¦.R"34çìDJœ¦.R

This creates a list that is too big for TIO, so the link uses a smaller string, but the idea is the same.

Try it online!

"34çìDJœ¦.R"           # Push this string
            34ç        # Push "
               ì       # Prepend
                DJ     # Duplicate and join 
                  œ¦   # Push all permutations except the original
                    .R # Pick a random element
\$\endgroup\$
1
\$\begingroup\$

Javascript (ES6), 128 bytes

!function a(){b="!"+a+"()",c=b.split(""),c.sort(()=>Math.round(Math.random())-.5),c!=b.split("")?console.log(c.join("")):a()}();

Uses sort() returning random -1,0, or 1 to shuffle the output.

\$\endgroup\$
0
\$\begingroup\$

Bash, 27 96 bytes

i=`cat $0`&&e=`fold -w1 $0|shuf|tr -d '\n'`&&while [ "$e" = "$i" ]; do `$0`; exit; done&&echo $e

fold divides the code in lines, shuf shuffles the lines, and tr put the code back together

fixed the issue of it outputing itself, now it will never output itself

Try it Online!

\$\endgroup\$
  • 1
    \$\begingroup\$ Does it have chance to output the code itself, without changes? \$\endgroup\$ – Dead Possum May 23 '17 at 14:53
  • \$\begingroup\$ It also seems to only shuffle the lines, so not all permutations are possible, in particular since the program only has a single line... \$\endgroup\$ – Martin Ender May 23 '17 at 14:55
  • \$\begingroup\$ All permutations are possible, test it. I am fixing the problem of it maybe ouputing itself \$\endgroup\$ – DrnglVrgs May 23 '17 at 15:02
  • 1
    \$\begingroup\$ That $0 looks like a violation of “Your program must not take any input.” \$\endgroup\$ – manatwork May 23 '17 at 15:12
  • \$\begingroup\$ I'm pretty sure that's just the filename. Therefore, even if it wasn't input, this is a cheating quine :( \$\endgroup\$ – CalculatorFeline May 23 '17 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.