dcsimg
A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: random order in a number

  1. #1
    Junior Member
    Join Date
    Nov 2006
    Posts
    17

    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.

  2. #2
    OOP is one letter from OOPS kortex's Avatar
    Join Date
    Aug 2005
    Location
    New Hope, PA
    Posts
    2,668
    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.
    Jeremy Wischusen
    Flash - Flex - LAMP - Web Developer Purple Inc
    AS OOP FAQ-Best Practices Thread | Flashkit OOP Tutorials | Purple Inc (day job) | Blog


  3. #3
    Senior Member
    Join Date
    Oct 2007
    Location
    Leeds, UK
    Posts
    118
    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;
    	
    }

  4. #4
    OOP is one letter from OOPS kortex's Avatar
    Join Date
    Aug 2005
    Location
    New Hope, PA
    Posts
    2,668
    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
    Last edited by kortex; 06-04-2008 at 03:09 PM.
    Jeremy Wischusen
    Flash - Flex - LAMP - Web Developer Purple Inc
    AS OOP FAQ-Best Practices Thread | Flashkit OOP Tutorials | Purple Inc (day job) | Blog


  5. #5
    trace("AKA: Biro Barna");
    Join Date
    Oct 2007
    Location
    RO.Timişoara
    Posts
    1,403
    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 0inValueString.lengthi++)
        {
            array[
    i] = inValueString.slice(ii+1);
            
            if (
    == (inValueString.length 1))
            {
                for (var 
    j:int 0inValueString.lengthj++)
                {
                    var 
    rand:Number Math.round(Math.random() * array.length);
                    
                    if (array[
    rand] == null)
                    {
                        
    j--;
                        continue;
                    }
                    else
                    {
                        
    array2[j] = array[rand];
                        array.
    splice(rand1);
                    }
                    
                    if (
    == (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.



    | Windows MSN: birobarna [at] hotmail [dot] com | Skype: barna.biro |
    WebLog: http://blog.wisebisoft.com/ |
    | Software Developer / Flash & Flex Developer | Student ( Computer Science ) | Interested in: Sharing Knowledge |
    |
    Romanian Adobe Flash, Flex, AIR Forum: http://www.flashforum.ro/
    | By perseverance the snail reached the ark. |


  6. #6
    trace("AKA: Biro Barna");
    Join Date
    Oct 2007
    Location
    RO.Timişoara
    Posts
    1,403
    Ahh, btw, sorry, my code is for AS 3.0 ; I forgot to mention that little thingie...



    | Windows MSN: birobarna [at] hotmail [dot] com | Skype: barna.biro |
    WebLog: http://blog.wisebisoft.com/ |
    | Software Developer / Flash & Flex Developer | Student ( Computer Science ) | Interested in: Sharing Knowledge |
    |
    Romanian Adobe Flash, Flex, AIR Forum: http://www.flashforum.ro/
    | By perseverance the snail reached the ark. |


  7. #7
    Junior Member
    Join Date
    Nov 2006
    Posts
    17
    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!

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center