code: def randomize(vm,
          length=(10,10),
          ints=(0,999),
          strs=(1,10),
          chars=(32,126),
          instruction_ratio=0.5,
          number_string_ratio=0.8,
          exclude=map(crianza.instructions.lookup, [".", "exit", "read", "write", "str"]),
          restrict_to=None):
  
      
      vm.code = []
      instructions = set(vm.instructions.values()) - set(exclude)
  
      if restrict_to is not None:
          instructions = instructions.intersection(set(restrict_to))
  
      instructions = list(instructions)
  
      for _ in xrange(random.randint(*length)):
          r = random.random()
          if r <= instruction_ratio:
              # Generate a random instruction
              vm.code.append(random.choice(instructions))
          elif r <= number_string_ratio:
              # Generate a random number
              vm.code.append(crianza.compiler.make_embedded_push(random.randint(*ints)))
          else:
              # Generate a random string
              vm.code.append(crianza.compiler.make_embedded_push('%s' %
                  "".join(chr(random.randint(*chars)) for n in xrange(0,
                      random.randint(*strs)))))
      return vm
docs: Replaces existing code with completely random instructions. Does not
      optimize code after generating it.
  
      Args:
          length: Tuple of minimum and maximum code lengths. Code length will
          be a random number between these two, inclusive values.
  
          ints: Integers in the code will be selected at random from this
          inclusive range.
  
          strs: Inclusive range of the length of strings in the code.
  
          chars: Inclusive range of characters in random strings.
  
          instruction_ratio: Ratio of instructions to numbers/strings,
          meaning that if this value is 0.5 then there will just as many
          instructions in the code as there are numbers and strings.
  
          number_string_ratio: Ratio of numbers to strings.
  
          exclude: Excluded instructions. For genetic programming, one wants
          to avoid the program to hang for user input.  The default value is
          to exclude console i/o and debug instructions.
  
          restrict_to: Limit instructions to the given list.
  
      Returns:
          The VM.