finally, some good fucking random

This commit is contained in:
nora 2020-12-05 23:15:16 +01:00
parent 95834b3586
commit a395a0dc98
15 changed files with 170 additions and 4 deletions

47
src/main/java/Buddy.java Normal file
View file

@ -0,0 +1,47 @@
import java.util.Scanner;
/**
* When you're really lonely in your code and you need a buddy
*/
public class Buddy {
/**
* The name of the Buddy, for example Bobby
*/
private String name;
/**
* The scream of the buddy when it realizes that it's not real. No use cases for it. It's not self-conscious or anything like that. Yet. (the scream is a constant because the buddy will constantly be screaming (it's not a constant because of memory issues, simply call setSCREAM to be able to use it))
*/
public String SCREAM;
public Buddy(String name) {
this.name = name;
setSCREAM();
}
public void help(){
Scanner scn = new Scanner(System.in);
System.out.println("Hi my friend, I'm " + name);
System.out.println("What's you problem?");
scn.nextLine();
System.out.println("Sorry I can't help you with that because my code is very limited just like you ☜(゚ヮ゚☜)☜(゚ヮ゚☜)☜(゚ヮ゚☜)☜(゚ヮ゚☜)☜(゚ヮ゚☜)☜(゚ヮ゚☜)");
}
public void joke() {
System.out.println("There 👉 is 😶 nothing ❌ funny 😂 about ↩ my 😀👈 life ♥.");
try {
Thread.sleep(300);
} catch (InterruptedException ignored) {
}
System.out.println("Wait, there is something.");
try {
Thread.sleep(1400);
} catch (InterruptedException ignored) {
}
System.out.println("You.");
}
public void setSCREAM(){
SCREAM = "A".repeat(Integer.MAX_VALUE).repeat(Integer.MAX_VALUE).repeat(Integer.MAX_VALUE);
}
}

View file

@ -0,0 +1,48 @@
/**
* While being a very useful class, SingedInteger has a very big problem. It can't store decimal numbers.
* That's why this class exists.
*/
public class DecimalNumber {
long integerPart;
long decimalPart;
/**
* Create a new DecimalNumber from a d**ble
* A double, you ask? Shut the fuck up, I say.
* @param n The d**ble
*/
public DecimalNumber(double n){
String s = String.valueOf(n);
String[] ss = s.split("\\.");
System.out.println(ss[0]);
System.out.println(ss[1]);
integerPart = Long.parseLong(ss[0]);
decimalPart = Long.parseLong(ss[1]);
}
/**
* Create a new DecimalNumber directly from a String. No more stupid parsing, just stupid exceptions!
* @param s the thing with funny chars I forgot the name it was something long and thin
* @throws ValueShouldProbablyActuallyBeANumberAndNotSomeWeirdGibberishDumbProgrammerException yeah it does actually throw that good luck catching it
*/
public DecimalNumber(String s) throws ValueShouldProbablyActuallyBeANumberAndNotSomeWeirdGibberishDumbProgrammerException {
try {
String[] ss = s.split("\\.");
System.out.println(ss[0]);
System.out.println(ss[1]);
integerPart = Long.parseLong(ss[0]);
decimalPart = Long.parseLong(ss[1]);
} catch (NumberFormatException | ArrayIndexOutOfBoundsException e){
throw new ValueShouldProbablyActuallyBeANumberAndNotSomeWeirdGibberishDumbProgrammerException();
}
}
/**
* Get the value as a d**ble
* @return The value as a d**ble
*/
public double getValue(){
return Double.parseDouble(integerPart + "." + decimalPart);
}
}

View file

@ -0,0 +1,54 @@
import Requests.OpenTDB;
import java.util.ArrayList;
import java.util.Collections;
/**
* Generates random numbers based on OpenTDB trivia results. Should not be used too often. Should not be used actually.
*
* <p>
* Every trivia question has 4 answers, giving us a random number out of 4 for every question
* 2 question can give an octal digit, which can be converted to an int
* 200IQ move, ik
* </p>
*/
public class GoodRandom {
OpenTDB trivia = new OpenTDB();
public GoodRandom(int seed) {
trivia.setCategory(seed % 20 + 10);
}
public GoodRandom() {
this(0);
}
public int randomInt(int max){
return randomInt(0, max);
}
public int randomInt(int min, int max){
int range = max - min;
//exponent of next biggest power of two
int digits = (int) Math.ceil(Math.log(range)/Math.log(2));
int twoDigits = (int) Math.ceil(digits / 2d);
int resultNumber = 0;
for (int i = 0; i < twoDigits; i++) {
resultNumber += Math.pow(4, i) * getFourDigit();
}
int resultNormalized = resultNumber % range;
return resultNormalized += min;
}
private int getFourDigit(){
trivia.getTrivia();
ArrayList<String> answers = new ArrayList<>();
answers.add(trivia.getCorrectAnswer());
answers.add(trivia.getIncorrectAnswers()[0]);
answers.add(trivia.getIncorrectAnswers()[1]);
answers.add(trivia.getIncorrectAnswers()[2]);
answers.sort(null);
return answers.indexOf(trivia.getCorrectAnswer());
}
}

View file

@ -0,0 +1,48 @@
/**
* When you need a way to store negative numbers
*/
public class SignedInteger {
int n;
boolean isNegative;
/**
* Create a new SignedInteger form an int (why you should do this? Why not.)
* @param n The i n t
*/
public SignedInteger(int n){
isNegative = n < 0;
this.n = Math.abs(n);
}
/**
* Get the value as an int
* @return The value
*/
int getValue(){
return isNegative ? -n : n;
}
/**
* Set the value. Saves memory. Because the programs this is used in are so huge
* @param n The new value
*/
void setValue(int n){
isNegative = n < 0;
this.n = Math.abs(n);
}
/**
* Add a new value. Can also be used to subtract something if the Parameter is negative.
* @param n The number to be added. Can be negative to subtract.
*/
void addValue(int n){
this.n += isNegative ? -n : n;
if(this.n < 0){
isNegative = !isNegative;
this.n = Math.abs(this.n);
}
}
}

View file

@ -0,0 +1,71 @@
/**
* Chars are quite useful for most applications, but sometimes, you need a lot of them
* Then variable naming can get confusing and char arrays are a bit too complicated to effectively work with
* That's when you should use StringOfChars, it stores a String (or Array) or chars and can be used very easily.
*/
public class StringOfChars {
private char[] string;
/**
* Create a new StringOfChars from lots of tiny cute chars (also called an 'array')
* @param letters
*/
public StringOfChars(char ... letters){
this.string = letters;
}
/**
* If you want to actually use the StringOfChars for anything (which you don't want to)
* @return The S T R I N G
*/
public String toString(){
return new String(string);
}
/**
* Get the char array itself
* @return The char array
*/
public char[] toCharArray(){
return string;
}
/**
* The length of the char array
* @return The length as an integer
*/
public int length(){
return string.length;
}
/**
* Get a single char at an index i
* @param i The index
* @return The Char
*/
public char atIndex(int i){
return string[i];
}
/**
* Append another char array
* @param chars The char array that should be appended
*/
public void append(char ... chars){
append(new StringOfChars(chars));
}
/**
* Append another StringOfChars
* @param string2 The StringOfChars
*/
public void append(StringOfChars string2){
char[] newString = new char[this.length() + string2.length()];
System.arraycopy(string, 0, newString, 0, this.length());
System.arraycopy(string2.toCharArray(), 0, newString, this.length(), string2.toString().length());
string = newString;
}
}

View file

@ -0,0 +1,24 @@
public class TestClass {
public static void main(String[] args) {
StringOfChars s = new StringOfChars('h', 'a');
s.append('l', 'l', 'o');
System.out.println(s.toString());
DecimalNumber dd = new DecimalNumber(2.4);
try{
DecimalNumber d = new DecimalNumber("hurensohn");
} catch (ValueShouldProbablyActuallyBeANumberAndNotSomeWeirdGibberishDumbProgrammerException e) {
e.printStackTrace();
}
Buddy bobby = new Buddy("bobby");
bobby.help();
bobby.joke();
GoodRandom random = new GoodRandom();
for (int i = 0; i < 10; i++) {
System.out.println(random.randomInt(20));
}
}
}

View file

@ -0,0 +1,11 @@
/**
* Sometimes I ask myself what is wrong with me but I always get a {@code ValueShouldProbablyActuallyBeANumberAndNotSomeWeirdGibberishDumbProgrammerException} instead of an answer
*/
public class ValueShouldProbablyActuallyBeANumberAndNotSomeWeirdGibberishDumbProgrammerException extends Exception{
@Override
public void printStackTrace() {
System.err.println("Holy fuck how can you be such a useless and dumb programmer wtf.");
System.err.println("Maybe don't fucking enter something that is not even a number. And in the end you're going to blame it on the end user. Pathetic.");
super.printStackTrace();
}
}