[Flashcoders] Determining probablity of a random pick.

2009-06-22 Thread Jiri

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?

Much appreciated,

Jiri
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Determining probablity of a random pick.

2009-06-22 Thread Paul Andrews

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
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders



___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Determining probablity of a random pick.

2009-06-22 Thread Jiri

Thank you i understand it. Could u please elloborate on the last part
..You can optimise this by using an array of pointers..

Do you mean that this;

a weight=5
b weight=1
c weight=2

First calculated the range for each node.
A = 0 - 5
B = 6 - 7
C = 8 - 10

Then store in an array like so?
Array[0-5] = A
Array[6-7] = B
Array[8-10] = C

Last step get a random number between 0 - 10 = for instance 5

//result is then
return Array[5] as XML


If this is what you mean, would this approach not take a 'relatively' 
big amount of memory?


JIri

Paul Andrews wrote:

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
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders



___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Determining probablity of a random pick.

2009-06-22 Thread Paul Andrews

Jiri wrote:

Thank you i understand it. Could u please elloborate on the last part
..You can optimise this by using an array of pointers..

Do you mean that this;

a weight=5
b weight=1
c weight=2

First calculated the range for each node.
A = 0 - 5
B = 6 - 7
C = 8 - 10

Then store in an array like so?
Array[0-5] = A
Array[6-7] = B
Array[8-10] = C

Last step get a random number between 0 - 10 = for instance 5

//result is then
return Array[5] as XML


If this is what you mean, would this approach not take a 'relatively' 
big amount of memory?
You don't have to store the node itself, just a reference to it, so no 
it wouldn't be a memory hog. It could even just be the numerical index 
of the node.


Paul




JIri

Paul Andrews wrote:

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
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders



___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders



___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Determining probablity of a random pick.

2009-06-22 Thread Anthony Pace

An easy way is to if say you have 3 objects in a list given a weight

options[0].weight = 1;
options[1].weight = 3;
options[2].weight = 4;

instead of having it have three selections, you could add the weights 
and make it have 8 to choose from that point to the option.


choice[0]=0;
choice[1]=1;
choice[2]=1;
choice[3]=1;
choice[4]=2;
choice[5]=2;
choice[6]=2;
choice[7]=2;


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?

Much appreciated,

Jiri
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Determining probablity of a random pick.

2009-06-22 Thread Jiri

I see, that makes sense.

Cheers.

Jiri

Paul Andrews wrote:

Jiri wrote:

Thank you i understand it. Could u please elloborate on the last part
..You can optimise this by using an array of pointers..

Do you mean that this;

a weight=5
b weight=1
c weight=2

First calculated the range for each node.
A = 0 - 5
B = 6 - 7
C = 8 - 10

Then store in an array like so?
Array[0-5] = A
Array[6-7] = B
Array[8-10] = C

Last step get a random number between 0 - 10 = for instance 5

//result is then
return Array[5] as XML


If this is what you mean, would this approach not take a 'relatively' 
big amount of memory?
You don't have to store the node itself, just a reference to it, so no 
it wouldn't be a memory hog. It could even just be the numerical index 
of the node.


Paul




JIri

Paul Andrews wrote:

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
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders



___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders



___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders