Friday, July 17, 2009

Generate unique random numbers within a range

Following on from a previous post on how to generate a list of unique random numbers, this similar function will allow you to do the same...but within a predefined range of numbers:

function randListFromRange(firstNo:Number, lastNo:Number, num:Number, shuffled:Boolean):Array {
var tempArray:Array = [];
for (i = firstNo - 1; i < lastNo && num > 0; ++i) {
if (Math.floor(Math.random() * (lastNo - i)) < num) {
tempArray.push(i + 1);
num--;
}
}
if (shuffled) {
tempArray.sort(function () {
return Math.floor(Math.random() * 2) ? true : false;
});
}
return (tempArray);
}

To use this function, just provide the first and last numbers in your range, and the number of items you wish to generate from that list. For example, you might want to create an array of 7 unique random numbers that lie between 36 and 83:

// to generate an array of 7 unique sequential numbers
// between, and including, 36 and 83
var myArr:Array = randListFromRange(36, 83, 7);
trace(myArr);

As previously, the function still allows for the list of random numbers to be shuffled:

// to generate an array of 7 unique shuffled numbers
// between, and including, 36 and 83
var myArr:Array = randListFromRange(36, 83, 7, true);
trace(myArr);

Two points to note:
1. If your first number is greater than your last number, then no random numbers will be generated. For example:

var myArr:Array = randListFromRange(20, 10, 5);
trace(myArr); // outputs nothing

2. If the specified range is smaller than the desired number, the function will return every integer within that range. For example:

// attempt to generate an array of 10 unique shuffled numbers
// between, and including, 10 and 15
var myArr:Array = randListFromRange(10, 15, 10);
trace(myArr); // outputs 10, 11, 12, 13, 14, 15

Thursday, July 9, 2009

Change HTML background image

In a similar vein to changing the background colour of a HTML page, this script will allow you to change the background image. Add the following Actionscript and a movieclip with an instance name of "btn":
import flash.external.*;
var jsCall:String;
btn.onPress = function() {
jsCall = String(ExternalInterface.call("changeBgImg", "myNewImg.jpg"));
};

Add the following Javascript to your HTML code:

<script language="JavaScript">
function changeBgImg(newBgImg) {
document.body.background = newBgImg
}
</script>

As before, you can still add a little random spice by using the following Actionscript:
import flash.external.*;
bgImages = new Array("film0.jpg", "film1.jpg", "film2.jpg", "film3.jpg", "film4.jpg", "film5.jpg", "film6.jpg", "film7.jpg", "film8.jpg", "film9.jpg");
var jsCall:String;
btn.onPress = function() {
ranImg = bgImages[Math.floor(Math.random() * 10)];
jsCall = String(ExternalInterface.call("changeBgImg", ranImg));
};

You can see an example here along with the code.

Note that this script will follow the default behaviour and cause your image to be tiled across your HTML background. If you don't want it to be tiled, try changing the script to this:

<script language="JavaScript">
function changeBgImg(newBgImg) {
document.body.background = newBgImg
document.body.style.backgroundRepeat='no-repeat';
document.body.style.backgroundPosition='center';
}
</script>

You can see an example of a non-tiled image change here.