probability problems

i'm writing some code where i have three doubles that represent the probability of doing three actions. the choice between the three actions is random, but biased toward the more probable of the actions.

at the moment the way i choose an action is order the probabilities in descending order, and generate a random number, if that number is less than the most probable, then i choose that action, else if it is less than the most probable plus the second most probable then that action, else the last action.

this seems to work fine, however i want to change the program so that there can be between 2 and 5 probabilities and actions. i realise i could modify the above method to do this, but is seems a bit hacky, and i was wondering if anyone knew a better way to choose between actions

thanks

alex

[834 byte] By [alex-lorenzia] at [2007-11-27 9:00:31]
# 1
First, decide on your probabilities. Let's say:A: 10%B: 25%C: 50%D: 15%From this point, roll a random number between 1 and 100 inclusively and any result between [1-10] should result in A, [11-35] => B, [36, 85] => C, [86, 100] => D.
Dalzhima at 2007-7-12 21:29:15 > top of Java-index,Java Essentials,Java Programming...
# 2

Put the probablilities into an array, and change your method to take a double[]. Like Dalzhim said, choose a random number 1-100. For each probablity in the array, check if the random number is in it's range. The range for a given probability is the sum of all the previous probablilities, to that sum plus the current probablility. i.e., if your probs. are 10, 35, 55, the range of the first is 0 thru 10, the range of the second is 10 thru 10+35, the third is 10+35 thru 10+35+55.

hunter9000a at 2007-7-12 21:29:15 > top of Java-index,Java Essentials,Java Programming...
# 3
cheers guys, what you've suggested makes sense.... think its basically the way that i'm doing it, but without sorting the numbers. not sure why i thought i had to...alex
alex-lorenzia at 2007-7-12 21:29:15 > top of Java-index,Java Essentials,Java Programming...
# 4
Both of the above limit the resolution. To me the simplest solution is to put the sorted transition probabilities into an array and do a binary search using based on the value of Random.getDouble() (or is it next Double()).
sabre150a at 2007-7-12 21:29:15 > top of Java-index,Java Essentials,Java Programming...
# 5
in this case sabre, if i sort the probabilities 10, 15, 25, 50, and do a binary search on the random value 35, it will return to me the index of 25, correct?
alex-lorenzia at 2007-7-12 21:29:15 > top of Java-index,Java Essentials,Java Programming...
# 6

> in this case sabre, if i sort the probabilities 10,

> 15, 25, 50, and do a binary search on the random

> value 35, it will return to me the index of 25,

> correct?

That would only give a 5% chance of the second prob, and a 10% chance of the third. The first would always be correct, and the fourth one is correct by chance in this example, but it would be 100-the correct prob instead.

hunter9000a at 2007-7-12 21:29:15 > top of Java-index,Java Essentials,Java Programming...
# 7

> in this case sabre, if i sort the probabilities 10,

> 15, 25, 50, and do a binary search on the random

> value 35, it will return to me the index of 25,

> correct?

The value is correct but the index is what you want. The first value in the array has to be zero and the last 1.0 .

P.S. I consider probabilities to be in the range 0.0 to 1.0 inclusive - not as % values.

Message was edited by:

sabre150

sabre150a at 2007-7-12 21:29:15 > top of Java-index,Java Essentials,Java Programming...
# 8

> > in this case sabre, if i sort the probabilities

> 10,

> > 15, 25, 50, and do a binary search on the random

> > value 35, it will return to me the index of 25,

> > correct?

>

> The value is correct but the index is what you want.

> The first value in the array has to be zero and the

> last 1.0 .

>

> P.S. I consider probabilities to be in the range 0.0

> to 1.0 inclusive - not as % values.

>

> Message was edited by:

> sabre150

How does converting the percentages to decimals change anything? I don't get how your binary search is supposed to work. What values would you put in the array if the probablilities of the four things were 10, 15, 25 and 50% ?

hunter9000a at 2007-7-12 21:29:15 > top of Java-index,Java Essentials,Java Programming...
# 9

> How does converting the percentages to decimals

> change anything? I don't get how your binary search

> is supposed to work. What values would you put in the

> array if the probablilities of the four things were

> 10, 15, 25 and 50% ?

You seem to be looking for a fight. I'm a pacifist so I can't oblige.

sabre150a at 2007-7-12 21:29:15 > top of Java-index,Java Essentials,Java Programming...
# 10

> > How does converting the percentages to decimals

> > change anything? I don't get how your binary

> search

> > is supposed to work. What values would you put in

> the

> > array if the probablilities of the four things

> were

> > 10, 15, 25 and 50% ?

>

> You seem to be looking for a fight. I'm a pacifist so

> I can't oblige.

No! Not at all! I'm just confused :) I didn't mean to sound aggressive.

I don't get how the binary search is supposed to work. Would the array contain {0, .1, .15, .25, .5, 1.0}

for the example I asked about?

hunter9000a at 2007-7-12 21:29:15 > top of Java-index,Java Essentials,Java Programming...
# 11

> > > How does converting the percentages to decimals

> > > change anything? I don't get how your binary

> > search

> > > is supposed to work. What values would you put

> in

> > the

> > > array if the probablilities of the four things

> > were

> > > 10, 15, 25 and 50% ?

> >

> > You seem to be looking for a fight. I'm a pacifist

> so

> > I can't oblige.

>

> No! Not at all! I'm just confused :) I didn't mean to

> sound aggressive.

>

> I don't get how the binary search is supposed to

> work. Would the array contain {0, .1, .15, .25,

> .5, 1.0}

for the example I asked about?

Given a random value (say 0.1612345) then the binary search would result in the index of the values less than or equal to the random value. In this case 2 which is the value the OP needs.

sabre150a at 2007-7-12 21:29:15 > top of Java-index,Java Essentials,Java Programming...
# 12

> Given a random value (say 0.1612345) then the binary

> search would result in the index of the values less

> than or equal to the random value. In this case 2

> which is the value the OP needs.

Let me make sure I'm understanding. If the random value is anything >= .15 and < .25, then the thing associated with the 15% prob. gets selected? And anything >= .1 and < .15 gets associated with the 10%.

I was assuming that the OP meant the given values to be the probabilities that the associated action would happen ie. action A has a 10% chance, B has 15%, etc. That would only give A a 5% chance and B a 10% chance.

hunter9000a at 2007-7-12 21:29:15 > top of Java-index,Java Essentials,Java Programming...
# 13

> > Given a random value (say 0.1612345) then the

> binary

> > search would result in the index of the values

> less

> > than or equal to the random value. In this case 2

> > which is the value the OP needs.

>

> Let me make sure I'm understanding. If the random

> value is anything >= .15 and < .25, then the thing

> associated with the 15% prob. gets selected? And

> anything >= .1 and < .15 gets associated with the

> 10%.

>

> I was assuming that the OP meant the given values to

> be the probabilities that the associated action would

> happen ie. action A has a 10% chance, B has 15%, etc.

> That would only give A a 5% chance and B a 10% chance.

That is why I said the 'transition' values. If one wants

A 10%

B 15%

C 1%

D 22%

E the rest

then the array would contain

0.0

0.1

0.25

0.26

0.48

1.0.

sabre150a at 2007-7-12 21:29:15 > top of Java-index,Java Essentials,Java Programming...
# 14
Gotcha, that makes sense now, thanks!
hunter9000a at 2007-7-12 21:29:15 > top of Java-index,Java Essentials,Java Programming...