#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main() {
int N,a;
srand(time(NULL));
for(a=1;a<=100;a++) {
N=rand() % 500 +300;
printf("%d.- %d \n",a,N);
}
}
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main() {
int N,a;
srand(time(NULL));
for(a=1;a<=100;a++) {
N=rand() % 500 +300;
printf("%d.- %d \n",a,N);
}
}
Your algorithm for generating random numbers in the range (500-800) is incorrect:
X = rand() % 500
gets a number in the range (0,500) N = X + 300
gets a number in the range (300-800) You should invest the values:
N = (rand() % 300) + 500;
// ^^^ ^^^
// rango offset
On the other hand, to guarantee that the numbers are different you have to store them somewhere ... and for that, nothing better than an arrangement:
#define MAX_NUMEROS 100
int numeros[MAX_NUMEROS] = { 0 };
for(a=0;a<MAX_NUMEROS;a++)
{
// generamos un numero
N = (rand() % 300) + 500;
// comprobamos si el numero ya esta siendo utilizado
for( int i=0; i<a; i++ )
{
if( numeros[i] == N )
{
N = 0; // Numero repetido, lo reseteamos
break;
}
}
if( N != 0 ) // Si el numero ha pasado las validaciones, lo insertamos
{
numeros[a] = N;
printf("%d.- %d \n",a,N);
}
}
Instead of painting them in the for, save all the numbers generated in an Array, and every time you generate a new one you will go through that and I will verify that it does not exist, if it does not exist, keep it in the array, if there are raisins the next, so until the array has 100 numbers.
this can help you solve your problem: