Skip to content

Instantly share code, notes, and snippets.

View alwynallan's full-sized avatar

Peter Allan alwynallan

  • Greater Philadelphia, Pennsylvaia
View GitHub Profile
@alwynallan
alwynallan / bin2svg.c
Created January 4, 2025 20:13
Takes binary data in 1-byte to 8-byte words and produces a .SVG image of 100K lines where the position on the circumference is the value of each word. Most graphics software balks and the extreme number of transparent objects, but Chrome and Illustrator can handle it, and Inkscape tries.
// bin2svg.c
// $ gcc -Wall -O3 bin2svg.c -o bin2svg -lm
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <math.h>
@alwynallan
alwynallan / Collatz-Weyl.c
Created January 21, 2024 23:06
PRNG with seeding and output - see link in code
// https://www.reddit.com/r/RNG/comments/19a2q24/collatzweyl_generators/?utm_source=share&utm_medium=web2x&context=3
// $ gcc -Wall -O3 Collatz-Weyl.c -o Collatz-Weyl
// $ ./Collatz-Weyl | pv > /dev/null <-- 650MiB/s on 2018 Zeon VM
// compared with 400MiB/s for ChaCha20
#include <stdio.h>
#include <assert.h>
static __uint128_t c[4] = {0, 0, 0, 0}; // c[0] must be odd
/*
https://math.stackexchange.com/a/3306
A simple (practical, low-computation) approach to choosing among three options with
equal probability exploits the fact that in a run of independent flips of an unbiased
coin, the chance of encountering THT before TTH occurs is 1/3. So:
Flip a coin repeatedly, keeping track of the last three outcomes. (Save time, if you
like, by assuming the first flip was T and proceeding from there.)
@alwynallan
alwynallan / pmg2.c
Created August 31, 2021 07:59
Solution per tstanisl at https://stackoverflow.com/a/68959383/5660198 and pmg's 2nd comment
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int compare (const void * a, const void * b)
{
if (*(double*)a > *(double*)b) return 1;
else if (*(double*)a < *(double*)b) return -1;
else return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int compare (const void * a, const void * b)
{
if (*(double*)a > *(double*)b) return 1;
else if (*(double*)a < *(double*)b) return -1;
else return 0;
}
@alwynallan
alwynallan / not_solution.c
Last active August 29, 2021 12:29
Bad attempt at solution per tstanisl at https://stackoverflow.com/a/68959383/5660198
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int compare (const void * a, const void * b)
{
if (*(double*)a > *(double*)b) return 1;
else if (*(double*)a < *(double*)b) return -1;
else return 0;
}
@alwynallan
alwynallan / thirds.c
Created February 23, 2021 21:51
Tests the 64-bit hardware efficient generation of random 1-in-3 choices.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
// [email protected] February 2021
// tests the 64-bit hardware efficient generation of 1-in-3 choices.
// uses the fact that 2^65 is just ~1% larger than 3^41
// whereas 2^2 is 25% larger than 3^1
@alwynallan
alwynallan / Makefile
Last active February 22, 2025 16:37
Hardware PWM Controller for the Raspberry Pi 4 Case Fan
CC = gcc
RM = rm -f
INSTRUMENT_FOR_PROMETHEUS := false
ifeq ($(INSTRUMENT_FOR_PROMETHEUS),true)
CFLAGS = -Wall -DINSTRUMENT_FOR_PROMETHEUS
LIBS = -lbcm2835 -lprom -lpromhttp -lmicrohttpd
else
CFLAGS = -Wall
/*
Deprecated, see https://gist.github.com/alwynallan/1c13096c4cd675f38405702e89e0c536
If you have to use software PWM, it's still here.
*/
@alwynallan
alwynallan / tz_data.json
Created January 17, 2016 23:00
Worldwide timezone data for iotclock.js
{
"servers":[
{"name":"pool.ntp.org"},
{"name":"asia.pool.ntp.org"},
{"name":"europe.pool.ntp.org"},
{"name":"north-america.pool.ntp.org"},
{"name":"oceania.pool.ntp.org"},
{"name":"south-america.pool.ntp.org"}
],
"regions":[