## Friday, April 1, 2011

### Lottery Number Function

Again following from a previous discussions on generating unique random numbers within a range, here's a quick function you can use when the range starts from 1, just as most lottery draws do.
`var getRandomLottoNumbers = function (m:Number, n:Number):Array { m==undefined?m=6:m=m n==undefined?n=49:n=n for(var v=[];m;--n)Math.random()*n>m?0:v[--m]=n;return v}`

Using it is quite straightforward. Just pass two arguments to the function - the number of selections you want to make (m), and the total numbers you are selecting from (n). If you omit the arguments, the function will default to selecting 6 random numbers from a total of 49:
`// generate 3 random numbers between 1 and 10trace(getRandomLottoNumbers(3, 10));// generate 6 random numbers between 1 and 49 (default)trace(getRandomLottoNumbers());`

## Thursday, October 29, 2009

### Comparisons with switch/case

The switch and case statements in Actionscript are a useful way of making nested if statements more readable in your code. However they do have one drawback in that the compiler won't allow you to make comparisons. For example, the following code will always skip over the case statements and return the default value:
`var myVar:Number = 15;switch (myVar) {case (myVar > 0 && myVar < 11) : trace("between 0-10"); break;case (myVar > 10 && myVar < 21) : trace("between 10-20"); break;case (myVar > 20 && myVar < 31) : trace("between 21-30"); break;default :  <-- always returns the default value trace("number outside of range"); break;}`

If you try to remove the references to myVar, the compiler will provide a number of error messages, along the lines of 'case' statements can only be used inside of a 'switch' statement, or Unexpected '>' encountered:
`case ( > 0 &&  < 11) : // <-- gives an errorcase  > 10 &&  < 21 : // <-- gives an error`

Fortunately it is still possible to perform comparisons by using a Boolean value as the switch expression:
`var myVar:Number = 15;switch (true) { // <-- note the Boolean value herecase (myVar > 0 && myVar < 11) : trace("between 0-10"); break;case (myVar > 10 && myVar < 21) : trace("between 10-20"); break;case (myVar > 20 && myVar < 31) : trace("between 21-30"); break;default : trace("number outside of range"); break;}`

## Sunday, September 6, 2009

### Anagrams

A quick prototype to check if two strings are anagrams of each other:
`String.prototype.isAnagram = function(string:String):Boolean  { return (this.toLowerCase().split('').sort().join('') == string.toLowerCase().split('').sort().join('')) ? true : false;};`

Use as follows:
`string_1 = "dear";string_2 = "read";string_3 = "reed";trace(string_1.isAnagram(string_2)); // truetrace(string_1.isAnagram(string_3)); // false`

## Sunday, August 16, 2009

### Easter revisited

Just changed the previous function to calculate Easter Day into a Date prototype. The prototype automatically returns a new Date() object for Easter for the current year but it will also accept an argument for any year:

`Date.prototype.calcEaster = function(year:Number):Date  { if(year == null || year == undefined)year = this.getFullYear(); month = Math.floor((((19 * (year % 19) + Math.floor(year / 100) - Math.floor(Math.floor(year / 100) / 4) - Math.floor((Math.floor(year / 100) - Math.floor((Math.floor(year / 100) + 8) / 25) + 1) / 3) + 15) % 30) + (32 + 2 * (Math.floor(year / 100) % 4) + 2 * Math.floor((year % 100) / 4) - ((19 * (year % 19) + Math.floor(year / 100) - Math.floor(Math.floor(year / 100) / 4) - Math.floor((Math.floor(year / 100) - Math.floor((Math.floor(year / 100) + 8) / 25) + 1) / 3) + 15) % 30) - (year % 100) % 4) % 7 - 7 * Math.floor(((year % 19) + 11 * ((19 * (year % 19) + Math.floor(year / 100) - Math.floor(Math.floor(year / 100) / 4) - Math.floor((Math.floor(year / 100) - Math.floor((Math.floor(year / 100) + 8) / 25) + 1) / 3) + 15) % 30) + 22 * (32 + 2 * (Math.floor(year / 100) % 4) + 2 * Math.floor((year % 100) / 4) - ((19 * (year % 19) + Math.floor(year / 100) - Math.floor(Math.floor(year / 100) / 4) - Math.floor((Math.floor(year / 100) - Math.floor((Math.floor(year / 100) + 8) / 25) + 1) / 3) + 15) % 30) - (year % 100) % 4) % 7) / 451) + 114) / 31) - 1; day = ((((19 * (year % 19) + Math.floor(year / 100) - Math.floor(Math.floor(year / 100) / 4) - Math.floor((Math.floor(year / 100) - Math.floor((Math.floor(year / 100) + 8) / 25) + 1) / 3) + 15) % 30) + (32 + 2 * (Math.floor(year / 100) % 4) + 2 * Math.floor((year % 100) / 4) - ((19 * (year % 19) + Math.floor(year / 100) - Math.floor(Math.floor(year / 100) / 4) - Math.floor((Math.floor(year / 100) - Math.floor((Math.floor(year / 100) + 8) / 25) + 1) / 3) + 15) % 30) - (year % 100) % 4) % 7 - 7 * Math.floor(((year % 19) + 11 * ((19 * (year % 19) + Math.floor(year / 100) - Math.floor(Math.floor(year / 100) / 4) - Math.floor((Math.floor(year / 100) - Math.floor((Math.floor(year / 100) + 8) / 25) + 1) / 3) + 15) % 30) + 22 * (32 + 2 * (Math.floor(year / 100) % 4) + 2 * Math.floor((year % 100) / 4) - ((19 * (year % 19) + Math.floor(year / 100) - Math.floor(Math.floor(year / 100) / 4) - Math.floor((Math.floor(year / 100) - Math.floor((Math.floor(year / 100) + 8) / 25) + 1) / 3) + 15) % 30) - (year % 100) % 4) % 7) / 451) + 114) % 31) + 1; return new Date(year, month, day);};`

Example usage (default and with optional argument):

`theDate = new Date();trace(theDate.calcEaster());// returns Sun Apr 12 00:00:00 GMT+0100 2009trace(theDate.calcEaster(1999));// returns Sun Apr 4 00:00:00 GMT+0100 1999`

## 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 83var 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 83var 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 15var 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"));};`

`<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.

## Friday, April 3, 2009

### Change HTML background colour

More External Interface stuff. This snippet of code will enable you to change the background colour of a HTML page. 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("changeBgColor", "#fffecb));};`

`<script language="JavaScript">function changeBgColor(newBgColor){if (window.document && window.document.bgColor)   {      document.bgColor = newBgColor;   }}</script>`
`import flash.external.*;var jsCall:String;btn.onPress = function() { ranColor = Math.round(Math.random() * 0xFFFFFF); jsCall = String(ExternalInterface.call("changeBgColor", ranColor));};`