Sorting a structure randomly

biro's picture

Hi Guys! this is my first post. So forgive me if the question could seem stupid

I have a directory scanner that retrieve me the list of movies in a specific folder. I'd like to "randomize the order" of that list, so every time i trig the structure, i can scramble the order.

I've tried with javascript with something like this PHP Code: function (__structure out) main (__structure input){ if(input !=null){ var result=new Object(); result.out=input.sort(function(){return (Math.round(Math.random())-0.5)); return result; } }
but it doesen't work....and i don't know neither how to debug it...

Any solution???

Thanks

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

cybero's picture
Re: Sorting a structure randomly

This could easily be done with a multiplexer and a relatively short list of files.

Then again, you might have a lot of movies, who knows :-) ?

How's about running an LFO or if you want to go really quickly, a random number patch [Animation] to the indexing of that directory structure?

See attached.

PreviewAttachmentSize
portandlandrandom.zip120.75 KB

cybero's picture
Re: Sorting a structure randomly

dude - love that little avatar icon of yours - but you really need to sort out your Kineme link, which like many another I've found points to kineme's domain, because it has been incorrectly entered as http://kineme.net/users/bikthefellas.wordpress.com, when you mean to point to your actual wordpress setup [ which looks like it's still W.I.P, unless I'm much mistaken].

.lov.'s picture
Re: Sorting a structure randomly

... or using my little plugin for getting random numbers of indexes: http://code.google.com/p/cogerandom/downloads/list

I can build an example patch later, but at the moment i'm on a windows machine...

cybero's picture
Re: Sorting a structure randomly

yeah, good call :-)

cybero's picture
Re: Sorting a structure randomly

BTW, you could always set the timebase of the LFO or Random patch to use patch or system time, that would spice things up even more :-) .

cybero's picture
Re: Sorting a structure randomly

double post in error

biro's picture
Re: Sorting a structure randomly

sorry dude! ;) i made a mistake between personal URL an working URL ;) (i'm feeling like the newbbie that i'm ;)

just provided to correct the mistake

biro's picture
Re: Sorting a structure randomly

It's a great example of using the LFO for randomizing...but it's not what i need :(

i'd like just to sort randomly that list and then keep using it as a structure.

Something like the Structure sort object in QC...

fsk's picture
Re: Sorting a structure randomly

heres some javascript code that gives you a structure of randomly sorted indices.

var result = new Object();
function (__structure indices) main (__number reorder,__index count)
{
 
     tindices=[];
     for(i=0;i<count;i++)
     {
          tindices[i]=i;
     }
     result.indices=[];
 
     for(i=0;i<count;i++)
   {
      index=Math.floor(Math.random()*tindices.length);
      result.indices[i]=tindices[index];
      tindices.splice(index,1);
   }     
   return result;
}

the cogeRandom patch takes a similar approach but it will just return one index instead of a structure of them. it gives you a new one on each rising edge at the "new number" input port. it will return -1 when it uses up all the indices (then you need to reset it).

i wonder why something like this is not in the description of the patch ;).

edit:

var result = new Object();
function (__structure randomizedStructure) main (__number reorder,__structure orderedStructure)
{
if(!_testMode)
{
     tindices=[];
     for(i=0;i<orderedStructure.length;i++)
     {
          tindices[i]=i;
     }
     result.randomizedStructure=[];
 
     for(i=0;i<orderedStructure.length;i++)
   {
      index=Math.floor(Math.random()*tindices.length);
      result.randomizedStructure[i]=orderedStructure[index];
      tindices.splice(index,1);
   }     
   return result;
}
}

had the first code in some composition, but this should do exactly what you need.