Public int [] getRandomNum (int num, int minValue, int maxValue)
{
The Random ra=new Random (unchecked ((int) DateTime. Now. Ticks));
Int [] arrNum=new int (num);
Int TMP=0;
for (int i=0; i
TMP=ra. Next (minValue and maxValue);//random access
This. TxtResult. Text +="temp:" + TMP;//output initial random number
ArrNum=[I] getNum (arrNum, TMP, minValue and maxValue, ra);//remove the value assigned to the array
}
Return arrNum;
}
Public int getNum (int [] arrNum, int TMP, int minValue, int maxValue, Random ra) {
int n=0;
While (n<=arrNum. Length - 1)
{
This. TxtResult. Text +=", "n + n +" "; How many times have//output cycle
If (arrNum==TMP) [n]//use cycle to judge whether there is a repeated
{
TMP=ra. Next (minValue and maxValue);//to random access,
This. TxtResult. Text +="tmp1:" + TMP + "\ t";//internal distribution of random number again
GetNum (arrNum, TMP, minValue and maxValue, ra);//recursive: if the number of Numbers and has made out a repeat to randomly access,
}
This. TxtResult. Text +="TMP:" + TMP + "";//every TMP values
N++;
}
This. TxtResult. Text +="return:" + TMP + "";//return value
Return the TMP;
}
I call getRandomNum,1,10 (2), the output not to repeat random number between 1 and 10 2, occasionally appear two duplicate values, according to the output of the internal use of parameter values, three layers of recursive TMP values will restore the original random number, from the code I didn't see what's the problem, who can help take a look at what went wrong?
The first random number 9, take the second random number, the original random number is 9, to generate a random number is 9, again to regenerate the is 7, the judgment after a few repetitions, the random number changed suddenly back to the original 9,
I: 0 temp: 9 n: 0 TMP: 9 n: 1 TMP: 9 return: 9
I: 1 temp: 9 n: 0 tmp1:9 n: 0 tmp1:7 n: 0 TMP: 7 n: 1 TMP: 7 return: 7 TMP: 7 n: 1 TMP: 7 return: 7 TMP: 9 n: 1 TMP: 9 return: 9
Recursion is no problem on the second floor, the first random number 6, take the second random number, the original random number is 6, to generate a random number or 4, don't repeat and then return the value 4 directly,
I: 0 temp: 6 n: 0 TMP: 6 n: 1 TMP: 6 return:
I: 1 temp: 6 n: 0 tmp1:4 n: 0 TMP: 4 n: 1 TMP: 4 return: 4 TMP: 4 n: 1 TMP: 4 return: 4
CodePudding user response:
Try not to repeat the random number generated,public int [] getRandomNum1 (int num, int minValue, int maxValue)
{
If (maxValue - minValue & lt; Num)
{
Throw new Exception (" set incorrectly ");
}
The Random ra=new Random ();
HashSetHashInt=new HashSet (a);
for (int i=0; I & lt; Num. I++)
{
While (true)
{
The int value=https://bbs.csdn.net/topics/ra.Next (minValue and maxValue);
if (! HashInt. The Contains (value))
{
HashInt. Add (value);
break;
}
}
}
Return hashInt. ToArray ();
}
CodePudding user response:
Code is too long,,,,If just random 1-10 of two not repeated random number,
So directly as follows, train of thought is to put the 1-10 random arrangement, take out the first two,
var q=Enumerable. Range (1, 10) OrderBy (x=& gt; Guid. NewGuid ()). Take (2);