So, i was pumping some random colours into a string at random locations, but the effect is un-pleasing, because its random. So pixels that were lit, get updated several times, while some remain unlit for quite some time.
so i want to test if the pixel was lit, and favour unlit pixels
which is better?
oldcolor = leds[i];
if (oldcolor != black)
or
if (leds[i])
Cheers
Unless this is in an inner loop, I’d go for whichever is easier to read and maintain.
If it is in an inner loop, benchmark it for a few seconds and see if there’s a practical difference; I bet there isn’t really.
My personally, I’d write
if( leds[i] ) …
but that’s partly because I’m a longtime C coder and that reads naturally to me in my head. It’s probably a tiny bit smaller and a tiny bit faster, but probably not enough to outweigh your personal preferences in terms of style and legibility.
Which do you like the look of, personally?
Hi Stuart - I use a list of non-repeating random numbers for this:
int randomTable[NUM_LEDS];
// create non-repeating randomised table
// Start off ordered, randomTable[0]=0, randomTable[1]=1… etc…
for (int range = 0; range < NUM_LEDS; range++) {
randomTable[range] = range;
}
// Random shuffle, so, randomTable[0]=38, randomTable[1]=13… etc…
for (int shuffle = 0; shuffle < NUM_LEDS - 1; shuffle++) {
int myrand = shuffle + random(NUM_LEDS - shuffle);
int save = randomTable[shuffle];
randomTable[shuffle] = randomTable[myrand];
randomTable[myrand] = save;
}
Then instead of using ‘i’ for your LED in your loop using the table:
for (int i = 0; i < NUM_LEDS; i++) {
leds[randomTable[i]] = //whatever
}
Mark would probably be able to re-code that into a single line of code 
Many thanks @Mark_Ortiz , I may use that, @Mark_Kriegsman o wasn’t sure if
if (led[I])
Was going to be possible, but while I read it as meaning the I’th led contains light (therefore its not black) I see lots of people using the old
color != black
Method.
Many thanks!
Also worth noting that random8() will return a ‘random number’ from 0…255 much much faster than Arduino standard library random(). Use random16() if you need larger numbers 0…65535.
You can also use
j = random8( 10 ); // 0…9
or
j = random8( 10, 20); // 10…19
I often use
j = random16( NUM_LEDS );
when I need to choose a random pixel quickly.
The sequence does repeat, but the repeat period is pretty long (65535 calls). To add entropy, I put this at the top of my loop() function:
random16_add_entropy( random() );
This adds entropy from a higher-bit-width entropy pool to the FastLED fast PRNG pool, ensuring virtually undetectable levels of repeat/patterns.
See longer discussion at github issue #82, and related post here https://plus.google.com/112916219338292742137/posts/184Qd9fHK6g
Ah, you know I keep forgetting that FastLED contains functions like that. I may have to create a cheat sheet.
Thanks @Mark_Kriegsman , I’m gonna gist that example.
Just a quick note about my code. It’s artificial - but ‘looks’ more random in a pleasing way. A bulb is never reused until the all the rest have been. Its great for strobe effects.
Yep. There are definitely times when a shuffled deck is what you want.