faqts : Computers : Programming : Languages : C

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

10 of 17 people (59%) answered Yes
Recently 4 of 10 people (40%) answered Yes

Entry

how to generate random integers from 0 to 10.000 without repeatition

Oct 11th, 2004 23:19
Ankit Sharma, Rosa Lopes,


Following code will do the work.
#######################################################################
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_NUM 10000
int main()
{
    int i, j;
    short int dup[10000];
    bzero(dup,sizeof(dup));
    srand( (unsigned)time( NULL ) );
    for (i = 0; i < MAX_NUM; i++) {
	  while(1)
	  {
	    j = (int) MAX_NUM * rand() / (RAND_MAX + 1.0);
	    if ( !dup[j])
	    {
	  	dup[j]=1;
		break;
	    }
	  }
        printf("%d\n", j);
    }
    return 0;
} 
#######################################################################