Compare commits

...

2 commits

Author SHA1 Message Date
Emma Nora Theuer
3799bcb0fb Add random cryptography hyperfixation that won't last longer than 5 minutes 2025-02-07 00:12:15 +01:00
Emma Nora Theuer
fdd5db23bf Needed this for my D&D games 2025-02-07 00:11:37 +01:00
2 changed files with 128 additions and 0 deletions

58
MonteCarlo.c Normal file
View file

@ -0,0 +1,58 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
struct Simulation {
double betsize;
double STARTING_BANKROLL;
double bankroll;
int attempts;
};
int roll() {
return (random() % 7) + 1;
}
double handle(int player, int house) {
int diff = house - player;
if (diff >= 1) {return 0;}
else if (diff == -6) {return 2;}
else if (diff == 0) {return 0;}
else {return 1.5;}
}
bool calculate(struct Simulation* simulation) {
if (simulation->bankroll >= simulation->betsize) {
simulation->bankroll -= simulation->betsize;
int player_roll = roll();
int house_roll = roll();
double modifier = handle(player_roll, house_roll);
simulation->bankroll += (modifier * simulation->betsize);
simulation->attempts++;
return false;
} else {return true;}
}
void run(struct Simulation* simulation, int attempts) {
for (int i = 0; i < attempts; i++) {
bool bankrupt = calculate(simulation);
if (bankrupt) {
break;
}
}
}
int main() {
srandom(time(NULL));
struct Simulation new_simulation;
new_simulation.betsize = 20;
new_simulation.STARTING_BANKROLL = 5000000000;
new_simulation.bankroll = new_simulation.STARTING_BANKROLL;
new_simulation.attempts = 0;
run(&new_simulation, 2147483647);
printf("Starting Bankroll: %f\n", new_simulation.STARTING_BANKROLL);
printf("Current Bankroll: %f\n", new_simulation.bankroll);
printf("Finished after %d attempts and with a betsize of: %f\n", new_simulation.attempts, new_simulation.betsize);
return 0;
}

70
vigenere.py Normal file
View file

@ -0,0 +1,70 @@
#!/usr/bin/env python3
import argparse
import string
parser: argparse.ArgumentParser = argparse.ArgumentParser(
prog='Basic Vigenère Cypher',
description='A quick and dirty implementation of the Vigenére Cypher. Accepts any key as the key.',
epilog='Enter the message first and the key second.'
)
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-e', '--encrypt', nargs=2, help="Message to be decrypted or encrypted and the key used")
group.add_argument('-d', '--decrypt', nargs=2, help="Message to be decrypted or encrypted and the key used")
class Vigenere:
def __init__(self) -> None:
self.args = parser.parse_args()
self.chars: str = ''.join(c for c in string.printable if c.isprintable()) + ''.join(chr(c) for c in range(128, 256) if not chr(c).isspace or c in (160,)) # All Characters from extended ASCII, excluding control codes
if self.args.encrypt:
self.msg: str = self.args.encrypt[0]
self.key: str = self._generate_key(self.args.encrypt[1])
else:
self.msg: str = self.args.decrypt[0]
self.key: str = self._generate_key(self.args.decrypt[1])
def _generate_key(self, key: str) -> str:
if len(key) < len(self.msg):
key = (key * (len(self.msg) // len(key) + 1))[:len(self.msg)]
else:
key = key[:len(self.msg)]
return key
def _create_alphabet(self, shift: int) -> str:
alphabet: str = self.chars
shift = (shift % len(alphabet))
return alphabet[shift:] + alphabet[:shift]
def _find_index(self, index: int) -> int:
return self.chars.index(self.key[index])
def encrypt(self) -> str:
encrypted_msg: str = ""
for index in range(len(self.msg)):
char: str = self.msg[index]
lookup_alphabet: str = self._create_alphabet(self._find_index(index))
original_index: int = self.chars.index(char)
encrypted_msg += lookup_alphabet[original_index]
return encrypted_msg
def decrypt(self) -> str:
decrypted_msg: str = ""
for index in range(len(self.msg)):
lookup_alphabet: str = self._create_alphabet(self._find_index(index))
location_index: int = lookup_alphabet.index(self.msg[index])
decrypted_msg += self.chars[location_index]
return decrypted_msg
def output(self) -> None:
if self.args.encrypt:
ciphertext = self.encrypt()
print(ciphertext)
else:
plaintext = self.decrypt()
print(plaintext)
if __name__ == "__main__":
vigenere_cipher = Vigenere()
vigenere_cipher.output()