6
\$\begingroup\$

An IDE developer has hired you to write a program that generates (by printing to stdout) n lines of randomly generated code. They will use your program to create sample code for use in marketing their newest compiler.

Rules:

Your randomly generated output code:

  • must not be the same each execution (for the same n parameter)
  • must be written in the same language as your program
  • must be able to be successfully compiled without errors or warnings (including "variable not used" messages, if your language includes them)
  • should use a variety of the language's built-in features
  • should (where appropriate) follow the language's most popular style guide. For example, PEP-8 for Python
  • should, if n is larger than 20, have functions. Functions should be limited to a length that follows the language's style guide
  • does not have to have comments (but the developer will be impressed if it does!)
  • may include a single blank line between code blocks for clarity

You can use an external dictionary to allow generation of function/variable names, and lists of standard functions/libraries for your language. If your program uses these, please include a link (to a pastebin if your dictionary is custom written for this task)

Please include a few sample outputs to demonstrate your program's capabilities.

Winning:

The winning program should implement all the above features, and generate realistic code without sacrificing execution time or program length (to a huge degree - this isn't golf).

The winner will be chosen by community vote from entries received before 1st May 2012. Voting should be based upon realism, variety, creativity and adherence to the criteria above.

Good luck!

\$\endgroup\$
8
  • \$\begingroup\$ possible duplicate of Generate a random program in your favorite language \$\endgroup\$ Apr 3, 2012 at 14:11
  • 2
    \$\begingroup\$ This seems different than the linked question to me because of the same language restriction. Should cut down on the x->BF entries, although I will be working on BF->BF \$\endgroup\$
    – captncraig
    Apr 3, 2012 at 18:08
  • 3
    \$\begingroup\$ As far as I am concerned "most votes" is not, has never been and will never be an "objective criteria". It is quantitatively different from "fewest bytes" which can be determined from the entries themselves. \$\endgroup\$ Apr 4, 2012 at 14:20
  • 1
    \$\begingroup\$ What's the point of "must not be the same each execution (for the same n parameter) "? Wouldn't a strictly deterministic generation be more Lorem-Ipsum-like? \$\endgroup\$ Apr 5, 2012 at 11:31
  • 2
    \$\begingroup\$ I’m voting to close this question because "must be able to be successfully compiled", "blocks", "functions", "style guide", and "variety of the language's features" are subjective and not featured in all languages. \$\endgroup\$
    – emanresu A
    Mar 29, 2022 at 3:03

1 Answer 1

10
\$\begingroup\$

Did it in Brainfuck! I had a bit of trouble generating random numbers. I hoped something simple like a LFSR could give me a fairly decent looking sequence, but I found it looped too quickly and gave boring output. I resorted to using a RNG found here, which actually takes quite a long time to run. Since BF has no capacity to get any source of entropy from the operating system, I first have the user input any text (I pound on they keyboard) to seed the RNG. After that it takes the number of lines and outputs random BF, using 16 hard-coded subsequences.

get random seed input till newline (10)
>>>+[,[<+>>+>>>>>>+++<<<<<<<-]>[<+>-]<----------]
get number of lines
<<<,[->>

Map: n nesting seed lineCount _ _ _ _ _ _ randh randl

mod by 32 to get char count for this line
[>+>+<<-]>>[<<+>>-]>++++[<++++++++>-]<<
[->-[>+>>]>[+[-<+>]>+>>]<<<<<] >[-]>>[-]<[<<+>>-]
loop that many times
<<[-<


generate next 'random' number
>>[-]>[-]>[-]>[-]>[-]>[-]>[<<<<<<+>>>>>>-]>[<<<<<<+>>>>>>-]<<<<+++++++
[<+++++++++++>-]<[<<[>>>>>>+<<<+<<<-]>>>[<<<+>>>-]<<[>>>>>+<<<+>+<<<-]
>>>[<<<+>>>-]<[>>>>+[<<<+>+>>-]<<[>>+<<-]+<[>-<[-]]>[>+<-]<<-]<-]+++++
+[>++++++++<-]>-[<<[>>>>>+<<<<+<-]>[<+>-]>-]<<<[-]>[-]+++++[<+++++>-]<
[>>>>>>>+[<<<<<<+>+>>>>>-]<<<<<[>>>>>+<<<<<-]+<[>-<[-]]>[>>>>+<<<<-]<<
-]++++++[>>>>>>+++++++++<<<<<<-]>>>>>>[<<<<<<<<+>>+>>>>>>-]<<<<<<[>>>>
>>+<<<<<<-]<<
seed is now next number in sequence


copy and mod 16
[>>+>+<<<-]>>>[<<<+>>>-]>++++[<++++>-]<<
[->-[>+>>]>[+[-<+>]>+>>]<<<<<]>[-]>>[-]<
now we have bottom 4 bits of data; pick an output sequence:
#00: +
[<+<+>>-]<[>+<-]+<[>-<[-]]>[-[-]>[-]<
>++++++[<+++++++>-]<+.[-]]>

#01: +
-[<+<+>>-]<[>+<-]+<[>-<[-]]>[-[-]>[-]<
>++++++[<+++++++>-]<+.[-]]>

#02: -
-[<+<+>>-]<[>+<-]+<[>-<[-]]>[-
[-]>[-]<
>+++++[<+++++++++>-]<.[-]]>

#03: [->-[>+>>]>[+[-<+>]>+>>]<<<<<]
-[<+<+>>-]<[>+<-]+<[>-<[-]]>[-[-]>[-]<
>+++++++[<+++++++++++++>-]<.
>+++++[<--------->-]<-.
+++++++++++++++++.
-----------------.
>+++++[<+++++++++>-]<+.
>++++[<------->-]<-.
>+++[<------>-]<-.
>+++[<++++++>-]<+..
>+++++[<++++++>-]<+.
>+++++[<------>-]<-.
>++++[<+++++++>-]<+.
>++++++[<-------->-]<.
>++++++[<++++++++>-]<.
>+++++[<--------->-]<-.
+++++++++++++++.
-----------------.
>+++[<++++++>-]<+.
>+++++[<++++++>-]<+.
>+++++[<------>-]<-.
>+++[<------>-]<-.
>+++[<++++++>-]<+..
>+++++[<++++++>-]<+.
>++++[<-------->-]<-.....>++++[<++++++++>-]<+.[-]]>

#04: <
-[<+<+>>-]<[>+<-]+<[>-<[-]]>[-
[-]>[-]<
>++++++[<++++++++++>-]<.[-]]>

#05: <
-[<+<+>>-]<[>+<-]+<[>-<[-]]>[-
[-]>[-]<
>++++++[<++++++++++>-]<.[-]]>

#06: >
-[<+<+>>-]<[>+<-]+<[>-<[-]]>[- ----[>+<----]>---.[-]<]>

#07: x y z
-[<+<+>>-]<[>+<-]+<[>-<[-]]>[-[-]>[-]<
>++++[<++++++++>-]<.
>++++++++[<+++++++++++>-]<.
>++++++++[<----------->-]<.
>++++++++[<+++++++++++>-]<+.
>++++++++[<----------->-]<-.
>+++++++++[<++++++++++>-]<.[-]]>

#08: [
-[<+<+>>-]<[>+<-]+<[>-<[-]]>[-
[-]>[-]<
>+++++++[<+++++++++++++>-]<.[-]<<<<+>>>>
]>

#09: [
-[<+<+>>-]<[>+<-]+<[>-<[-]]>[-
[-]>[-]<
>+++++++[<+++++++++++++>-]<.[-]<<<<+>>>>
]>

#10: ++[>+<------]>
-[<+<+>>-]<[>+<-]+<[>-<[-]]>[- 
 [-]>[-]<
>++++++[<+++++++>-]<+..
>++++++[<++++++++>-]<.
>++++[<------->-]<-.
>+++[<------>-]<-.+++++++++++++++++.
---------------......>++++++[<++++++++>-]<.
>+++++[<------>-]<-.[-]]>

#11: +<->++
-[<+<+>>-]<[>+<-]+<[>-<[-]]>[- [-]>[-]<
>++++++[<+++++++>-]<+.
+++++++++++++++++.
---------------.
+++++++++++++++++.
>+++[<------>-]<-..[-]]>

#12: .
-[<+<+>>-]<[>+<-]+<[>-<[-]]>[-[-]>[-]<
>+++++[<+++++++++>-]<+.[-]]>

#13: ,
-[<+<+>>-]<[>+<-]+<[>-<[-]]>[-[-]>[-]<
>++++[<+++++++++++>-]<.
[-]]>

#14: [<+<+>>-]
-[<+<+>>-]<[>+<-]+<[>-<[-]]>[-
[-]>[-]<
>+++++++[<+++++++++++++>-]<.
>+++++[<------>-]<-.
-----------------.
+++++++++++++++++.
-----------------.
>+++[<++++++>-]<+..-----------------.
>++++++[<++++++++>-]<.[-]]>

#15: [-]
-[<+<+>>-]<[>+<-]+<[>-<[-]]>[-[-]>[-]<
>+++++++[<+++++++++++++>-]<.
>+++++[<--------->-]<-.
>++++++[<++++++++>-]<.[-]]>

[-]<<<
]
>+++++++[<+++++++++++++>-]<++<<[>>.<<-]>>[-]<<++++++++++.[-]<
]

Output for input

qqq
10

is

[<+<+>>-]<[-] x y z.< x y z<.++[>+<------]>[[-]+<. x y z++[>+<------]>.+<->++[->-[>+>>]>[+[-<+>]>+>>]<<<<<][+[<+<+>>-] x y z[<+<+>>-][<+<+>>-][<+<+>>-]++[>+<------]>+<->++]]
-[->-[>+>>]>[+[-<+>]>+>>]<<<<<][<+<+>>-][-][ x y z++[>+<------]>,[-][->-[>+>>]>[+[-<+>]>+>>]<<<<<]-]
<[->-[>+>>]>[+[-<+>]>+>>]<<<<<]
+,++[>+<------]>-[[-]+<->++[[->-[>+>>]>[+[-<+>]>+>>]<<<<<],++[>+<------]>[->-[>+>>]>[+[-<+>]>+>>]<<<<<]++[>+<------]>.[+ x y z+<]]]
++[>+<------]>++-[-]-[-]+,[->-[>+>>]>[+[-<+>]>+>>]<<<<<][->-[>+>>]>[+[-<+>]>+>>]<<<<<][->-[>+>>]>[+[-<+>]>+>>]<<<<<]+,+[<++[>+<------]>,-+<->++]
-[<+<+>>-]-., x y z[ x y z<[<+<+>>-]< x y z<[+<->+++<->++-++[>+<------]>+<->+++[[<-[-][+<->++]]]]]
[<+<+>>-]++[<+<+>>-]-<<[-]+<->++<[]
,[->-[>+>>]>[+[-<+>]>+>>]<<<<<]++[>+<------]>++<->+++<->++[<+<+>>-]<
++[>+<------]>[<+<+>>-][+<->+++<->++[[->-[>+>>]>[+[-<+>]>+>>]<<<<<]<[<+<+>>-]++[>+<------]> x y z x y z<..[[-][<+<+>>-],[<+<+>>-]+,]]]
-, x y z<<[<[->-[>+>>]>[+[-<+>]>+>>]<<<<<].[,<-]]

and it runs in about 2 minutes on my box. Output contains entirety of BF syntax, including comments, and it well formed.

\$\endgroup\$
1
  • 11
    \$\begingroup\$ You're gonna be rich kid, when any of the major Brainfuck IDE developers see this... \$\endgroup\$ Apr 8, 2012 at 8:46

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