Week 9 - Input Devices
Making a Random Number GeneratorA few weeks ago we've had a discussion in class about the pseudo-random generator used for the people page. I decided to take a different approach to solve that issue.
Since signals, specifically electrical, are inherently noisy, it's quite common to use the noise as a source of entropy for generating random numbers. I decided to use this week's circuit to measure a sensor and use its noise as random numbers. I then edited the class's page (locally) so that it uses my circuit-enabled RNG, rather than the software one.
You can find the specifics below, but first, here's how it works:
As to the process, I decided to start with the reference phototransistor based board. It's worth mentioning that mostly any sensor can be used - they are all quite noisy :).
I milled the board as usual:
And then stuffed it:
And... it didn't work!
As I tried to play with the FTDI plug, I accidently broke the board. That was quite frustrating, so again - I milled the board and stuffed it. Surprise surprise, it still didn't work!
I had long suspected that my programmer isn't really working. At this point, I decided to borrow a different one - and finally I saw some progress - I was able to program the board, but not to use it. At least at this point I knew that my programmer is dead.
I had milled and stuffed yet another board (#3), which FINALLY worked!
Now for the interesting part - turning Neil's program into a RNG and using it in the class website. This involved three parts:
First, editing Neil's program - stripping down the Tk GUI, and adding the code to extract the random number. The module is quite simple, it essentially goes through a few samples (since there's filtering involved in the original code, so taking the first sample didn't work well), and then it does the following:
int(sensor_reading - floor(sensor_reading) * 100) % n
Essentially, what this does is take the least significant two digits modulus the number of people in the class (n). Note that if larger numbers are required, it's easy to to repeat this process for every digit (or bit).
Second, I created a small python HTTP server with websockets support using Tornado. This part communicates with the serial module and the static client-side webpage.
Third, I took the class people's page, and I added websocket support for it (the client part). I then edited the random selection function to communicate with the server(and as a result - with the board). This works as seen above.
For those who are interested, you can find the code in Github, or here: