Random...

Written on Monday, March 05, 2007 by Unknown

There was an assignment (or rather programming quiz) given for the System Software lecture..this is the first part of the program. Eventually this program works..bt is not efficient enough, need to refine it..



Am not a too great C programmer..(i would rather be a Java guy..) had to do some search over google to know how to generate random numbers..



here goes the code..



#include <stdio.h>

#include <stdlib.h>

#include <time.h>



const int min = 1;

const int max = 20;



int generate_unique_random();



int main()

{

     int ran;

     int j=1;

     while(j<21)

     {

        ran=generate_unique_random();

        printf("Number %d is %d,\n",j,ran);

        j++;

     }

}



int generate_unique_random()

{

     static int arr[21];

     time_t seconds;

     time(&seconds);

     srand((unsigned int) seconds);

     int number;

     while(1)

     {

        number = rand() % (max - min + 1) + min;

        if (arr[number]!=1)

        {

           arr[number]=1;

           return number;

        }

     }

}




powered by performancing firefox

If you enjoyed this post Subscribe to our feed

1 Comment

  1. Avinash Meetoo |

    Not bad...

    What about a O(1) version?

     

Post a Comment