Random number
-
i want to generate two random numbers , assume: Random rd = new Random(); int m1 = rd.Next(1,x); int m2 = rd.Next(1,y); i want to generate them for multiple times provided that the two numbers m1 & m2 do not repeated. thanks
-
i want to generate two random numbers , assume: Random rd = new Random(); int m1 = rd.Next(1,x); int m2 = rd.Next(1,y); i want to generate them for multiple times provided that the two numbers m1 & m2 do not repeated. thanks
Do you mean you want to keep generating them until they are not equal to each other? Or you're going to do something like fill an array up with random numbers but you don't want any to be the same as another number in the array? If you're wanting to generate them until they aren't equal just a simple while loop will work:
Random rd = new Random(); int m1 = rd.Next(1,x); int m2 = rd.Next(1,y); int iInfLoopChk = 0; //prevents while loop from locking your program in infinite loop while( m1 == m2 && iInfLoopChk < 5000 ) { m1 = rd.Next(1,x); m2 = rd.Next(1,y); iInfLoopChk++; }
^You'll need some logic if you're worried about the infinite loop that tells you both numbers cannot be anything other than equal to eachother. (Like if m1 is always 0 and m2 is always 0 due to logic error or special circumstance) -
i want to generate two random numbers , assume: Random rd = new Random(); int m1 = rd.Next(1,x); int m2 = rd.Next(1,y); i want to generate them for multiple times provided that the two numbers m1 & m2 do not repeated. thanks
Try Random rd = new Random(); int m1 = rd.Next(1, x); int m2; do { m2 = rd.Next(1, y); }while(m1 != m2); Sanjay Sansanwal www.sansanwal.com
-
i want to generate two random numbers , assume: Random rd = new Random(); int m1 = rd.Next(1,x); int m2 = rd.Next(1,y); i want to generate them for multiple times provided that the two numbers m1 & m2 do not repeated. thanks
Not that. The case of m1 = m2 is not a problem, but i want the pair of two numbers (m1 and m2 together) not to be repeated. for example: if x=2 and y=3, this pair of x and y not to be repeated, because i want to use them as one unit.
-
Not that. The case of m1 = m2 is not a problem, but i want the pair of two numbers (m1 and m2 together) not to be repeated. for example: if x=2 and y=3, this pair of x and y not to be repeated, because i want to use them as one unit.
struct XY {
int X, Y;
}now just dump that in a hashtable, and check if the valuetype is in there. Note this will only work for ordered pairs. You will need to override gethascode and equals if u want them to be equal unordered or whichever way you fancy. hth :) top secret
Download xacc-ide 0.0.3 now!
See some screenshots