actionscript 3 - Randomly selecting an object property -


i guess step in order. original question @ bottom of post reference.

i writing word guessing game , wanted way to: 1. given word length of 2 - 10 characters, randomly generate valid english word guess 2.given 2 - 10 character guess, ensure valid english word. created vector of 9 objects, 1 each word length , dynamically created 172000 property/ value pairs using words word list name properties , setting value true. inner loop is:

for (i = 0; < _wordcount[wordlength] - 2; i)  {     _wordslist[wordlength]["" + _wordsvector[wordlength][i++]] = true; } 

to validate word , following lookup returns true if valid:

function validate(key:string):boolean {     return  _wordslist[key.length - 2][key] } 

i transferred them vector objects take advantage of hash take lookup of properties. haven't looked @ how memory takes it's been useful learning exercise. wasn't sure how best randomly choose property 1 of objects. thinking of validating whatever method chose generating 1000 000 words , analyzing statistics of distribution. suppose question should first better off using other approach such keeping lists in vectors , doing search each time ?


original question

newbie first question:

i read thread said traversal order in for.. in determined hash table , appears random.

i'm looking way randomly select property in object. first element in .. in traversing properties, or perhaps random nth element in iteration random. i'd ensure there approximately equal probability of accessing given property. objects have between approximately 100 , 20000 properties. other approaches ?

thanks.

looking @ scenario described in edited question, i'd suggest using vector.<string> , map object.

you can store keys in vector , map them in object, can select random numeric key in vector , use result key in map object.

to make clear, take @ simple example:

var keys:vector.<string> = new vector.<string>(); var map:object = { };  function add(key:string, value:*):void {     keys.push(key);     map[key] = value; }  function getrandom():* {     var randomkey = keys[int(math.random() * keys.length)];      return map[randomkey]; } 

and can use this:

add("a", "x"); add("b", "y"); add("c", "z");  var radomvalue:* = getrandom(); 

using object instead of string

instead of storing strings can store objects have string inside of them, like:

public class word {     public var value:string;     public var length:int;      public function word(value:string)     {         this.value = value;         this.length = value.length;     } } 

use object value instead of string, need change map object dictionary:

var map:dictionary = new dictionary();  function add(key:word, value:*):void {     keys.push(key);     map[key] = value; } 

this way won't duplicate every word (but have little class overhead).


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -