 A Flash Developer Resource Site

# Thread: random order in a number

1. ## random order in a number

Hi foreros
I am trying to generate random numbers which will be the result of a random combination of the figures in a given number, for example, if I have
var num:Number=01234; the function should give me three posible combinations of the figures in it, such as 12034, 34120 and 41302.
Do I have to convert the number into an array?
Can anybody help me with the function?
Thank you in advance.  Reply With Quote

2. I will assume that you need to start and and with a numeric type.
var shuffled:Number = parseInt(num.toString().split('').sort(randomSort) .join());

function randomSort(){
return Math.floor(Math.random()*3)-1;
}

have not actually tested it, but I think that should do it. It would give you a random sequence of the original number each time you run it, but there is the possibility you could get the same sequence in separate calls.  Reply With Quote

3. You could something like this, writing your number as a string, and
using an array, but it seems long-winded. I can't help thinking there
must be a quicker way.

Code:
```var given:String = "01234";

trace(randomOrderings(given)); // 31042,04213,12043

function randomOrderings(original:String):Array
{
// returns an array containing three different random orderings of
// the digits in 'original' string

var endArray:Array = new Array(); // will hold our new orderings
var c:uint = original.length; // number of characters to be sorted

for(var j:uint = 0; j<3; j++) // we want three different orderings
{
// copy the digits in 'original' into an array called 'startArray'
var startArray:Array = new Array();
for(var i:uint =0; i<original.length; i++)
{
startArray[i] = original.substr(i,1);
}
// create an empty string called newOrder to hold a new ordering of the digits
var newOrder:String = new String();
newOrder = "";

// fill the newOrder string by repeatedly selecting a digit at random
for(var k:uint = 0; k < c; k++)
{
// pick a random position in startArray
var position:uint = Math.floor(Math.random()*startArray.length);

// add the digit in that position to the string 'new'
newOrder += startArray[position];

// remove that digit from startArray
startArray.splice(position,1)
}
// put the new ordering into endArray

endArray[j] = newOrder;
}

return endArray;

}```  Reply With Quote

4. ok I actually tried this and I get something along the lines of what you are looking for

var num:String = '01234';
var shuffled = num.split('').sort(randomSort).join('');
trace (shuffled);
function randomSort(){
return Math.floor(Math.random()*3)-1;
}

the trace output:
04312  Reply With Quote

5. Well, since everyone came up with something then here's my approach too:

PHP Code:
``` var inValue:Number = 123456;var outValue:Number;var array:Array = [];var array2:Array = [];randomize();function randomize():void{    var inValueString:String = String(inValue);        for (var i:int = 0; i < inValueString.length; i++)    {        array[i] = inValueString.slice(i, i+1);                if (i == (inValueString.length - 1))        {            for (var j:int = 0; j < inValueString.length; j++)            {                var rand:Number = Math.round(Math.random() * array.length);                                if (array[rand] == null)                {                    j--;                    continue;                }                else                {                    array2[j] = array[rand];                    array.splice(rand, 1);                }                                if (j == (inValueString.length - 1))                {                    outValue = Number(array2.join(""));                    trace(outValue);                }            }        }    }}  ```
Didn't try out the other methods but if kortex's method works, that might be best solution. Mine is just something I threw together in 3 mins... works, tested it, but you'll be the one deciding which one works best for you.  Reply With Quote

6. Ahh, btw, sorry, my code is for AS 3.0 ; I forgot to mention that little thingie...  Reply With Quote

7. I appreciate your interest! Yes, this is quite much what I needed.
And,Plenary, I am still working with AS2, but anyway, I think I also forgot to mention it.
Thank you all for your help!  Reply With Quote

#### 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 