Monthly Archives: February 2009

String.localeCompare()

Actionscript:
  1. var letterA:String = "a";
  2. var letterZ:String = "z";
  3.  
  4. trace(letterA.localeCompare(letterZ));
  5. trace(letterZ.localeCompare(letterA));
  6. /*
  7. outputs
  8. -25
  9. 25
  10. */

String.localeCompare() calculates the sorting order between two Strings....

Posted in strings | Tagged , | Leave a comment

Random Sort

Actionscript:
  1. function randomize(a:*, b:*):int{
  2.        return Math.round(Math.random()*8) - 4;
  3. }
  4.  
  5. var i:int;
  6. var fruits:Array;
  7.  
  8. trace("Math.random()");
  9. for (i = 0; i<4; i++){
  10.   // reset fruits array:
  11.   fruits = ["apple", "grape","pear","cherry"];
  12.   fruits.sort(randomize);
  13.   trace(fruits);
  14. }
  15.  
  16.  
  17. // seeds
  18. var s1:Number= 0xFF00FF;
  19. var s2:Number = 0xCCCCCC;
  20. var s3:Number= 0xFF00F0;
  21.  
  22. function tRandomize(a:*, b:*):int{
  23.        return Math.round(rand()*8) - 4;
  24. }
  25.  
  26. trace("\nTausworthe rand()");
  27. for (i= 0; i<4; i++){
  28.   fruits = ["apple", "grape","pear","cherry"];
  29.   fruits.sort(tRandomize);
  30.   trace(fruits);
  31. }
  32. // from www.ams.org/mcom/1996-65-213/S0025-5718-96-00696-5/S0025-5718-96-00696-5.pdf
  33. function rand():Number {
  34.     s1=((s1&4294967294)<<12)^(((s1<<13)^s1)>>19);
  35.     s2=((s2&4294967288)<<4)^(((s2<<2)^s2)>>25);
  36.     s3=((s3&4294967280)<<17)^(((s3<<3)^s3)>>11);
  37.     var r:Number = (s1^s2^s3) * 2.3283064365e-10;
  38.     r = (r<0) ? r+=1 : r;
  39.     return r;
  40. }
  41.  
  42. /*
  43. outputs:
  44. Math.random()
  45. grape,apple,pear,cherry
  46. pear,cherry,apple,grape
  47. grape,apple,pear,cherry
  48. grape,apple,cherry,pear
  49.  
  50. Tausworthe rand()
  51. apple,grape,pear,cherry
  52. cherry,grape,pear,apple
  53. grape,apple,cherry,pear
  54. grape,pear,apple,cherry
  55. */

The above shows how to randomly sort or shuffle an array. This is useful in games. To achieve this I made use of the compareFunction argument of Array.sort(). Most sorting algorithms go through the array and compare values until the desired sort order is achieved. The compareFunction argument is a function that takes two values a and b and returns an integer that is negative positive or zero... see this info from the docs:
* A negative return value specifies that A appears before B in the sorted sequence.
* A return value of 0 specifies that A and B have the same sort order.
* A positive return value specifies that A appears after B in the sorted sequence.

So in the case of a randomizing an array you simply need to return a random int -1, 0 or 1. This is what I've done in the past (Math.round()*2) -1) ... but when I was writing this snippet it seemed like 0 caused less variation in the output of the array so I made the range from -4 to 4 instead. This could have just been my imagination, but it seems like having less chance of a zero caused the arrays to be a bit more shuffled.

The reason I also included a version that uses Tausworthe is because of the easy seeding. In some cases you may want to use seeded randomness to sort an array.

UPDATE:
Was digging around about this and found a much faster method for randomizing arrays... not a big deal if you have small arrays, but if you need to randomize 1000's of values this method is much faster than using Array.sort()

Posted in arrays, misc | Tagged , | Leave a comment

String.indexOf()

Actionscript:
  1. var words:String = "one two three four five";
  2.  
  3. trace("two: ", words.indexOf("two"));
  4.  
  5. var letters:String = "abcd";
  6.  
  7. trace("d: ",letters.indexOf("d"));
  8.  
  9. trace("z: ",letters.indexOf("z"));
  10.  
  11. /*
  12. outputs:
  13. two:  4
  14. d:  3
  15. z:  -1
  16. */

indexOf() searches a string for another string and returns an index... in line 3 above, I search the words string for the smaller string "two" and indexOf() gives me the index of the letter "t". If indexOf() doesn't find anything it will return -1 (as in the case of line 9).

I seem to recall using this in some unexpected places. I'll see if I can dig up an example over the next few days.

Posted in string manipulation, strings | Tagged , | Leave a comment

Zig Zag Binary ix2 & i/2

Actionscript:
  1. var txt:TextField = TextField(addChild(new TextField()));
  2. txt.text = "";
  3. txt.width = 190;
  4. txt.height = 400;
  5. txt.multiline = true;
  6.  
  7. var count:int = 1;
  8. function render():void{
  9.     var line = int(count).toString(2);
  10.     while(line.length <31){
  11.         line = "0" + line;
  12.     }
  13.     txt.appendText(line + "\n");
  14.     txt.scrollV= txt.maxScrollV;
  15. }
  16.  
  17. addEventListener(Event.ENTER_FRAME, onCountUp);
  18. function onCountUp(evt:Event):void {
  19.     count *= 2;
  20.     render();
  21.     if (count ==0x40000000){
  22.         removeEventListener(Event.ENTER_FRAME, onCountUp);
  23.         addEventListener(Event.ENTER_FRAME, onCountDown);
  24.     }
  25. }
  26. function onCountDown(evt:Event):void {
  27.     count /= 2;
  28.     render();
  29.     if (count ==1){
  30.         addEventListener(Event.ENTER_FRAME, onCountUp);
  31.         removeEventListener(Event.ENTER_FRAME, onCountDown);
  32.     }
  33. }

The above animates a zig zag pattern in a TextField.

Posted in Math, string manipulation, strings | Leave a comment