Posts ID:159514 Favorites Creations Medals
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<
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 (#1)
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.
 #3 Feb 24 2009, 7:47 pm In response to Falacy (#2) Looks like what you need is a pickweight() proc, then.
In response to Kaioken (#3)
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.
 #5 Feb 24 2009, 8:04 pm In response to Falacy (#4) 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.
 #6 Feb 24 2009, 8:06 pm In response to Kaioken (#5) 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.