ID:159514
 
Is there an internal way to add something to a list with a preset probability for use in pick()? I need to be able to add things to a list with a certain probability as I loop through them, and then have it pick one. I can code something for it myself if there isn't already something internal for it, I'm just wondering... ex:
world<<pick(prob(10);"10",prob(20);"20")   //what can be done.

var/list/L=list()
for(var/v=10 to 20 step 10)
L.Add(prob(v);"[v]") //something like what I need
world<<pick(L)
Nah, it wouldn't make sense to have some probability feature built specifically into adding to lists or something. Provided I gather what you're trying to do correctly, um, can't you simply do this?
if(prob(x)) L += "[x]"
In response to Kaioken
Kaioken wrote:
Nah, it wouldn't make sense to have some probability feature built specifically into adding to lists or something. Provided I gather what you're trying to do correctly, um, can't you simply do this?
if(prob(x)) L += "[x]"


Possibly, but then I could end up with an empty list even if there was something to pick from.
What I'm doing more exactly: Looking at all enemies in oview(), adding them to a list, then choosing one to attack, enemies with a higher threat level get targeted more often.
In response to Falacy
Looks like what you need is a pickweight() proc, then.
In response to Kaioken
Kaioken wrote:
Looks like what you need is a pickweight() proc, then.

I didn't really read all that, but here's what I'm doing now: (in general)
proc/ThreatPick(var/list/L)
var/list/IndexList=list()
var/CurIndex=0
for(var/obj/O in L)
CurIndex+=1
for(var/i=1;i<=O.ThreatLevel;i++) IndexList+=CurIndex
return pick(IndexList)

Then using the returned index to reference an actual Troop obj in the original list. Threat levels vary from 1 to 10.
In response to Falacy
Falacy wrote:
I didn't really read all that

You just need to press CTRL-F and read some bits about pickweight(), not the whole article.
In response to Kaioken
Kaioken wrote:
You just need to press CTRL-F and read some bits about pickweight(), not the whole article.

Done already, probably more efficient Lummox's way; since it may loop less depending on list size and threat level.