[Question]
  [
**Closed**. This question needs [details or clarity](/help/closed-questions). It is not currently accepting answers.
 
 
---
**Want to improve this question?** Add details and clarify the problem by [editing this post](/posts/240225/edit).
Closed 2 years ago.
[Improve this question](/posts/240225/edit) 
`[Sandbox](https://codegolf.meta.stackexchange.com/a/24186/98630)`
Your challenge is to write a program that when first run, will do one of the actions below at random with equal chances of both:
* A cat program
	+ A standard cat program: read everything from `STDIN` and put it on `STDOUT` (or your language's closest equivalent).
* Do nothing and halt
	+ This shouldn't output anything.
The behaviour is permanent - once the program chooses between the two options, it should do that no matter how much times you run the script. It's fine if this resets when you reboot.
Please provide an explanation on how to reset the program (which file to delete for example)
[Standard loopholes](https://codegolf.meta.stackexchange.com/questions/1061/loopholes-that-are-forbidden-by-default) are forbidden.
This is a [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the shortest code (in bytes or equivalent) wins.
  
[Answer]
# Python 3, 94 bytes
```
from random import*
s=randint(0,1)*"print(open(0).read())"
open(__file__,"w").write(s)
exec(s)
```
[Try it online!](https://tio.run/##nclBDoMgEAXQ/ZyCshGMITbdtl6FUB0jSYUJTGM9PRWTXqCr///7T5eXguMSRVPmFFeRXJiO8CvFxC3kRwUfWPXdVbeSUu2RMKhem4RuUlpLOMHa2b/Q2k5uUpsteUaVNeAHxyNLIwbBmNnQDkA7LzHcfiDuwvhAbzbs4/9nqQjnhAt8AQ "Bash – Try It Online")
Picks one of `​` or `print(open(0).read())` and replaces the contents of the file with it. Then, executes that code once.
To reset it, you will have to copy-paste this code into the file again, since it overwrites the file itself. This perseveres so long as the file exists and is not modified, so it is remembered through reboots and even if you move the file to another computer.
-5 bytes thanks to Jonathan Allan  
(Also -5 thanks to Jakque by using `choice(["print(open(0).read())",""])` instead of `randint(0,1)*"print(open(0).read())"` - both are excellent golfs; I just chose the first one.)
[Answer]
# [R](https://www.r-project.org/), ~~37~~ 36 bytes
*Edit: -1 byte thanks to Giuseppe*
```
{q=rt(1,1)<0;function(x)if(q)cat(x)}
```
[Try it online!](https://tio.run/##lZHBboMwDIbvPIXFpCmRaDUm7ULHm@ySggOWwGmdVKyq@uwsMEZbaYft5lj29//@I6OvWnFYEzco5Xg5lhJUnuX6/WVnT1wFcqw@NVl11JUJsbyOTyAnBgoQWgRL4mNFPYISw7XruzNUrXMePewxDIgMcROegd3GHXSRTJw8Sz841cm9vErNvoq9lW8aQwwGLA6zgi8S60QRxO5r8aYvEylNM8o8HsoZuPsFeE0ikt0ANXYY8Nv2chtUrj9M3e5cJNKr@/XFCccH/IKdkQN1HaCIk0lEcFOjJcaH@VWseDi3/EvYC/UnkFsUoHpq2hAjnu/xJn6A8XPthBpi03XnxU29Oshg3arJWhTksN1u9S3X/D@5jl8 "R – Try It Online")
Function that acts randomly as cat or no-op on first call, and then stays with this behaviour forever (or until deleted).
---
# [R](https://www.r-project.org/), 83 bytes
```
if(!F)write(paste("F=",F<-sample(1:2,1)),".Rprofile");if(F<2)show(scan("stdin",""))
```
[Try it online!](https://tio.run/##DctBCoAgEADAt7SnXbBAj2VXH9APpJQEU3EFn29e5jZ1jOBxMdRraA6L5SmYE4TRK9uvRIdyV0ISCdiuUrMP0QEdcxmtiN/ckW@bELg9IYEAIBrjBw "R – Try It Online")
Full program: saves state into ".Rprofile" file in directory from which [R](https://www.r-project.org/) or `Rscript` is launched: delete this to reset program behaviour.
[Answer]
# [Haskell](https://www.haskell.org/), 89 bytes
This method uses no special files. In fact it doesn't store data anywhere.
To set up your `randomize_va_space` should be set to zero (linux).
You can check this with:
```
sysctl -anr "e_v"
```
I won't tell you *how* to set it to `0` since in general this should not be set to `0`, and setting it to `0` represents a possible security risk.
Don't play with your kernel unless you know what you are doing or have nothing to lose on the device.
```
import Unsafe.Coerce
main=([[interact id],[pure()]]>>=([0..7]>>))!!mod(unsafeCoerce(+))16
```
[Try it online!](https://tio.run/##Jcu9CoAgFEDhvaewzUshtdRUS8/QJA5SN5K8Gv48v0VtBw7fqeOF1pZi6PYhsdVFfaBYPIYNK9LGTVxK4xIGvSVmdtXKOwfkoNQ8v68TYnwLoK7J7zx//ue8AeiHUpAIKT4 "Haskell – Try It Online")
TIO is stuck on cat and I have no way to reset it. You can switch the `+` to a `:` to see a version that is stuck on the noop.
To reset it locally it should be enough to set the `randomize_va_space` to `2` run the program once and then set it back to `0`.
## Explanation
*More detailed explanation [here](https://codegolf.stackexchange.com/a/213793/56656)*
In Haskell, all complex objects including functions are internally represented as a pointer. This is a number in binary that "points" to a specific location in memory. This is because we want to pass these values by reference, since copying the whole thing is expensive, and since Haskell disallows mutation we can with no problem.
However simple objects like ints are passed by value since they are so small that copying them is about as expensive as copying a pointer would be. unsafeCoerce is a super unsafe function which just takes the raw bytes from one object and reinterprets it as the raw bytes for another type.
So if we use unsafeCoerce from a function to an `Int`, the resulting `Int` is just the value of the pointer to the object. And the value of that simple object is dependent only on where the complex object is located not anything about what it is.
When `randomize_va_space` is on, this means the value of `unsafeCoerce(+)` is a random multiple of 8. However when it's off, it is fixed to one value which depends on some factors about the machine.
[Answer]
# [Bash](https://www.gnu.org/software/bash/) + coreutils, 64 bytes
```
awk -va=$(awk '!/E/{$0=(systime()-$1)%88}1' /proc/uptime) 'a<44'
```
[Try it online!](https://tio.run/##S0oszvj/P7E8W0G3LNFWRQPEUlfUd9WvVjGw1SiuLC7JzE3V0NRVMdRUtbCoNVRX0C8oyk/WLy0ASWgqqCfamJio//9fnJ@bWpKRmZcOAA "Bash – Try It Online")
Here's how it works.
`AWK` is called with a variable `a` which is a floating point number greater than or equal to 0 and less than 88. So it uses the `a<44` test to get a 50/50 chance to decide what action to take. If that evaluates as true, then it prints STDIN to STDOUT. If it's false, it silently consumes STDIN.
The value of `a` is set by the inner `AWK` call. That logic read the first numeric value in `/proc/uptime`, which is the number of seconds since the system was booted. The it subtracts that "uptime" from the current date/time to get the boot time of the OS. Then it computes a modulo of 88 seconds to deal with the (very annoying) fact that the calculations aren't consistent at the seconds level.
Essentially it's using the boot time of the OS to decide whether or not to "cat" or "discard" the input. So it will make the same decision each time it's run until the system is rebooted.
[Answer]
# [Zsh](https://www.zsh.org), ~~29~~ 21 bytes
```
X=$[RANDOM%2]
f()>&$X
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m724qjhjwYKlpSVpuhZbI2xVooMc_Vz8fVWNYrnSNDTt1FQiIHJ702xsbDLzCkpLuLCwIGpg5gAA)
A function which either does`>&1` (copy input to STDOUT) or `>&0` (write to STDIN, which doesn't work).
Can be shorter.
---
If using the PID as a source of randomness is allowed (which doesn't work on Attempt This Online because of how the sandbox works):
## Zsh, 11 bytes
```
f()>&$[$%2]
```
[Answer]
# [APL (Dyalog Unicode)](https://www.dyalog.com/), 15 bytes
Anonymous lambda. Takes one or two dummy arguments. Requires 0-based indexing.
```
{6::∇a∘←?2⋄a:⍞}
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///qG@qp/@jtgkG/6vNrKwedbQnPuqYAeTbGz3qbkm0etQ7r/Z/GpD/qLfvUVfzo941j3q3HFpv/KhtIlBncJAzkAzx8AwGquldw4WT0NXV5dJMLUosTlVI5BoApQA "APL (Dyalog Unicode) – Try It Online")
You can reset the program by erasing its global state `a`.
`{`…`}` "dfn":
 `6::` if we hit a value error:
  `a∘←` globally assign to `a`:
   `?2` a random 0 or 1
 `⋄` now try:
  `a:` if `a` [is 1]:
   `⍞` read STDIN [and implicitly return and print it]
[Answer]
# [C (gcc)](https://gcc.gnu.org/), 149 bytes
Stores either `s` or `u` into the `user.a` extended attribute of the executable. `setxattr()` doesn't replace an already-existing value, so I just write the value and then retrieve it to see whether to cat the input (`s`) or not (`u`).
To reset the program, run `setfattr -x user.a {programname}`
```
main(a,v,s)char*v,*s;{char r[]="user.a";srand(time(0));s=r+rand()%2;setxattr(*v,r,s,1,1);getxattr(*v,r,s,1);if(*s&2)while(~(a=getchar()))putchar(a);}
```
[Try it online!](https://tio.run/##XY1BCsJADEWvIgUlGaPYbsOcRFyEcWwHbJFkWgXRq4@t7tw9Hv/xw64NoZRe0gBCExmGTtRN5IyfC670ePLVaFH3UrGpDGfIqY9wQGTzuv0aXDdsMT8kZ4W5VjKqqUZu/yVyuoCzTYP3Ll0jvEH8PFquABFv4w8F@VVKjpY/ "C (gcc) – Try It Online")
[Answer]
# JavaScript (ES2020), browser, ~~56~~ 52 bytes
```
(localStorage.d||=Math.random())>.5&&alert(prompt())
```
Sets the `d` property of localStorage if it's undefined. Stacksnippets doesn't like setting localstorage, so run this in your console to see.
Full program. -4 thanks to tsh.
[Answer]
## Batch, 53 bytes
```
@if "%c%"=="" set/ac=%random%%%2
@if %c%==1 find/v""
```
Use `set c=` to reset the flag. Explanation: If `c` is empty, the first line sets `c` to a random value that is either `0` or `1`, then the second line copies STDIN to STDOUT only if `c` is `1`. I don't know why the builtin `find` command fails to find the empty string in any input, but it's consistent, so we can simply invert the condition, thus turning `find` into a `cat` program.
[Answer]
# [AWK](https://www.gnu.org/software/gawk/manual/gawk.html), 48 bytes
```
BEGIN{a=systime()%2;getline a<"_";print a>"_"}
a
```
[Try it online!](https://tio.run/##SyzP/v/fydXd06860ba4srgkMzdVQ1PVyDo9tSQnMy9VIdFGKV7JuqAoM69EIdEOyK7lSvz/P8Q1OETB0y8gNAQA "AWK – Try It Online")
`systime()` is used to decide whether to print or not. State is saved in the file `_`. `getline` does not clobber variables if it doesn't read anything.
[Answer]
# Python 3, ~~66~~ 62 bytes
```
import random
if random.randint(1,2)==1:print(str(input("I")))
```
Gets a random value and if it's 1, it gets and prints user input. I just put an I (for input) because my Python interpreter allows you to give input even when not asked for.
[Answer]
# [Zsh](https://www.zsh.org/), 35 bytes
```
((RANDOM&1))&&p='<&0'&&<&0
<<<$p>$0
```
[Try it online!](https://tio.run/##qyrO@G@nkJtYmZSanFiiYGOjoO7q76b@X0MjyNHPxd9XzVBTU02twFbdRs1AXU0NSHLZ2NioFNipGPwHKuTiKijKzCtRSMssKi5RqFGoKs6AG8ZVlFqQCjTT0EChWgGiTL0ktbikWB1NoULtfwA "Zsh – Try It Online")
Randomly overwrites the file with either `<&0` (equivalent to `cat`) or nothing.
[Answer]
# MATLAB/[Octave](https://www.gnu.org/software/octave/), ~~80~~ 60 bytes
```
global x;if x;else;x=randi(1:2);end;if x>1;input('','s');end
```
Will either do nothing, or whatever the user inputs to `ans=` (considered "STDOUT" for our purpose), then stick with that choice.
When first used a global variable (`x` here) is empty. Empty values evaluate to false. If `x` is false, it will set it to a random integer of either 1 or 2. This will then never evaluate false again, so the first part of the program will then never run again.
If `x` is 2, then the program will request user input (considered "STDIN" for our purpose). If `x` is 1, will do nothing at all.
---
Relies on the fact that a global variable is persistent once created, and will only reset if the user clears all variables ("reboots" for our purpose).
I would add a TIO link, but unfortunately that doesn't seem to want to handle global variables.
]