score:5

Accepted answer

this should do the trick:

var random = new random();
var list = new list<string> {
    "a0.png",
    "b0.png",
    "b1.png",
    "b2.png",
    "b3.png", 
    "c0.png",
    "c1.png",
    "d0.png",
    "d1.png",
    "d2.png" 
};
var startingchar = "d";

var filteredlist = list.where(s => s.startswith(startingchar)).tolist();
console.writeline(filteredlist.count);

int index = random.next(filteredlist.count);
console.writeline(index);

var font = filteredlist[index];
console.writeline(font);

but the problem with the entire solution is that the smaller the resulting filtered list is the less likely you are to get really random values. the random class works much better on much larger constraints - so just keep that in mind.

score:4

random random = ...;
var itemsstartingwithc = input
    .where(x => x.startswith("c"))
    .tolist();
var randomitemstartingwithc =
    itemsstartingwithc.elementat(random.next(0, itemsstartingwithc.count()));

the call to tolist isn't strictly necessary, but results in faster code in this instance. without it, count() will fully enumerate and elementat will need to enumerate to the randomly selected index.


Related Query

More Query from same tag