# I need to make a C program that shows 100 random numbers between 500 and 800 that are different, and I do not know how to make them different

1
``````#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);
}
}
``````

asked by GERMAN SOSA 24.05.2017 в 01:14
source

2

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);
}
}
``````