Created
January 10, 2020 14:26
-
-
Save rmax/93e092b625a2f266bf8eff11acf5c606 to your computer and use it in GitHub Desktop.
Entropy experiment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Entropy experiment.""" | |
from dataclasses import dataclass | |
from math import inf | |
from secrets import randbits | |
DEFAULT_STEP: int = 64 # shall we use bigger step? | |
@dataclass | |
class Entropy: | |
"""<3 entropy""" | |
state: int | |
iteration: int = 0 | |
def has_heath_death(entropy: Entropy) -> bool: | |
"""Are we death? | |
Returns True or False, if you are lucky. | |
""" | |
return entropy.state == inf | |
def increase_entropy(entropy: Entropy, step: int = DEFAULT_STEP) -> int: | |
"""Because why not. | |
Returns entropy change delta. | |
""" | |
current_state = entropy.state | |
entropy.iteration += 1 | |
entropy.state = randbits(entropy.iteration) | |
return abs(current_state - entropy.state) | |
def main(initial_state: int, increase_step: int) -> int: | |
"""Execution entrypoint.""" | |
entropy = Entropy(initial_state) | |
delta = 0 | |
while not has_heath_death(entropy): | |
print(f"Still alive!\tEntropy Delta: {delta:x}") | |
delta = increase_entropy(entropy) | |
print("You dead. Long live Vim") | |
return -1 | |
if __name__ == "__main__": | |
import random | |
import os | |
import sys | |
# For reproduceability (Yeah, I don't know how to spell :P) | |
random.seed(int(os.getenv("RANDOM_SEED", 0))) | |
initial_step = int(os.getenv("ENTROPY_INITIAL_STATE", 0)) | |
increase_step = int(os.getenv("ENTROPY_INCREASE_STEP", 1)) | |
sys.exit(main(initial_step, increase_step)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment