Python: Using XOFs for general-purpose Random

As always, one's own stack overflow answers make the best blog posts. In this case, we craft a version of random.Random with a few modifications: Pulls its data from an arbitrary stream (in our case, a DRBG such as a hash function or deterministic CSPRNG) Is slightly more efficient at generating random integers Has fixed code for .shuffle, on the offchance CPython ever changes theirs, and to make it work …

Python: Rounding fractions half-up

By default, Python's fractions.Fraction rounds halves to the nearest even number. If you, instead, want to round a fraction but send halves up, here's how that's done: from math import floor from fractions import Fraction def roundhalfup(x: Fraction) -> int: """ Rounds x to the nearest integer, with ties being rounded towards positive infinity """ return floor(x + Fraction(1, 2)) def divroundhalfup(a: int, b: int) -> int: """ Returns the …

My WordPress custom CSS

Posted for administrative use as well as archive/reference /* Customizations */ .site-title a { text-decoration: none; } .entry-content aside { font-style: italic; display: inline; } .entry-content .language-python, .entry-content .language-javascript { tab-size: 4; -moz-tab-size: 4; } .entry-content .language-sql, .entry-content .language-css { tab-size: 2; -moz-tab-size: 2; } .entry-content.category-transcript a { color: revert; } body.page-id-1388 { background: url('https://timecube.2enp.com/grid.gif'); } body.page-id-1388 #canvas, body.page-id-1388 #page{ background: revert; } /* undo weird Suki design choices …

Javascript: Parsing query parameters

There are at least 2×5 things you could mean by "parsing" a query string in Javascript What data type do you want returned? Map Object How do you want duplicate keys handled? Just the first item Just the last item All the items, as a Set All the items, as an Array Throw an error As a quick exercise, here is a sampler of all those (if you're in …