Home > Uncategorized > Flex Randomness

Flex Randomness

January 13th, 2009 Leave a comment Go to comments

The only reason why your Flex app should do something randomly is by your using the Math.random() function. You can argue about the UIDUtil.createUID() and such but you get the point. Today I witnessed randomness as never before.

Try to run this code (Flex 3.2)

var obj:Object = { a: "valueA", b: "valueB" };
for(var property:String in obj) {
  trace(property);
}

Sometimes it will output a-b and other times b-a. I have no idea how it determines the order and how come it’s never the same. But it sure caused me some pain. Found a workaround using PHP once received on the back-end!

Tags:
  1. January 15th, 2009 at 13:35 | #1

    Your example always traces b-a to me, but if I add a “c” property at the end I get “bca”…and yes it seems i get a non-logical trace….but allways the same one. I don’t think is random…we only miss the logic behind it:P

  2. February 6th, 2009 at 14:09 | #2

    I have same problem with Flash Media Server. I think trace() function use asynchronous event to broadcast messages to user interface (Flash).

    So if you realy want to see if the order of members are randomly change try this :

    var obj:Object = { a: “valueA”, b: “valueB” };
    var s:String = ”;
    for(var property:String in obj) {
    s += property + “\n”;
    }
    trace(s);

  3. February 6th, 2009 at 14:46 | #3

    In my case, I traced it after I discovered that when iterating through the object, I got an array whose elements were never in the same order. So I can be sure that it’s not due to asynchronous event broadcasting. It’s really just the ActionScript.

    Your idea is good though. Combining all elements into a single string before output does prevent biased results.

  1. No trackbacks yet.

Switch to our mobile site