A Flash Developer Resource Site

# Thread: randomly sort an array question

1. Hi,

I would like to generate an array of the numbers 1 to 15 in a random order without writing a squillion lines of code.

regards,
a_non_amous.

2. Hi,

Heres my attempt.

================================================== =
rand_array = new Array (15);
value1 = "010203040506070809101112131415"

for (n=0; n<rand_array.length; n++){
ival1 = (15-n);
ival2 = random(ival1);
ival3 = (ival2 * 2);
ival4 = (ival3 + 2);
ival5 = (strlen(value1) - ival4);
ival6 = strlen(value1);
value2 = substring(value1,0,ival3);
value3 = substring(value1,ival4,ival5);
value2 = (value2 + value3);
rand_array[n] = substring(value1,ival3,2);
value1 = value2;
}
================================================== =

Looking for improvements..

3. ## sort

Originally posted by a_non_amous
Hi,

Heres my attempt.

================================================== =
rand_array = new Array (15);
value1 = "010203040506070809101112131415"

for (n=0; n<rand_array.length; n++){
ival1 = (15-n);
ival2 = random(ival1);
ival3 = (ival2 * 2);
ival4 = (ival3 + 2);
ival5 = (strlen(value1) - ival4);
ival6 = strlen(value1);
value2 = substring(value1,0,ival3);
value3 = substring(value1,ival4,ival5);
value2 = (value2 + value3);
rand_array[n] = substring(value1,ival3,2);
value1 = value2;
}
================================================== =

Looking for improvements..

I'm looking at this and it's hard to follow what you're doing but does this work? If it does that not alot of code to accomplish it.

But the first thing I'd think of is do you really need it sorted? Could you not when using the array randomly choose an element instead? Also you don't seem to be keeping track of if an element in the array has already been used.

I'll look at this and get back with you.

4. Hi,

Details:

================================================== ========
rand_array = new Array (15);
value1 = " 1 2 3 4 5 6 7 8 9101112131415" //setup numbers

for (n=0; n<rand_array.length; n++){
ival1 = (15-n); //length val1 / 2
ival2 = random(ival1); //randomize
ival3 = (ival2 * 2); //start pos select
ival4 = (ival3 + 2); //end pos select
ival5 = (strlen(value1) - ival4); //len end pos to end str
value2 = substring(value1,0,ival3); //left to start
value3 = substring(value1,ival4,ival5); //right from end
value2 = (value2 + value3); //cut out selection
rand_array[n] = substring(value1,ival3,2); //get selection
value1 = value2; //set value1 with selection gone
================================================== ========

works perfectly.

Am still open to improvements.

Regards,
a_non_amous.

5. Hi a_non_amous,

Here is an answer to your question with some inline comments to help explain.

Code:
```/*
init an array with the elements you want in it.
the elements can be anything you want them to be
including peoples names.
*/

rand_array = new Array("01","02","03","04","05","06","07","08","09","10","11","12","13","14","15");
junk = "";

// how many elements are there?
max_items = rand_array.length;

/*
This next bit of code simply goes through the list
of elements one at a time and swaps each
successive element with one that is randomly
picked elsewhere in the array.

The end result is a randomly sorted array of
elements.
*/

for(ndx = 0; ndx < max_items; ndx += 1)
{
// randomly pick an element
r_num = random(14);

// remember the current element
tmp = rand_array[ndx];

// swap the current element with that of the
// randomly selected element.
rand_array[ndx] = rand_array[r_num];

// now replace the randomly selected element with
// the element we remembered earlier.
rand_array[r_num] = tmp;
}

// That's it! Array is now scrambled.```
You can make the code a lot shorter by removing the comment lines out of it.

In essence the code above doesn't require you to keep track of which elements you have randomly selected since you are only swapping them around within the array itself.

Also, you can avoid a lot of string manipulation if you initialise the array with the 15 numbers at the start. From what I can tell you are trying to get that leading zero in.

HTH....oh and yes there is another beer fee

6. I will have to buy a pub at this rate.

sample of incomplete project with code in use
at --

http://www.ozlinkz.com/flash/puzzle.html

regards,
a_non_amous.

7. ## Looks very nice....

Hi a_non_amous,

I like the look of what you have done. It's similar in concept to what another forum member has done not too long ago. I just can't recall who it was.

Don't let this discourage you as I think it's great and can't wait to see the final work.

I don't think you need to invest in a pub....not yet anyway

8. ## Re: Looks very nice....

Nice Ed! As usual

Here was what I was trying (didn't work)

myarray=new Array (15);

for (idx=0;idx<15;idx++){
placed = false;

while (placed==false){
pointer=random(14);
if (myarray[pointer]==null){
placed=true;
myarray[pointer]=idx;

}
}
}

Had a problem with it locking up in the while loop.

9. ## Locking up...

Hi Bret,

Thanks.

Your code will work too.

The locking problem is because the random() function has been asked to generate a random number from 0 to 13.

ie. random(14); // will generate a random number from 0 to 14-1.

After checking my code I too have used 14 in the random() function. The code will still randomly 'sort' the array as each of the elements will then be swapped with a random element positioned between 0 and 13. So the 14th position in the array will still be randomly placed.

The help on the random() element needs to be updated so that is states 'from 0 to n-1'.

10. ## Re: Locking up...

Originally posted by kusco
Hi Bret,

Thanks.

Your code will work too.

The locking problem is because the random() function has been asked to generate a random number from 0 to 13.

ie. random(14); // will generate a random number from 0 to 14-1.

After checking my code I too have used 14 in the random() function. The code will still randomly 'sort' the array as each of the elements will then be swapped with a random element positioned between 0 and 13. So the 14th position in the array will still be randomly placed.

The help on the random() element needs to be updated so that is states 'from 0 to n-1'.

Yeah I thought that was a bug, even sent it a report

I made a randomrange function that does what I expected from a random function

Code:
```//Randomrange Function by Bret Lanius http://bretlanius.com
function randomrange(Min,Max){//Function to get a range of random numbers
therange=Max-Min;
therand=random(therange);
myrand=Min+therand;
return myrand
}```
this way you can get a random number in a range i.e.

mynumber=randomrange(20,40)

will return a random number bewtween 20 and 39

Oh and I added this to the syntax menu so I can just drop it in via a click, and also a moveto function for elements

11. ## Useful function

Hi Bret,

That function will come in handy now and then.

I couldn't resist and offer a more slimline method of getting the same result.
Code:
```function RandomRange(Min,Max)
return random(Max-Min) + Min;```

12. ## Re: Useful function

Originally posted by kusco
Hi Bret,

That function will come in handy now and then.

I couldn't resist and offer a more slimline method of getting the same result.
Code:
```function RandomRange(Min,Max)
return random(Max-Min) + Min;```
Almost did something like that after I worked it out. But decided to leave it this way so it would be easier to understand when someone looked at it.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap

Click Here to Expand Forum to Full Width