c# - How can I extract 3 random values from an array? -
i build myobject array :
myobject[] myobject = (from myobject varobj in myobjects select varobj).toarray();
and now, i'd extract 3 random myobject array! how can on c#?
of course, if array lenght <3 need extract objects!
myobject[] myobject = ...; int upper = 1; if (myobject.length > 1) { random r = new random(); upper = math.min(3, myobject.length); (int = 0; < upper; i++) { int randind = r.next(i, myobject.length); myobject temp = myobject[i]; myobject[i] = myobject[randind]; myobject[randind] = temp; } }
now take elements of array 0 upper
.
Comments
Post a Comment