Create a cat program, a.k.a a program that takes an input and prints it.

...Except, the program will randomly take characters away from your input and print that instead.

Each character in the input should have generally equal odds to be removed with the program, though, as it is hard to make that, the odds for each character can vary by 10% at most.

Your program should take the input, then randomly remove characters from the input, then print that version again. (You can print with trailing newlines or other characters in case your language has to print newlines.)

If the input was BOOOWL, it shouldn't remove all Os with an equal chance: each character (not unique) should be considered, so instead of every O combined having a 1/5 chance (for example), each O should have a 1/5 chance, so, instead of there being a 1/5 chance of BWL, there should be a 1/5 chance of BOWL, BOOWL.

Input is restricted to STDIN or closest equivalent.

Each character must have a minimum of 10% and a maximum of 30% chance to be removed.

Each character's odds should be calculated individually.

You can use any component of your language supporting random actions, be it functions or something other.

Output must be through STDOUT or the closest equivalent. If your language does have STDOUT, do not output in any other way. If your language cannot output strings as text, use closest equivalent (C's character array output's OK here).

This is code golf. Shortest program wins.

  • 4
    \$\begingroup\$ Should there always be between 10% and 30% chance for a specific character to be removed? Or is that just for the purpose of the example? \$\endgroup\$ – att Aug 11 '19 at 20:40
  • 2
    \$\begingroup\$ what do you mean by "sets of one character"? if the input is BOWL OF SOUP might all the O's be deleted in one go? \$\endgroup\$ – roblogic Aug 12 '19 at 5:14
  • 1
    \$\begingroup\$ all answers thus far use a fixed 20% chance for a character to be removed. I'm not sure the question intent is for all characters to have the same odds. \$\endgroup\$ – Nzall Aug 12 '19 at 7:10
  • 3
    \$\begingroup\$ Output must be through STDOUT, as a text. Do not output a character array. <-- I have a language that allows you to output a character array (it is flattened before output). Is that disallowed? How about languages like C, where a string is basically a character array? \$\endgroup\$ – Ismael Miguel Aug 12 '19 at 9:12
  • 1
    \$\begingroup\$ Use the closest equivalent. C's character array strings are OK, as they are the closest equivalent to text. \$\endgroup\$ – AndrewTheCodegolfer Aug 12 '19 at 9:20

34 Answers 34


Gaia, 10 bytes


Try it online!

Has a 1/9th (11%) chance of removing any given character. This abuses the fact that the meta ¦ treats a string as a list, much as Python does.

 ⟨	⟩¦	| for each character do:
  w8&		| wrap as list and duplicate 8 times
     Ø+		| add empty string to that list
       ṛ	| select element at random
	  $	| and join with no separator
		| implicitly print top of stack

Python3, 61 bytes

Does this count? I guess if you consider execution across seconds of time "random"

import time
for c in input():print(end=c[time.time()%5<1:])
  • \$\begingroup\$ Welcome to the site! Is it possible you could add a link to an online testing site, such as TryItOnline!, so that other users can verify your answer? Especially given that, as far as I can see, this doesn't remove any characters when run. \$\endgroup\$ – caird coinheringaahing Aug 14 '19 at 9:26

Pepe, 61 bytes


Try it online!

There is 11.11% or \$\frac{1}9\$ chance of characters being deleted.


... # some-command -> (stack) // some-explanation

REEe # Input (str) -> (R)
rEE # Create label 0 -> (r) // main loop begins
  RREeeeeEeEe # Push 10 -> (R) // this is for the randomiser
              # R flag: place it in the beginning
  REEeEe # Random number from 1 to 10 -> (R)
  rEEEEE # Increment -> (r) // 0 > 1, for the next command
  rrEE # Create label 1 -> (r) // without previous command, the existing label 0 is replaced
       # r flag: Skip until rEe (return)
    Re # Pop -> (R) // removes the random number
    ReEe # Output as char & pop it -> (R)
    RRERRE # Push 0 2 times -> (R) // these are popped to fix the pos of R stack
  rEe # Return to where goto was called
  ree # Goto 1 if 1 != random from 1 to 10 // The whole rrEE ... rEe ree is an if..then
  re # Pop -> (r)
  ReRe # Pop 2 times -> (R)
ree # Loop while char of (R) != 0

Ruby -p, 27 bytes

The -p flag takes each line of input as the global variable $_ and runs it through the code block and then prints its contents. gsub modifies $_ by replacing characters that match the given regex (only works if -p or -n is present).

gsub(/.|\n/){$&if rand>0.2}

Try it online!


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.