c# - Random number generator with no duplicates -
basically i'm creating program randomly generate 6 unique lottery numbers there no duplicates in same line, here code have far...
//generate 6 random numbers using randomiser object int randomnumber1 = random.next(1, 49); int randomnumber2 = random.next(1, 49); int randomnumber3 = random.next(1, 49); int randomnumber4 = random.next(1, 49); int randomnumber5 = random.next(1, 49); int randomnumber6 = random.next(1, 49); textbox1.text = randomnumber1.tostring(); textbox2.text = randomnumber2.tostring(); textbox3.text = randomnumber3.tostring(); textbox4.text = randomnumber4.tostring(); textbox5.text = randomnumber5.tostring(); textbox6.text = randomnumber6.tostring(); }
i'm getting random numbers there same number on same line, how make each number unique????
thanks in advance
you need store them in collection , each time pick new number need make sure it's not present already, otherwise need generate new number until find unique number.
instead of this, generate sequence between 1
, 49
, shuffle them , pick 6 number out of sequence, example:
var rnd = new random(); var randomnumbers = enumerable.range(1,49).orderby(x => rnd.next()).take(6).tolist();
wiki
Comments
Post a Comment