A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: making random not repeat

  1. #1
    Junior Member
    Join Date
    Aug 2009
    Posts
    3

    making random not repeat

    Hi, I have some actionscript here, Im wanting to change the variable numSound. At the moment it is a random number 1-5 which works well but I dont want repetition. Tried all sorts of things but no luck. Any help appreciated.
    Code:
    private function randRange(min:Number, max:Number):Number
    	{
        	var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min;
        	
        	return randomNum;
    	}
    	
    	/**
    	 * selected random sound 
    	 */
    	public function playHelpSound():Void
    	{
    		trace("playHelpSound active: " + active);
    		
    		if (active)
    		{
    			var numSound:Number = randRange(1, 5);
    		
    			var urlSound:String = XPathAPI.selectSingleNode(_xmlTeacherSounds.firstChild, 'teacher/study/sound[@sndnum="st' + numSound + '"]').attributes['sndurl'];

  2. #2
    Senior Member vinayak.kadam's Avatar
    Join Date
    Oct 2006
    Location
    gotoAndPlay("Pune");
    Posts
    831
    You have two options:

    1st Option: When you generate a random no, push it in some array and later when you generate 2nd random no, check it with the previous nos to be sure that it is not repeated.

    2nd Option: Generate and array having nos in a random no within the range of 1 to 5. Use array nos sequentially so as to never repeat the number.
    As ever,
    Vinayak Kadam

  3. #3
    Junior Member
    Join Date
    Aug 2009
    Posts
    3
    thanks, thats a bit beyond me. I have tried code like this but I dont know how to get the number out of it
    Code:
    var arrayToRandom:Array = new Array();
                 var qRequired:Number = 5;
     
                 // Inserts Numbers in Array
                 for(var i:Number = 1; i<=qRequired; i++)
                 {
    	              arrayToRandom.push( i );
                    }
     
                       // Outputs numbers non-repeated
                      for(var k:Number = 1; k<=qRequired; k++)
                     {
    	              // Search for value inside the Array in a random position
    	                 var randomPos:Number = Math.floor( Math.random() * arrayToRandom.length );
     
    	                // Selects value and removes it from Array
    	              var valueFromArray = arrayToRandom.splice(randomPos, 1);
     
    	              // Converts value in number
    	               var numberRand = parseInt(valueFromArray);
     
    	               trace(numberRand);
    }

  4. #4
    Senior Member vinayak.kadam's Avatar
    Join Date
    Oct 2006
    Location
    gotoAndPlay("Pune");
    Posts
    831
    Use this code to randomize an array:
    PHP Code:
    arrayToRandom = [1,2,3,4,5]; // array of numbers

    arrayToRandom.sort(function(){return Math.floor(Math.random()*3)-1}); // randomise the array 

    trace(arrayToRandom); 
    last trace will show you that the array 'arrayToRandom' will have numbers in the range of 1 to 5 in random order.
    As ever,
    Vinayak Kadam

  5. #5
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    Code:
    var arrayToRandom:Array = new Array();
    var qRequired:Number = 5;
     
                 // Inserts Numbers in Array
    for(var i:Number = 1; i<=qRequired; i++)
    {
    	 arrayToRandom.push( i );
    }
     
    // Outputs numbers non-repeated
    for(var k:Number = 1; k<=qRequired; k++)
    {
    
    	var randomPos:Number = random(arrayToRandom.length);
    	randomNumber = arrayToRandom[randomPos];
    	arrayToRandom.splice(randomPos, 1);
    	trace(randomNumber); 
    }
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  6. #6
    Junior Member
    Join Date
    Aug 2009
    Posts
    3
    if I replace

    var numSound:Number = randRange(1, 5);

    with your code or any of the others I have tried I get random numbers but they repeat

    maybe I need to put it earlier in the code?

  7. #7
    Senior Member Robb@exo's Avatar
    Join Date
    Jun 2003
    Location
    Canada
    Posts
    1,540
    Came across this link recently from another post. Check out the "Array randomise" section which incorporates non-repetitive values.
    http://www.jackleaman.co.uk/flash_db...nscriptV8.html
    Wile E. Coyote - "Clear as mud?"

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