-
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'];
-
Senior Member
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.
-
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);
}
-
Senior Member
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.
-
Flashmatics
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);
}
-
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?
-
Senior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|