Lorem Ipsum Random Code Generation

6

2

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!

Alex L

Posted 2012-04-03T05:18:59.403

Reputation: 193

possible duplicate of Generate a random program in your favorite language

– user unknown – 2012-04-03T14:11:06.163

If you were to assign a point value to each of the requirements you'd have something rather more like the "objective winning criteria" required. Without that the issue of duplication is moot. Question for the crowd does [code-golf]/not [code-golf] constitute a sufficient distinction to avoid closing? – dmckee --- ex-moderator kitten – 2012-04-03T15:15:27.517

Good point, and thanks for the link. I'll improve the question to include a scoring system tomorrow morning (it's 2am now). – Alex L – 2012-04-03T17:47:40.423

2This 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 – captncraig – 2012-04-03T18:08:50.990

@dmckee I've modified the challenge, and anxiously await everyone's entries! I plan to add a bounty and my own entry to increase the appeal. – Alex L – 2012-04-04T09:44:24.370

3As 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. – dmckee --- ex-moderator kitten – 2012-04-04T14:20:48.020

1What'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? – ceased to turn counterclockwis – 2012-04-05T11:31:28.277

Answers

9

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.

captncraig

Posted 2012-04-03T05:18:59.403

Reputation: 4 373

10You're gonna be rich kid, when any of the major Brainfuck IDE developers see this... – ceased to turn counterclockwis – 2012-04-08T08:46:32.363