Home > Software engineering >  C # output different random Numbers, but why will overflow? Error is infinite recursion or an infini
C # output different random Numbers, but why will overflow? Error is infinite recursion or an infini

Time:10-08

The HTML code is as follows, but the number range close to the number of elected, judge the same random number function will appear error, tip error is an infinite loop or infinite recursion, why? Elected number range is 30 and at the same time pick number is 29, there will be a mistake,

Using System;
Using System. Collections. Generic;
Using System.Com ponentModel;
Using System. The Data;
Using System. Drawing;
Using System. The Text;
Using System. Windows. Forms;

The namespace _1. _5
{
Public partial class Form1: Form
{
Public _click ()
{
InitializeComponent ();
}

Int the up, number=0;
Private void button1_Click (object sender, EventArgs e)
{
If (number!=0) label3. Text="";
Up.=the Convert ToInt32 (this) numericUpDown1) Value);//range, maximum
Number=the Convert. ToInt32 (this) numericUpDown2) Value);//number of random number
If (up & lt;=number)
{
MessageBox. Show (" pick the scope must be greater than pick number!" "Error");
return ;
}
Int a=new int [] [up].
A=getRandomNum (number, 0, up);//calls to generate a random number
for (int i=0; I & lt; Number; I++)
{
Label3. Text=label3. Text + "" + a [I] the ToString ();
}
}

Private void button2_Click (object sender, EventArgs e)
{
Close ();
}

//the following code from the book


//generate a random number
Public int [] getRandomNum (int num, int minValue, int maxValue)
{
The Random ra=new Random (unchecked ((int) DateTime. Now. Ticks));

Int [] arrNum=new int (num);//define array (size)

Int TMP=0;
for (int i=0; I & lt; Num. I++)
{
TMP=ra. Next (minValue and maxValue);//generate a random number assigned to TMP
ArrNum=[I] getNum (arrNum, TMP, minValue and maxValue, ra);
}
Return arrNum;
}
The Random ra=new Random (unchecked ((int) DateTime. Now. Ticks));
//determine whether the current generated random number and the number of repeat before - recursive algorithm
Public int getNum (int [] arrNum, int TMP, int minValue, int maxValue, Random ra)
{

For (int n=0; N & lt;=arrNum. Length - 1; N++)
If (arrNum [n]==TMP)
{
TMP=ra. Next (minValue and maxValue);/* * * * * * prompt infinite recursion here * * * * * * * */
GetNum (arrNum, TMP, minValue and maxValue, ra);
}
Return the TMP;//return to getRandomNum () function in the
}

In the error

CodePudding user response:

Collapsed in the pop-up dialog box, press the corresponding button to enter debugging press Alt + 7 key to view the Call Stack, namely "the Call Stack" from the inside to the following out of from the inner to outer function Call history, double-click a row to the cursor to the Call of the source code or assembly instruction, don't understand when double click on the next line, until we can read ,

CodePudding user response:

Recursive function must have a condition to exit, but if meet the conditions of risk very little, can lead to very deep, lead to a stack overflow error
  • Related