Skip to content

Instantly share code, notes, and snippets.

View loreb's full-sized avatar

Lorenzo Beretta loreb

View GitHub Profile
@loreb
loreb / devurandom.vim
Created November 22, 2012 23:22
vim RNG (/dev/urandom)
" I like picking a random colorscheme, but I hate the way it's done in most vim scripts
" -- localtime(), system("echo $RANDOM"), etc etc.
" And I wanted to try this gist thing...
function! DevUrandom()
let rv = 0
" readline reads 4096 bytes at a time(think fread())
" and returns a *list* of lines.
" Don't only read one line because:
" - we read more anyway, and
" - a short line means 0 has 1/256 probability rather than 1/4G!
@loreb
loreb / djbRANDOM.vim
Created February 14, 2013 17:30
The vimscript translation of a small rng published by Daniel J. Bernstein.
" https://groups.google.com/forum/?hl=en&fromgroups=#!topic/sci.physics/SWhCr8SEVWw
" TLDR: a PRNG by Dan Bernstein ported to vimscript
" The generator shown below is faster than Marsaglia's on my Pentium-100.
" It passes the Diehard tests without trouble.
"
" Use RANDOM for one 32-bit random number. Do not use RANDOM twice in one
" line. You can put a 64-bit seed into random_in[2 and 3]. The effect of
" seed m is to jump ahead by 2^68 m in a single sequence of period 2^132.
" It's easy to determine the seed from the sequence, but not with the sort
@loreb
loreb / marsaglia.sh
Last active December 14, 2015 10:29
George Marsaglia's favorite PRNG in vimscript and POSIX shell - fast, good, easy to use, remarkably short for what it does. The shell version breaks under mksh, but I'm too lazy to fix it now^H^H^H^H fixed.
#! /bin/sh
# Marsaglia's favorite PRNG, see his 20/01/99 post on sci.stat.math at
# https://groups.google.com/forum/?fromgroups=#!topic/sci.crypt/yoaCpGWKEk0
# https://en.wikipedia.org/wiki/Random_number_generation#Computational_methods
# = API =
# marsaglia_seed [w] [z] / both non-zero
# marsaglia() returns a random uint32 in $RAND
# -- can't use $RANDOM, setting it means seeding the PRNG in bash/ksh/zsh
@loreb
loreb / ISAAC_readable.vim
Created March 13, 2013 16:34
Bob Jenkins' ISAAC implementation converted to Vimscript (http://www.burtleburtle.net/bob/rand/isaacafa.html). At least three orders of magnitude slower than the C version.
" http://www.burtleburtle.net/bob/c/readable.c
" ----------------------------------------------------------------------------
" readable.c: My random number generator, ISAAC.
" (c) Bob Jenkins, March 1996, Public Domain
" You may use this code in any way you wish, and it is free. No warrantee.
" * May 2008 -- made it not depend on standard.h
" ----------------------------------------------------------------------------
" /* a ub4 is an unsigned 4-byte quantity */
" typedef uint32_t ub4
@loreb
loreb / suoneria
Created August 29, 2013 22:31
A little script for my mom's new cell -- explaining just didn't work :) "suoneria" is the italian word for "ringtone"
#! /bin/sh
# Original msgs edited or stripped (they were in italian);
# iirc knoppix localizes sell scripts, I don't care.
zdie() {
zenity --error --text="$*" --no-markup
exit 123
}
@loreb
loreb / bug.1st
Created September 15, 2013 21:09
Captain's log, Star Date 91312.3 -- this github thing apparently has bug fixing facilities...
https://github.com/cjdelisle/cjdns/issues/341
1st time I've reported a bug here on github rather than using email or reportbug
-- that shows I should use github a little bit^W^Wlot more...
@loreb
loreb / arc4.sh
Created September 22, 2013 16:40
OpenBSD's arc4.c rewritten in striictly POSIX /bin/sh, mostly to survive some **loud** TV show (I'll go to hell for this :) TODO wtf is up with zsh?
#! /bin/sh
# Ispirato dal papa in Sardegna XD
# -- per la cronaca, ha finito prima lui, ma di poco.
#/* $OpenBSD: arc4.c,v 1.3 2007/09/11 12:07:05 djm Exp $ */
#/*
# * Copyright (c) 2003 Markus Friedl <markus@openbsd.org>
# *
# * Permission to use, copy, modify, and distribute this software for any
# * purpose with or without fee is hereby granted, provided that the above
# * copyright notice and this permission notice appear in all copies.
@loreb
loreb / getpeerid()
Created September 22, 2013 16:46
Something I had never thought about, quoted from http://micro.nicholaswilson.me.uk/post/33359696556/cute-little-thought-its-obvious-why-there-arent [foreshortened, Nicholas Wilson] Methinks there should be an errno value for this...
Cute little thought: It’s obvious why there aren’t APIs for finding out, for example, the current credentials of the process on the other end of a UNIX-domain socket (there might be several processes on the end!) Less obviously, there could be no process on the other end: suppose you have a socket at your end, and the remote end is dup’ed into two related processes. You pass in an fd (with an SCM_RIGHTS message), then close it. It’s not open in your process, and it can’t be associated with either receiving process (either one of them could read it first). So, who ever’s on the other end of the passed fd is in the unusal situation of having a valid, connected socket with absolutely no process on the other end! Quirky.
@loreb
loreb / dmc_uintmax_max.c
Created October 28, 2013 17:26
If this gets accepted it will be my first time reporting a compiler bug XD (edit: and **if** it can be submitted without a bugzilla account or whatever)
/* Digital Mars bug */
/* From stdint.h:
#define UINTMAX_MAX 18446744073709551615
... except that "number is not representable":
the compiler rejects integer literals bigger than ULONG_MAX,
which is 32 bits on windows XD
*/
#include <stdint.h>
#include <limits.h>
@loreb
loreb / sane_gnu_info
Created January 18, 2014 14:37
A better reader for GNU info pages that went straight into my ~/bin/, courtesy of robot_t on https://news.ycombinator.com/item?id=7069889 (ps support OpenBSD!)
#! /bin/sh
# This is my ~/bin/info
exec /usr/bin/info "$@" | exec less # linux
exec /usr/local/bin/info "$@" | exec less # *BSD
# References:
# https://news.ycombinator.com/item?id=7072844
# https://news.ycombinator.com/item?id=7075802
# THANK YOU robot_t on ycombinator!!!