ID:179955
 
Quick question, since I've never seen it done, how would I go about, or what would I use to pick a random item from a list?
Foomer wrote:
Quick question, since I've never seen it done, how would I go about, or what would I use to pick a random item from a list?

I usually use
var/randomitem = listofitems[rand(1,listofitems.len)]

listofitems.len is the number of items in the list. If you don't declare the list properly*, it may say that it is a bad var. If so, try length(listofitems) instead.

rand(1,listofitems.len) returns a random number between 1 and the length of the list.

Then you just use that as an index into the list to get one random element in the list. :)


* To declare a list so that the len var is valid, make sure to declare it like this:
var/list/listofitems = list()

You can declare a list like this:
var/listofitems = list()

but then it doesn't have all the nice extras that a list variable does, like the len var.
In response to Shadowdarke
I find this technique so useful that I give it its own proc -- pickfrom(). I don't see how anyone could get by without it; I use it in nearly every serious project I've started.

-AbyssDragon
In response to AbyssDragon
I find this technique so useful that I give it its own proc -- pickfrom(). I don't see how anyone could get by without it; I use it in nearly every serious project I've started.

I call mine Listpick(). But I imagine the code within the proc is very similar, all one line of it. :)
Believe it or not, folks, I think the hard-coded pick() proc allows you to select from a list.

var/list/L = list()
var/X = pick(L)

I believe Deadron requested it a ways back, so I'm not sure if it's only Listed or whether it is actually implemented.

Anyway, I use the following:

proc/Pick(list/L)
if(!L) return null
else
return L[rand(1,L.len)]
In response to AbyssDragon
AbyssDragon wrote:
I find this technique so useful that I give it its own proc -- pickfrom(). I don't see how anyone could get by without it; I use it in nearly every serious project I've started.

-AbyssDragon

Useful, but I like the weighting abilities with prob() built into pick(), so I actually code the list out by hand into the item spawning proc in my game.

Of course now I'm imagining a system where I can assign objects a variable called rarity, and let a generic proc pick from objects, weighting on their rarity. :-)
In response to Skysaw
Of course now I'm imagining a system where I can assign objects a variable called rarity, and let a generic proc pick from objects, weighting on their rarity. :-)

Ooh! I have one of those! I'd need time to dig it up, but I could email it to you.
In response to Spuzzum
Spuzzum wrote:
Of course now I'm imagining a system where I can assign objects a variable called rarity, and let a generic proc pick from objects, weighting on their rarity. :-)

Ooh! I have one of those! I'd need time to dig it up, but I could email it to you.

I appreciate it, but I don't really need it at this time, since my game is functioning fine in this regard.

My thought actually was that it would be fun to code something like this, so I would have wanted to do it myself anyway. :-)
In response to Skysaw
Skysaw wrote:
Spuzzum wrote:
Of course now I'm imagining a system where I can assign objects a variable called rarity, and let a generic proc pick from objects, weighting on their rarity. :-)

Ooh! I have one of those! I'd need time to dig it up, but I could email it to you.

I appreciate it, but I don't really need it at this time, since my game is functioning fine in this regard.

My thought actually was that it would be fun to code something like this, so I would have wanted to do it myself anyway. :-)

Ah, OK. I understand.

My method was initially based on "fixing" Guy's old conveyance code to work with weighted-probabilities rather than ordered probabilities.

For example:

//slow west current
conveyProbs = list(15, 30, 50, 70, 100)
conveyDirs = list(NORTH, SOUTH, NORTHWEST, SOUTHWEST, WEST)

versus my method,

//slow west current
conveyProbs = list(10, 20, 40, 20, 10)
conveyDirs = list(SOUTH, SOUTHWEST, WEST, NORTHWEST, NORTH)
In response to Spuzzum
I thought I was the only one to use confusing parallel lists! (The element data in Darke Dungeon is stored in 4 or 5 lists) Hehee, now I don't feel so bad... though recently I've gotten away from them in favor of lists of specialized data objects.
In response to Shadowdarke
Shadowdarke wrote:
I thought I was the only one to use confusing parallel lists! (The element data in Darke Dungeon is stored in 4 or 5 lists) Hehee, now I don't feel so bad... though recently I've gotten away from them in favor of lists of specialized data objects.

Parallel lists are just vectors with different info, you know. =)