c# - Randomize items and their locations in a list of length 3 -
so have list of fruit objects in list<fruit> allfruits (contains 60 or fruit objects) , have empty list of length 3 house selection of 3 fruits list<fruit> fruitselection.
this part of quiz game, on each round, positions of fruits need different (randomised).
in c#, how can select random fruit allfruits place in random position in fruitselection , same 2 fruits, , ensure not duplicates , placed in @ position not taken within fruitselection?
any appreciated.
ps. fruit has unique name property.
use .net random class. can use param orderby in linq. here code (use linqpad):
var fruits = new list<string> { "apple", "orange", "mango" }; var r = new random(); var neworderfruits = fruits.orderby(x => r.next()).tolist(); neworderfruits.dump(); you see using list of 3 strings can substitute list of allfruits fruits string list , different order each time.
please remember not use new instance of random each time!
Comments
Post a Comment