I have a college exercise that is to generate a random primal number, if that number is not prime it must be generating another random one until one is, but it always falls into numbers that are not prime and are in infinite loop, the What am I doing wrong in the code? Thanks in advance for your help.
Follow the code below:
static void Main(string[] args)
{
int p, div = 0;
bool nPrimo = false;
Random r = new Random();
p = r.Next(1, 100);
while (nPrimo == false)
{
for (int i = 1; i <= p; i++)
{
if (p % i == 0)
{
div++;
}
}
if (div == 2)
{
nPrimo = true;
}
else
{
p = r.Next(1,100);
}
}