| Bytes | Lang | Time | Link |
|---|---|---|---|
| 090 | GFortran | 190812T111859Z | roblogic |
| 004 | Thunno 2 | 230815T132309Z | The Thon |
| 004 | Vyxal s | 210817T213159Z | emanresu |
| 040 | Zsh | 190812T092550Z | roblogic |
| 042 | PHP | 190811T113010Z | Night2 |
| 083 | TSQL 2012 | 190812T092546Z | t-clause |
| 027 | Ruby p | 190814T220319Z | Value In |
| 071 | C# Visual C# Interactive Compiler | 190812T035241Z | Gymhgy |
| 005 | MathGolf | 190814T125325Z | maxb |
| 061 | Pepe | 190814T105231Z | u-ndefin |
| 030 | Scala | 190812T163956Z | Dr Y Wit |
| 061 | Python3 | 190814T085829Z | personje |
| 010 | Gaia | 190813T201102Z | Giuseppe |
| 050 | Zsh | 190813T070702Z | GammaFun |
| nan | 190812T202517Z | Benjamin | |
| 023 | R | 190812T083134Z | Nick Ken |
| 133 | Oracle SQL | 190812T164757Z | Dr Y Wit |
| 051 | Javascript | 190812T072322Z | jonatjan |
| 068 | Lua | 190812T101310Z | val - di |
| 004 | 05AB1E | 190812T071036Z | Kevin Cr |
| 018 | Perl 5 p | 190812T053013Z | Xcali |
| 050 | C gcc | 190812T022228Z | ErikF |
| 002 | Japt f | 190812T023815Z | Gymhgy |
| 020 | Cubix | 190812T013612Z | MickyT |
| 010 | J | 190811T220942Z | Jonah |
| 024 | Wolfram Language Mathematica | 190811T204041Z | att |
| 005 | Pyth | 190811T130250Z | Sok |
| 003 | Japt | 190811T200917Z | Shaggy |
| 015 | Retina | 190811T165122Z | Neil |
| 004 | Charcoal | 190811T164421Z | Neil |
| 063 | Python 3 | 190811T123646Z | movatica |
| 011 | Befunge98 PyFunge | 190811T131551Z | negative |
| 005 | Jelly | 190811T130555Z | Nick Ken |
| 023 | Octave | 190811T123912Z | flawr |
| 009 | MATL | 190811T124824Z | flawr |
| 009 | APL dzaima/APL | 190811T111614Z | Adá |
GFortran, 120 90 bytes
Not too bad, if we use the deprecated RAN() function, which is pseudo-random, i.e. you get the same sequence each time. The proper way to generate random numbers in GFortran is with CALL RANDOM_SEED() and CALL RANDOM_NUMBER(R) but that's a lot of bytes!
character(99)S;read(*,'(A)')S;do i=1,len_trim(S)
if(ran()*5>1)call fput(S(i:i));enddo
end
Thunno 2, 4 bytes
æ5Lɼ
Explanation
æ5Lɼ # Implicit input
æ # Filter it by:
ɼ # Random number from
5L # [0, 1, 2, 3, 4]
# (×, ✓, ✓, ✓, ✓)
# Implicit output
Vyxal s, 4 bytes
'₀ʁ℅
Each char has 10% chance to be removed.
' # Filter by...
℅ # Random choice from
₀ʁ # 0...10
Zsh, 53 41 40 bytes
for c (${(s::)1})((RANDOM%4))&&printf $c
Converts the input to an array of characters, then tries to print each element c, unless it's eaten by the ((RANDOM%4)) evaluating to false!
PHP, 43 42 bytes
for(;''<$l=$argn[$i++];rand()%5&&print$l);
Each character has 20% of chance to be removed.
T-SQL 2012, 83 bytes
Looping through the input from right to left removing 0 or 1 character.
25% chance for each character getting removed.
DECLARE @i varchar(max)='The cat ate my homework'
DECLARE @ int=len(@i)WHILE @>0SELECT
@i=stuff(@i,@,str(rand()*2)/2,''),@-=1PRINT @i
Explanation:
rand()*2returns a float, which can't be used in thestuffcommand.The
strconverts this into a varchar after rounding to nearest whole number. The float is being converted to a varchar(which isn't allowed as third parameter instuffeither).This varchar has a 25% chance of being '2', 50% chance of being '1', 25% chance of being '0'. Dividing by 2, there is a 25% chance of result being 1. This division converts the varchar to an integer.
Integer is the expected third parameter in
stufffunction.
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}
C# (Visual C# Interactive Compiler), 71 bytes
var y=new Random();foreach(var k in ReadLine())if(y.Next(5)<4)Write(k);
MathGolf, 5 bytes
æƒ√∞*
Explanation
æ foreach character...
ƒ random float in range [0,1)
√ take square root (making P(x < 0.5) = 0.25)
∞ pop a, push 2*a
* repeat character int(x) times
Each character will be repeated 0 or 1 times, depending on the random value. Since the expected value after the square root is shifted, there is a 25% probability that each character is removed.
Alternative 5-byter
gÉ;4w
Filter the characters by a random number in [0, 4]. Due to how filtering works, I have to discard the actual character within the filter loop, which adds 1 byte.
Pepe, 61 bytes
REEerEERREeeeeEeEeREEeEerEEEEErrEEReReEeRRERRErEereereReReree
There is 11.11% or \$\frac{1}9\$ chance of characters being deleted.
Explanation:
... # 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
Scala, 51 46 30 bytes
s=>s.flatMap(x=>if(math.random>.2)Some(x)else None)
PS. Like in many other solutions, the probability of dropping char is 20%.
Update:
-5 bytes by using String instead of Option[String] in flatMap
s=>s.flatMap(x=>if(math.random>.2)x+""else "")
30 bytes by using filter
s=>s.filter(x=>math.random>.2)
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:])
Gaia, 10 bytes
⟨w8&Ø+ṛ⟩¦$
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
Zsh, 50 bytes
for c (${(s::)"$(<&0)"})
((RANDOM%5))&&echo -nE $c
Similar to RobLogic's answer, but following the input requirements more closely, and works for inputs with backslashes.
"$(<&0)" instead of "<&0" or $(<&0) because the first doesn't work in substitutions, and the second eats newlines. The -nE flags are necessary to prevent backslashes from being parsed as escape sequences, and to prevent newlines being inserted.
echo -nE
Java
Non-terminating: 82 bytes
v->{for(int i;;i=System.in.read(),System.out.print(Math.random()<.2?"":(char)i));}
v->{var n=System.in;for(int i;n.available()>0;i=n.read(),System.out.print(Math.random()<.2?"":(char)i));}
R, 32 23 bytes
function(x)x[rt(x,3)<1]
A function taking a character vector as input and returning a processed character vector. Each character has a 20% chance of being removed.
Thanks to @Roland and @Giueseppe for helping save 7 bytes, and @JDL for a further 2!
Oracle SQL, 133 bytes
select listagg(decode(sign(dbms_random.value-0.2),1,substr(x,level,1)))within group(order by level)from t connect by level<=length(x)
It works with an assumption that input data is stored in a table t(x), e.g.
with t(x) as (select 'The cat ate my homework' from dual)
Javascript, 46 44 51 bytes
i=>alert([...i].filter(c=>Math.random()>.2).join``)
+7 bytes because of the added STDOUT requirement
-2 bytes thank to Birjolaxew
original answer: 44 bytes without the STDOUT requirement
i=>[...i].filter(c=>Math.random()>.2).join``
Lua, 69 68 bytes
for c in io.lines(nil,1)do io.write(math.random()>.2 and c or '')end
Kinda straightforward, but seems to be shortest version: iterate over stdin char by char (with io.lines… that name is misleading), then based on random value either print one or empty string (e.g. nothing).
05AB1E, 5 4 bytes
ʒ₄Ω≠
-1 byte thanks to @Grimy.
Try it online or run the same program 10 times.
Each character has a 25% change of being dropped.
Explanation:
ʒ # Filter the characters of the (implicit) input-string by:
₄ # Push 1000
Ω # Pop and push a random digit from it
≠ # And check that it's NOT 1 (!=1)
# (after which the result is output implicitly)
≠ could also be _ (==0).
C (gcc), 50 bytes
This program has a 20% chance of dropping a letter. Unfortunately the random number generator isn't seeded so you get the same sequence on each run. Basically the only trick is inverting the input character to halt the loop on EOF.
main(c){for(;c=~getchar();rand()%5&&putchar(~c));}
C (gcc), 64 59 bytes
Thanks to ceilingcat for the -5 bytes.
If you want the RNG seeded on each run.
main(c){for(srand(&c);c=~getchar();rand()%5&&putchar(~c));}
Japt -f, 2 bytes
5ö
The -f flag "runs the program on each element in the first input, outputting an array of those that return a truthy value." 5ö returns a random number between 0(inclusive) and 5(exclusive). Like JavaScript, 0 is falsy in Japt.
Cubix, 20 bytes
u$w\A|UDw@?;...>o._U
Longer than I had hoped as I had a number of no-ops that I can't seem to get rid of. The chance to drop a character is 25%. I assume this is okay.
u $
w \
A | U D w @ ? ;
. . . > o . _ U
. .
. .
Brief explanation:
A|Athis initialises the the stack, Input all, reflect back, Input all (just an EOI -1);?pop to of stack, test for EOI (-1)._?@if negative, reflect back into test and end on halt$Djump the\into the random direction setter.- from the direction setter, 3 direction lead to the
ofor output then back into the loop, one misses theoin it's path and goes straight to the loop.
- from the direction setter, 3 direction lead to the
J, 10 bytes
#~5>6?@$~#
Similar to Adam's APL answer, though I actually wrote it before looking at his.
6.. $~ #Take the length of input#and shape$~the number 6 into a list that long.?@Treat each six in that list as a die and roll?it.>5Is the die less than 5 (possible values are 0..5)? Use that boolean result to create a bit mask.#~Filter the input with that mask.
Wolfram Language (Mathematica), 24 bytes
Select[RandomReal[]>.2&]
Takes a list of characters as input. Each character has a .2 chance to be removed.
Pyth, 8 5 bytes
sfO4Q
sfO4Q Implicit: Q=eval(input())
f Q Filter characters of Q where the following is truthy:
O4 Random number in the range [0-4)
Any non-zero value is truthy, so this will drop characters 25% of the time
s Concatenate into string, implicit print
Previous version, 8 bytes:
s*Vm!!O4
s*Vm!!O4QQ Implicit: Q=eval(input())
Trailing QQ inferred
m Q Map each character in Q using:
O4 Choose random integer in [0-4)
!! Logical NOT twice - maps 0 to 0, anything else to 1
The result is a list of 0s and 1s, with 0 having 25% chance to appear
*V Q Vectorised multiplication of the above with Q
s Concatenate into string, implicit print
Japt, 3 bytes
Each character has a 1 in 5 chance of being removed. The 5 can be changed to anything between 4 & 9, inclusive, or A for 10 to change the odds.
Æ5ö
Æ5ö :Implicit input of string
Æ :Filter by
5ö : Random integer in the range [0,5), with 0 being falsey
Retina, 15 bytes
/./_?(`.
Try it online! Explanation:
/./_
Process each character individually.
?(`
Perform a substitution at random. The first substitution deletes the character, while the other three leave it unchanged, thus giving a 25% chance of deleting the character. This can be decreased as necessary by appending additional pairs of newlines.
Charcoal, 4 bytes
ΦS‽⁵
Try it online! Link is to verbose version of code. Explanation:
S Input a string
Φ Filter where nonzero
‽⁵ Random number 0..4
Implicitly print
You can use any number from 4 to 10 to get chances of 25% to 10% respectively.
Python 3, 63 bytes
from random import*
for c in input():print(end=c[random()<.2:])
Python 2, 67 65 bytes
from random import*
print''.join(c for c in input()if.8>random())
Each character has a 20% chance of beeing dropped.
Different approach, same length:
from random import*
print''.join(c[random()<.2:]for c in input())
Befunge-98 (PyFunge), 11 bytes
>#@~3j4???,
Each character has a 25% chance of being removed. This decision is made at the three ? instructions.
? sets the program counter to one of the four directions, with equal probability. In this case, up & down wrap back around to the same instruction, so we can ignore those as options.
There are two ways out of the forest of ?s: to the right (output) and to the left (no output). This situation is symmetric, so if starting from the middle ?, there is a \$p_2 = 1/2\$ chance of outputting. The chance of outputting if starting from the right ? is \$p_3 = 1/2 * 1 + 1/2 * p_2 = 3/4\$. Therefore, after reading a character, we jump to the rightmost ? to determine whether or not to output.
Jelly, 9 5 bytes
5X’µƇ
A monad which takes a Jelly string as its argument and returns the processed Jelly string. When used as a full program implicitly prints the output. Each character has a 20% chance of being removed.
Explanation
µƇ | Filter using the following as a monad for each character:
5X | - Random number between 1 and 5
’ | - Decreased by 1
Octave, 23 bytes
Generates an array of the same size as the input (strings in Octave are character arrays), checks each of the random numbers whether it is greater than 0.2 and then uses logical indexing to extract the characters at the corresponding positions.
@(s)s(rand(size(s))>.2)
MATL, 9 bytes
t&n&r.2>)
Exaplanation:
t implicitly take input and duplicate it
&n compute the size of the input and...
&r generate a random array of that size
.2> check which entries of that array are greater than 0.2
) and use the result using logical indices to extract certain characters of the input
APL (dzaima/APL), 10 9 bytesSBCS
Anonymous tacit prefix function. Each character has exactly 20% chance of being removed.
⊢⌿⍨4≥∘?5¨
5¨ zero for each character
? random integer range 1–5 for each character
4≥ Boolean mask for those integers that are less than or equal to 4
⊢⌿⍨ filter the argument using that mask