Random() - How to start at different points for multiple instances

Hi all,

if i understand correctly, Random() is not really random at all but rather an algorithum for creating values that cover a given spectrum, which is the same row of values every time. This is ok for my purposes, but I would like to avoid two instances of Random() mirroring the output (at least at the same time). For example, I have two LED candles in which I am using Random() to create the flicker effect, it is ok if the flicker pattern is the same but it should be offset so that they do not flicker the same at the same time. I did this with a delay() between candles and that is ok for two or three instances, but if I add more candles, the delay from the first until the last candle starts is too noticable. My Question: Is there a way to tell Random() to start at different places in the algorithum process?

to do something like (pseudocode):

Candle 1 flickers at random Candle 2 flickers at random +50 Candle 3 flickers at random +100

Ok thanks alot for any advice,

Jason

Then, either store a different “true” random number in EEPROM# or reboot each candle a different number of times.

# As an added bonus, if you initialize the value stored in EEPROM to a random number, the patterns will no longer repeat and no two cables will be the same. random.org is a good source for “true” random numbers.

You can reseed the generator using randomSeed(). Call it with analogRead() to a disconnected analogue pin, before each call (or every n calls) to random(), to give a fairly random sequence.

http://arduino.cc/en/Reference/RandomSeed

Call it with analogRead() to a disconnected analogue pin

That is not reliable.

How reliable does it need to be for flickering candles in a not too obvious way? Multiple real candles flicker in discernably similar patterns when placed in the same air space.

In some circumstances, even on an unconnected pin, analogRead always returns the same value. Which is worse than what @opal_1970 has now.

opal_1970:
My Question: Is there a way to tell Random() to start at different places in the algorithum process?

If you call random() in sequence for each candle, surely they will all be different? Can you post your code?

Both solutions sound as though they might give me what I am looking for, I will try both out and see how they look. One thing I did not mention in my first post is that I eventually want to export the code to an ATtiny chip or the like. I will have to see how compatible that will be.

Thanks for the replies, I will let you know how it goes.

Jason

this is the basics of what I am working with at the moment…

void flicker()
{ 
Sets a random value of LED voltage in the range of 0V - 5V with PWM 
analogWrite(ledPin1, random(100,255)); 

        //raising the Min range makes a good flicker affect, otherwise 0, 255 is min/max

analogWrite(ledPin2, random(100,255));
       //analogWrite(ledPin3, random(100,255));
       //analogWrite(ledPin4, random(100,255));
       //  ...
delay(random(10,40)); //Limits the speed.
}

Yes, but you are limiting the speed for all 3 at the same place. There’s the problem.

Get rid of delay ().

This code gives a reasonable flickering effect on pins 3, 5, 6:

byte candles [] = { 3, 5, 6 };

const int FLICKER_INTERVAL = 400;

void setup ()
{
  for (byte i = 0; i < sizeof candles; i++)
    pinMode (candles [i], OUTPUT);
 
   pinMode (0, OUTPUT);   // stop the Rx/Tx lines lighting up
   pinMode (1, OUTPUT);
}  // end of setup

unsigned long flickerTime [sizeof candles];
unsigned long startTime [sizeof candles];

void loop ()
{
  for (byte i = 0; i < sizeof candles; i++)
    {
    if (millis () - startTime [i] >=  flickerTime [i])
      {
      flickerTime [i] = random (FLICKER_INTERVAL);
      startTime [i] = millis ();
      analogWrite (candles [i], random(0,255));
      } // end of if time up 
    } // end of for
}  // end of loop

You can tweak the settings a bit, but a least they come on and off at different times.

Hi Nick,
the delay is only the time between the steps to the next brightness level, getting rid of the delay will only make them flicker the same at a faster pace… and as mentioned above, putting a delay between each of the instances does offset the flicker effect, providing the wanted effect… but this only works with a couple LEDs. after about 4 “delayed in step” LEDs the last LED is noticably off when the first one starts its sequence, and I will be flickering about 30 LEDs.

I haven’t looked at the example in your other post yet, but I will check it out and see if I can tweak it to fit. Thanks.
Jason