Showing posts with label random. Show all posts
Showing posts with label random. Show all posts

17 June, 2017

Galois LFSR PRNG in NeoPixel goggles

I've been playing with software to run on my Adafruit Neopixel Goggles. The software gets to make pretty patterns on 32 x 24-bit RGB LEDs arranged in two rings.

One of the modes picks random colours to change each ring to. The Arduino stack supplies a random() function to do this, but it seemed to take up about 600 bytes of the limited 5kb flash program memory on the in-goggles microcontroller.

I wondered if I could make a smaller-but-less-random generator. After all, the colour pattern does not need to be cryptographically secure. I'm just trying to avoid getting the same sequence of colours every time.

Someone pointed me at Linear Feedback Shift Register PRNGs and I implemented one based around a description in Wikipedia. I chose the Galois one because pistols at dawn.

That seemed to work well for the basic generation of random numbers, but the Arduino always started up with the same seed, and so gave the same sequence of colours each time. Luckily, there are 512 bytes of EEPROM on this microcontroller and so I used two other those to generate and store a seed to be used next time.

Initially, I generated two random bytes at power-on, and stored those into the EEPROM. However, this rapidly proved to have a really short period: there were only two different patterns being displayed by the goggles in this mode!

So, next, which seems to work better, the code now initialises the PRNG from EEPROM, takes a single bit from it (and discards it) and then writes out the PRNG state into the EEPROM. That means that the start state advances by one bit every boot.

Code for the goggles is here on github: https://github.com/benclifford/goggles.

13 February, 2011

choosing a password

many sites want a password. for a lot of middle-to-low security accounts, I keep a(n encrypted) database of passwords on my computer, rather than making them memorable or using the same one on all. So I cut and paste each password and don't care about it being easily typable. To generate the passwords, I use a command-line like this:

$ cat /dev/random | strings -n 16
:*jx4%8er:>kRKh:
a#ka;lPB6rB9SX";lk
6B!'X@Q{@QQ LZB?
hZ if=A2u3;-S]v?P
Ix6RwEwqVqEg~0fFi
[hkE*0T~GZX^5=h<4

19 June, 2010

normally distributed random numbers in haskell

somehow I expected to be able to get a normally distributed random number in haskell really easily by typing some term like "haskell random normal" into google. that came up with a bunch of stuff but nothing so simple as:
n <- getNormal mean dev
.

The most promising seemed to be the cabal package random-fu - although it looks like it has a lot of stuff in it and really all I want is the above single monadic action.

Cue the usual BS about installing packages (not haskell specific, just damned packaging in general) - today I need to edit my local hackage repository to remove some malformed packages, and upgrade GHC. frr. 6 hours just getting cabal install random-fu to work.

But once that was all done, the next morning, I got what I wanted - a short IO Double action: sampleFrom DevURandom (normal mean dev)

Hurrah.