311c342bfd324eb9d3603222f7f36f24

Probably better ways to do this, quickly wrote it up after a joke in IRC..

# generate_bf.rb
# (copyleft) Matt Gauger 2008
# Randomly generates a brainfuck program
# Commandline argument is used as the length of the program
# Otherwise, the program defaults to 140 characters in length to fit on Twitter
length = 140
program = ""

ARGV.each do |n|
  if n != nil and n != 0
    length = n.to_i
  end
end

length.downto(1) { |n| 
  choice = rand(7)
  
  case choice
    when 0
      program.concat('>')
    when 1
      program.concat('<')
    when 2
      program.concat('+')
    when 3
      program.concat('-')
    when 4
      program.concat('.')
    when 5
      program.concat(',')
    when 6
      program.concat('[')
    when 7
      program.concat(']')
    else
  end
}

puts program

Refactorings

No refactoring yet !

Fcd45b7de93cc8fa417fa43426973e06

JonM1827

July 31, 2008, July 31, 2008 20:20, permalink

No rating. Login to rate!

I'm sure there are much better ways to do this. I never use ruby, so I don't really know the ins and outs of it. Just off the top of my head though, this will put it down from 35 lines to 15 lines.

length = 140
program = ""
chars = ['>','<','+','-','.','[',']']

ARGV.each do |n|
  if n != nil and n != 0
    length = n.to_i
  end
end

length.downto(1) { |n|
  program.concat(chars[rand(7)])
}

puts program
1e8f141e7857d397d8020ed3b759e88a

Maciej Piechotka

July 31, 2008, July 31, 2008 20:31, permalink

1 rating. Login to rate!

A little bit different working - but in 3 lines

# generate_bf.rb
# (copyleft) Matt Gauger 2008
# Randomly generates a brainfuck program
# Commandline argument is used as the length of the program
# Otherwise, the program defaults to 140 characters in length to fit on Twitter

# generated Brainfuck code not guaranteed to do anything !!

length = ARGV.last.to_i || 140
chars = %w{< > + - . , [ ]}
puts Array.new(length) {chars[rand(chars.length)]}.join
Fcd45b7de93cc8fa417fa43426973e06

JonM1827

July 31, 2008, July 31, 2008 20:37, permalink

No rating. Login to rate!

and that is why I need to learn ruby at some point :)

edit:
Just one change to make it work...

@Maciej ARGV.last is nil, but ARGV.last.to_i is 0...

-Jon

# generate_bf.rb
# (copyleft) Matt Gauger 2008
# Randomly generates a brainfuck program
# Commandline argument is used as the length of the program
# Otherwise, the program defaults to 140 characters in length to fit on Twitter

# generated Brainfuck code not guaranteed to do anything !!

length = ARGV.last ? ARGV.last.to_i : 140
chars = %w{< > + - . , [ ]}
puts Array.new(length) {chars[rand(chars.length)]}.join
311c342bfd324eb9d3603222f7f36f24

mattgauger

July 31, 2008, July 31, 2008 20:57, permalink

No rating. Login to rate!

Cool, I'm learning lots in a very short time span! :)

This is a superficial hack to add some of the example 'patterns' from the Brainfuck Wikipedia article, for more interesting generated programs. Of course, this breaks the nice ability to specify output length.

Any suggestions?

length = ARGV.last ? ARGV.last.to_i : 140

chars = %w{< > + - . , [ ] [-] ,[.,] >,[.>,] [->+<] >[-]<[->+<]}

# added 'patterns':
# [-] cell clear
# ,[.,] simple loop
# >,[.>,] moving the pointer
# [->+<] addition, destructive
# >[-]<[->+<] byte copy

puts Array.new(length) {chars[rand(chars.length)]}.join
Fcd45b7de93cc8fa417fa43426973e06

JonM1827

July 31, 2008, July 31, 2008 21:11, permalink

1 rating. Login to rate!

I'm sure there is a much more elegant way to do this as it is generating far to many things, but there is always the lazy way of doing things... substring :)

length = ARGV.last ? ARGV.last.to_i : 140
chars = %w{< > + - . , [ ] [-] ,[.,] >,[.>,] [->+<] >[-]<[->+<]}
puts Array.new(length) {chars[rand(chars.length)]}.join[0, length]
33a7163d5fbf2609f7c16afc5e1c867c

abhi

September 5, 2008, September 05, 2008 15:07, permalink

No rating. Login to rate!
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
 int a,i,n;
 double b=0;
 printf("enter a no.");
 scanf("%d",&n);
 for(i=0;i<=6;i++)
 {
 a=n%2;
 n/=2;
 b+=a*pow(10,i);
 }
 printf("%7.0f",b);
 getch();
}
D41d8cd98f00b204e9800998ecf8427e

sigs

January 6, 2010, January 06, 2010 17:26, permalink

No rating. Login to rate!

indent intended.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
 int a,i,n;
 double b=0;
 char *s = "mehmeh";
 printf("enter a no.");
 scanf("%s",&s);
 if (strcmp(s, "no"))
 printf("You didn't enter a no  )):");
 else
 printf("kthxmaet");
 getch();
}
221ae1a04fc3e1b5b7d3d98d87669f5f

Cees Timmerman

November 20, 2011, November 20, 2011 05:36, permalink

No rating. Login to rate!

Python 3 one-liner for 140 bytes of random bf code.

import random; print(''.join(random.choice('<>+-.,[]') for i in range(140)))
221ae1a04fc3e1b5b7d3d98d87669f5f

Cees Timmerman

November 20, 2011, November 20, 2011 05:37, permalink

No rating. Login to rate!

Python 3 one-liner for 140 bytes of random bf code.

import random; print(''.join(random.choice('<>+-.,[]') for i in range(140)))
435329553773fd72bba9cc8534e8da65

Cees Timmerman

November 20, 2011, November 20, 2011 05:38, permalink

No rating. Login to rate!

Python 2.7+ one-liner for 140 bytes of random bf code.

import random; print(''.join(random.choice('<>+-.,[]') for i in range(140)))

Your refactoring





Format Copy from initial code

or Cancel