c# - How can I convert this to return multiple items? -



c# - How can I convert this to return multiple items? -

i have list of games, need select randomly, day of week, next code works 1 game.

var gameoftheday = games.allactive[(int)(datetime.today.gethashcode()) % games.allactive.count()];

what need homecoming more 1 game, randomized, based on x ( x in case above day of week, alter specific string )

i need create semi-random generation of items.

semi - since want feed keyword, , same results per keyword

random - since need create game list random

for example, every time come in page title "hello", see same games, selected specificly keyword games list based on keyword "hello".

in same way gameoftheday works.

you can utilize linq this:

int limit = 10; string keyword = "foo"; random rng = new random(keyword.gethashcode()); var gamesoftheday = games.orderby(x => rng.next()).take(limit);

however, have overhead sort. if have lot of games compared amount you're selecting—enough sort might expensive, , plenty it's safe maintain retrying in event of collision—manually doing might faster:

hashset<game> gamesoftheday = new hashset<game>(); while(gamesoftheday.count < limit && gamesoftheday.count < games.length) { int idx = rng.next(games.length); gamesoftheday.add(games[idx]); }

note in either case random constructed seed dependent on keyword, order same every time keyword. combine hashes of current datetime , keyword unique random sequence day-keyword combination.

c# list random

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -