Ooh... Just thought of a problem... Is this bad?

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
magician13134
 
Posts: 1119
Joined: Wed Jun 13, 2007 9:17 am

Ooh... Just thought of a problem... Is this bad?

Post by magician13134 »

In a program I'm writing, I save a number to EEPROM later to be used for seeding random, but it wasn't very random, so I just stuck that line of code into the 'delayms' function to be called several... thousand? million? times every time I use this device... Is that a bad thing? Do I only get a certain number of EEPROM writes before it fails? I found this
During rewrites, the gate oxide in the floating-gate transistors gradually accumulates trapped electrons. The electric field of the trapped electrons adds to the electrons in the floating gate, lowering the window between threshold voltages for zeros vs ones. After sufficient number of rewrite cycles, the difference becomes too small to be recognizable, the cell is stuck in programmed state, and endurance failure occurs. The manufacturers usually specify minimal number of rewrites being 10^6 or more.
On Wikipedia... So it look like it is a problem, but I wasn't quite sure if that was something I should worry about. Thanks

User avatar
opossum
 
Posts: 636
Joined: Fri Oct 26, 2007 12:42 am

Re: Ooh... Just thought of a problem... Is this bad?

Post by opossum »

magician13134 wrote:Do I only get a certain number of EEPROM writes before it fails?
Yes. It will eventually fail. Usually well beyond the max cycles in the spec sheet, but it will fail.

One way to get a random seed or random number is to time how long a button is pressed.

Assume a pushbutton to ground on bit 0 of port A...

Code: Select all

uint_8t random;
// Wait for button to be pressed
while(PINA&1);
// Increment random while button is held down
while((PINA&1)==0) ++random;

magician13134
 
Posts: 1119
Joined: Wed Jun 13, 2007 9:17 am

Post by magician13134 »

But will that remember a new seed once it's powered down? Or will it start over in the same pattern until the button is pressed?

User avatar
opossum
 
Posts: 636
Joined: Fri Oct 26, 2007 12:42 am

Post by opossum »

It would have to be done at each power up.

"Push button to begin"

magician13134
 
Posts: 1119
Joined: Wed Jun 13, 2007 9:17 am

Post by magician13134 »

Ah, I see. Well I combined the two. It writes a random number to EEPROM every time the button is pressed, then seeds with that. This seems to work well, and only writes to EEPROM 10 or less times per use.

octelcogopod
 
Posts: 192
Joined: Tue Jan 30, 2007 5:14 am

Post by octelcogopod »

3 other ways:

-Zener diode
-Resistor
-Plain cheapo silicon diode/transistor
-TSL237 (would have to be exposed to the world, not in a case)

I'd say to get the crappiest, cheapest, noisiest silicon you can find. Overdrive it and put it through a couple of op-amps and use as little shielding as possible. Noise!

You can try an even simpler method, but it's not guaranteed to be random. However, this guy seems to have success with it:
The Random Number Generator
At the heart of the device is the random number generation. This is our interface to the “unknown”. If the spirits can talk to us, this is how they’ll be doing it.
My first attempt at random number generation was to use 3 ADC channels. The idea was to let them float with long wires attached to them so the surrounding fields were directly affecting the number generated. I used the lowest 3 bits of each ADC channel (each channel is 10 bits) and combined them into a 9-bit number for a range of 0 to 511. The ADC window is 0V to 3.3V, which makes the lowest 3 bits sensitive to about 26mV of ambient noise. I also sequentially rotated the position of each 3-bit field within the 9-bit number to make sure that no one reading could dominate the other two.
To test the generator I set up a special piece of code that would just run and keep track of how many times each number from 0 to 511 was picked. When I pushed the stop button after a while, it would dump the result to a terminal. Have a look at the result below.

That doesn’t look very random, does it? It’s worth reminding the reader at this point that there’s another factor in the randomness that should be considered, that being the timing of the user’s hand in turning the device upside-down and right-side up. This test didn’t take that into consideration. But the plot shows that for any given time the result is more likely to be in a few obvious places. We can’t have that, so on to take two.
The solution ended up being remarkably simple. In fact, I’m almost embarrassed to suggest it. But hey, we’re all friends here. I kept the first setup, but now each subsequent random number is created from the last number plus the new one. If the old number and the new number add up to be greater than 511, I subtract 512 to get it back in the window. The result? Have a look.

That’s what we in the biz call noise. And that will do just fine. With a random field of 512 choices, we can pack our SD card with up to 512 custom messages. Oh, the possibilities.
(the images are missing, sorry)

Locked
Please be positive and constructive with your questions and comments.

Return to “Microcontrollers”