Thread: generating 10 unique random numbers in C

  1. #1
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136

    Question generating 10 unique random numbers in C

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    {
        int i=0,j=0,n=0,a[10],flag=-1;
        for(i=0;i<10;i++)
        {
            a[i]=-1;
        }
        while(i<10)
        {
             n=rand()%10;
             for(j=0;j<10;j++)
             {
                   if(n==a[j])
                   {
                       flag=0;
                       break;
                   }
                   else
                  {
                       flag=1;
                  }
             }
             if(flag==1)
             {
                  a[i]=n;
                  i++;
              }  
         }
        for(i=0;i<10;i++)
        {
             printf("%d\n",a[i]);
         }  
    }
    when i compile and run the above code I get -1.
    it should generate 10 unique random numbers between 10, but it does not...

    i use gcc this is the output
    Code:
    $ gcc random.c
    $ ./a.out 
    -1
    -1
    -1
    -1
    -1
    -1
    -1
    -1
    -1
    -1

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Of course it does. What is the value of "i" after the first for-loop?

    Also, you don't need to set flag = 1 each time in the loop for(j) - set it before the loop, and remove the else-branch.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    its really pathetic of me...

    please mods lock and delete this thread...

    i didnt set i to 0 before while loop

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    And you should seed the generator really... with srand().

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It might take a while to get the 10th slot filled (random guesses, with only one answer).

    Start with an array filled with 1 to 10, then use a shuffle.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. drand48(): generating pseudo random numbers in a range
    By Isolda_ in forum C Programming
    Replies: 2
    Last Post: 08-31-2007, 11:21 AM
  2. generating random numbers using srand
    By skyrnnr87 in forum C Programming
    Replies: 5
    Last Post: 11-24-2006, 06:45 AM
  3. calculating the variance of random numbers
    By Unregistered in forum C Programming
    Replies: 18
    Last Post: 11-22-2004, 08:16 AM
  4. Generating Random Numbers
    By pizzapie in forum C++ Programming
    Replies: 17
    Last Post: 09-23-2004, 02:46 AM
  5. Generating Random Numbers
    By knight543 in forum C++ Programming
    Replies: 3
    Last Post: 01-11-2002, 06:55 PM