Close

On Random Numbers and Power Consumption

A project log for Risk Battle Simulator

Love Risk, but hate constant dice rolling? With this device, there is no more inevitable dice falling off tables and knocking around armies.

scott-clandininScott Clandinin 07/08/2016 at 03:270 Comments

What is random?

I wondered to myself today, really just how good is the random() function from the arduino standard library? I looked it up, saw it was what I expected it to be called, and threw it right in. I made the mistake of not reading the whole page, however.

"If it is important for a sequence of values generated by random() to differ, on subsequent executions of a sketch, use randomSeed() to initialize the random number generator with a fairly random input, such as analogRead() on an unconnected pin."

Damn it.

Now that I pay attention, upon powering up, the results would be exactly the same each time if I only pressed the simulate button. I suppose this "random" function uses something like the program counter to generate the number, rather than a time-based counter. Since I would often play around with the army sizes at start up, I never noticed any pattern or flaw in the "randomization". Rookie mistake.

If I were wiser I would have prepared for a way to easily reprogram the device. I took a gamble that it would be perfect and I lost. Much like the random() function, the results were predetermined and I was destined to fail.

Power consumption considerations

Since this is a battery powered device, I wanted to make sure it wasn't hogging too much power to do such a simple thing. A good way to reduce power is to set up the microcontroller to use it's internal clock rather than an external crystal. Another is to reduce the clock speed. The following link is a nice solution to both of these problems with little work.

https://www.arduino.cc/en/Tutorial/ArduinoToBreadboard

This tutorial sets up the micro to use the internal oscillator at 8MHz. It is nice because it doesn't require looking at datasheets and spending times configuring things. That's why Arduino exists anyways right? Since low power consumption isn't a huge concern of mine, I'm fine with only dropping it to 8MHz. If power was a crucial consideration, I would drop the frequency way down to conserve power. The nice thing about this project is the simplicity and lack of ultra time-sensitive controlling.

Another perk to using the internal oscillator is of course not having to buy and solder in a crystal and capacitors! I'll definitely be doing it this way in all future projects.

Discussions