[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
  • - [8]?

    thanks time,
    dave.

i won't claim know best way, sort of "bubble unsort" algorithm came mind:

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 8), works too.


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > [solved] Shuffle an array.


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums