Jiri wrote:
Hello list,
I have an XMLList that can vary in length. I pick a random node each
time using the simple method below:
<code>
var tRandom:int = int(Math.random()*tXMLSource.nodes.length() );
return tXMLSource.nodes[tRandom]
</code>
Now i would like to change it, so that I can specify a weight to each
node. This weight would have to determine the probability the nodes
get selected out when a random pick is being performed.
Does anybody know how to build such an algorithm?
Sum all of the node weights, then generate a random number within that
range.
Now you have to find the node corresponding to that number. This can be
done by traversing the nodes in sequence. As you traverse the nodes sum
the weight until the value generated is within the range - then you have
the right node.
You can optimise this by using an array of pointers, or keeping the
weights in an external array, but that may be more or less interesting
depending on the number of nodes involved and what optimisation you need.
Paul
Much appreciated,
Jiri
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders