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 / 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>
// alwynallan@gmail.com 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 June 21, 2024 18:53
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":[
@alwynallan
alwynallan / iotclock.js
Last active January 17, 2016 23:52
Javascript for IoT Clock Settings page. Page was too big for ESP8622 when it was included directly. Oops, can's serve .js out of a gist.
var xmlhttp = new XMLHttpRequest();
var url = "http://apa.hopto.org/tz_data.json"; // do we need the domain??
var tzArr;
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
tzArr = JSON.parse(xmlhttp.responseText);
populateOptions(tzArr);
}
};