[solved] Shuffle an array.
hey everyone, i'm working on project complete. i'm adding little style it.
so have array like:
int list[] = {0,1,0,0,1,0,1,1,0};
so best way randomly shuffle positions
so have array like:
int list[] = {0,1,0,0,1,0,1,1,0};
so best way randomly shuffle positions
- - [8]?
thanks time,
dave.
i won't claim know best way, sort of "bubble unsort" algorithm came mind:
remember seed randomizer, maybe time or unused analog input.
btw, if values using 1 , 0, little more memory efficient use int , bit manipulation instead (since 9 bits, not
, works too.
code: [select]
int list[] = {0,1,0,0,1,0,1,1,0}
for (int a=0; a<9; a++)
{
r = random(a,8) // dont remember syntax now, random 8 included.
int temp = list[a];
list[a] = list[r];
list[r] = temp;
}
remember seed randomizer, maybe time or unused analog input.
btw, if values using 1 , 0, little more memory efficient use int , bit manipulation instead (since 9 bits, not
, works too.
Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > [solved] Shuffle an array.
arduino
Comments
Post a Comment