Reply to comment

user warning: Unknown column 'i18n.language' in 'where clause' query: SELECT DISTINCT b.* FROM blocks b LEFT JOIN blocks_roles r ON b.module = r.module AND b.delta = r.delta LEFT JOIN i18n_blocks i18n ON (b.module = i18n.module AND b.delta = i18n.delta) WHERE (i18n.language ='en' OR i18n.language ='' OR i18n.language IS NULL) AND ( b.theme = 'garland' AND b.status = 1 AND (r.rid IN (1) OR r.rid IS NULL) )ORDER BY b.region, b.weight, b.module in /var/alternc/html/o/orangeseeds/usr/drupal-6/modules/block/block.module on line 456.

Random random seed

The random() function of the AVR is pretty useful, however, since it is based on a seed, it will always deliver the same random numbers if the seed is the same. On a computer, one generally overcomes this problem by setting the seed using a pseudo-random process such as the computer clock.

Since there is no internal clock on an AVR, a good solution is to use an open analog pin. The following code does exactly this, but instead of just calling

randomSeed( analogRead(pin) );

it calls analogRead() four times to fill up all of the 32 bits of the seed. Here it is:

  /// Initializes the random number generator 
  /// by using analogRead on an open pin.
  void seed(byte pin) {
    unsigned long seed = 0;
    for (int s=0; s<32; s+=10) {
      unsigned long val = 0;
      do {
        val = analogRead(pin);
      } while (val == 0); // discard zeros
      seed |= (val << s);
    }
    randomSeed( seed );
  }

Reply

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.