shithub: util

ref: b57f2016149d82de9796a82826ed88f85e16a147
dir: /iching.c/

View raw version
#include <u.h>
#include <libc.h>

void
main() {
	int fd;
	int i, j;
	uchar bits[3];
	int lines[6];
	int linesproc[6];
	long value;
	uchar table[64] = { 2, 23, 8, 20, 16, 35, 45, 12,
						15, 52, 39, 53, 62, 56, 31, 33,
						7, 4, 29, 59, 40, 64, 47, 6,
						46, 18, 48, 57, 32, 50, 28, 44,
						24, 27, 3, 42, 51, 21, 17, 25,
						36, 22, 63, 37, 55, 30, 49, 13,
						19, 41, 60, 61, 54, 38, 58, 10,
						11, 26, 5, 9, 34, 14, 43, 1 };
	Rune first;
	Rune changed;

	fd = open("/dev/random", OREAD);
	if (fd < 0)
		sysfatal("open");

	value = 0;
	for (i = 0; i < 6; i++) {
		if (readn(fd, bits, 3) != 3)
			sysfatal("read");

		lines[i] = 0;
		for (j = 0; j < 3; j++)
			lines[i] += bits[j] & 1? 2: 3;
		print("%d ", lines[i]);
		linesproc[i] = lines[i] & 1;
		value += linesproc[i] == 1? 1 << (5 - i): 0;
	}
	print("\n");
	value = 0x4DC0 - 1 + table[value];
	first = value;

	value = 0;
	for (i = 0; i < 6; i++) {
		if ((lines[i] % 3) == 0)
			linesproc[i] = linesproc[i] == 1? 0: 1;
		value += linesproc[i] == 1? 1 << (5 - i): 0;
	}
	value = 0x4DC0 - 1 + table[value];
	changed = value;

	print("%C %C\n", first, changed);
}