Mar 2, 2013

[C/C++] random number generator

// This program generates 6 random numbers between 1 and 45.

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

void main(void)
{
int i;
int t;
int ran[6];
srand(time(NULL));

printf("This program generates 6 random numbers between 1 and 45.\n\n\t");

for(i=1; i<=6; i++){
    ran[i] = rand() % 45 + 1;
    for(t=1; t<=5; t++){
        if(ran[i] == ran[i-t]) {
        ran[i]=rand()%45+1;
        }
    }
    printf("%d\t",ran[i]);
}
printf("\n\nPress 'Enter' key to continue...");
getchar();   // This command stops the cmd windows from closing automatically after running the code.
}

No comments:

Post a Comment