Skip to content

Instantly share code, notes, and snippets.

@mjtb49
mjtb49 / lazy_iterate_for_ruined_portals.py
Last active February 28, 2025 22:22
Illustrating how to iterate over seeds giving a particular ruined portal without having to think about lattices. To do this in the fastest way takes lattices, but a naive iteration is perfectly possible and not much slower, and this gist is to iterate that point.
# lcg constants. Replace A and B to change indices of targeted call.
A = 25214903917
B = 11
M = 1 << 48
mask = M - 1
# Can mess with this
argument_to_next_int = 28 # must not be a perfect power of 2
known_lower_bits = 17 # must be >= 17
lower_bits = 0 # must be the lower bits of some valid seed
import itertools
import random
import time
A = 0x5deece66d
B = 11
LCG_MOD_POW = 48
M = 2**LCG_MOD_POW
BIG_MASK = M - 1
import random
import itertools as iter
KING_SQUARE = (0, 0)
KING_THREATENS = {(-1, -1), (-1, 0), (-1, 1),
(0, 1), (0, -1),
(1, -1), (1, 0), (1, 1)}
MATING_SQUARES = {(-1, -1), (-1, 0), (-1, 1),
(0, 1), (0, 0), (0, -1),
(1, -1), (1, 0), (1, 1)}
@mjtb49
mjtb49 / lcg_log.py
Last active February 17, 2025 05:30
Earthcomputer requested that I write up a lcg discrete log solver for arbitrary lcgs. I have now done so, but lazily, there are several points where this could be improved and I am not entirely convinced by the approach. This solver assumes some sort of factorization is possible - in particular it needs to factor both phi(m) and m at some point …
import math
import sympy
import sympy as sp
import random
class LCG:
def __init__(self, a, b, m):
self.a = a % m
@mjtb49
mjtb49 / LootTableRNG.java
Last active November 13, 2024 19:05
1.20-pre4 Loot Example
import com.google.common.base.Charsets;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;
import com.google.common.primitives.Longs;
import java.util.Random;
public class LootTableRNG {
private static final HashFunction MD5 = Hashing.md5();
@mjtb49
mjtb49 / main.py
Created June 7, 2022 04:08
Factoring Polynomials Mod 2
import random
class GF2Poly:
def __init__(self, c):
self.c = c
def __add__(self, other):
return GF2Poly(other.c ^ self.c)
import java.util.Random;
import java.util.UUID;
public class UUIDBonker {
public static UUID randomUuid(Random random) {
//fabric mappings seem to call m and l the other way round. This disagrees with UUID constructor.
long m = random.nextLong() & -61441L | 16384L;
long l = random.nextLong() & 4611686018427387903L | Long.MIN_VALUE;
return new UUID(m,l);
import java.util.*;
public class AdditionUnderXOR {
static final int SS = 1000000;
enum Ore {
COAL(60005),
IRON(60006),
import kaptainwutax.biomeutils.source.NetherBiomeSource;
import kaptainwutax.featureutils.structure.NetherFossil;
import kaptainwutax.seedutils.mc.MCVersion;
import java.util.Random;
public class NetherFossils {
static final NetherFossil FOSSIL = new NetherFossil(MCVersion.v1_16_1);
public static void printSomeFossilSeeds(int some) {
Random r = new Random();
/**
* An example to show basic functionality of KaptainWutax's libraries
* and common usage patterns.
*/
import kaptainwutax.biomeutils.source.OverworldBiomeSource;
import kaptainwutax.featureutils.structure.Village;
import kaptainwutax.seedutils.mc.ChunkRand;
import kaptainwutax.seedutils.mc.MCVersion;
import kaptainwutax.seedutils.mc.pos.CPos;