Compare commits
2 commits
47c2d6a63b
...
3799bcb0fb
Author | SHA1 | Date | |
---|---|---|---|
|
3799bcb0fb | ||
|
fdd5db23bf |
2 changed files with 128 additions and 0 deletions
58
MonteCarlo.c
Normal file
58
MonteCarlo.c
Normal 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
70
vigenere.py
Normal 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()
|
Loading…
Reference in a new issue