C# - Returning a Unique Random Number -
this question has reply here:
random number generator generating 1 random number 8 answersi have searched hours , i'm not getting it. don't seem know how homecoming values using fisher-yates , many ways listed. i'm dying here.
i can randomnumber, reused on , over. need unique everytime when returned (or tend think possible).
i need help understanding should do, why each part does, , stuff dummies. works:
private int randomnumber(int min, int max) { random random = new random(); homecoming random.next(min, max); }
and i'm putting , working (but not unique random numbers used)... included felt needed looked @ , positioned:
private void computersturn() { control.controlcollection coll = this.controls; foreach (control c in coll) { if (...) { if (...) { if (...) { if ((c.name == "btn" + convert.tostring(randomnumber(1,9)) && (c.enabled != false) )) { if (...) { //code here } } } } } } }
again, randomnumber works...but it's not unique. wish larn how homecoming unique number (if possible).
the way generate unique numbers random
define in class this:
public static class randomgenerator { private static readonly random _random = new random(); public static int genrand(int x, int y) { homecoming _random.next(x, y); } }
in case seek utilize code way:
private void computersturn() { control.controlcollection coll = this.controls; foreach (control c in coll) { if (...) { if (...) { if (...) { if ((c.name == "btn" + convert.tostring(randomgenerator.genrand(1, 9)) && (c.enabled != false) )) { if (...) { // code here } } } } } } }
c#
No comments:
Post a Comment