# Random number sequence

Hoi,

I made sort of a slideshow with a folder of picts. Now there playing in order. But i want them to play random from lets say 1 to 10 but showing every pict only once. than starting over and show all 10 in a different random sequence.

I can't seem to ficker it out. Can somebody help me? It can't be that hard?

Thx

Tijn

## Comment viewing options

### Re: Random number sequence

/me dusts off the old math book

What you're wanting is a "permutation", not exactly a "random sequence" -- a random sequence can reuse items, while a permutation is just a reordering.

to do this, you'll want to generate a list of indexes (0, 1, 2, 3, 4, 5, ..., n), and then Swap random indices. This is quite simple with javascript:

```function (__structure order) main (__index count)
{
var result = new Object();
result.order = new Array;
// generate in-order indexes
for(i=0;i<count;++i)
result.order[i] = i;
// randomize by swapping lots of times
for(i=0;i<count*5;++i)
{

var src = Math.floor(Math.random()*count);
var dst = Math.floor(Math.random()*count);
var temp = result.order[dst];
result.order[dst] = result.order[src];
result.order[src] = temp;
}
return result;
}```

Hope that helps

### Re: Random number sequence

I need this thing in a lots of situation, so i made a plugin: http://code.google.com/p/cogerandom/

a|x

### Re: Random number sequence

isn't that unstable (as in, depending on the sort algorithm it could plausibly never complete)? not at all likely to ever happen, but the runtime isn't necessarily guaranteed.

### Re: Random number sequence

The plugin is just what i needed.... Sory but java script isn't realy my thing. Can't seem to get my head around text programming.

Thanks again

Tijn

### Re: Random number sequence

this plugin has a memory leak; see attached composition (running with VBLSync disabled helps make it apparent more quickly)

you should release workarray in disableExecution, not stopExecution. (not sure how to commit to google code)

PreviewAttachmentSize
cogerandom leak.qtz2.74 KB

### Re: Random number sequence

Ahh, i fixed it, thanks for your help!

### Re: Random number sequence

Another Javascript way to do it — for when you decide you'd like to learn JS. QC is so much more interesting with a bit of JS.

```function (__structure Order) main (__index Pics)
{
var result = new Object();
var _Order = new Array();

//declare aN array AND fill it with index numbers
var _List = new Array();
var me = new Array();
//   var another = new Array();
for (i=0; i < Pics; i++) {
_List [i] = i }
me = _List
// iterate through the list randomly selecting items until done
for (i = 0; i < Pics; i++)
{
var n = Math.floor ( Math.random() * me.length );
_Order [i] = me [n];

/* remove the item so it's not selected again (various options here like change
value of selected index then list.sort list.pop method, study Array section in
Cybero's JS guide) */
bye = me.splice(n,1);
}
result.Order = _Order;
//   result.List = _List;
return result;
}```
PreviewAttachmentSize
Random shuffle thru struct.qtz18.41 KB