Thread: Creating a random array

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    10

    Creating a random array

    HI,
    I need to create a random array of 500 numbers between 1 and 1000. I understand how to create an array of 500 numbers but I can't seem to make it random. Can anyone help me.

    Thank You.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    FAQ > How do I... (Level 1) > Generate random numbers?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    10
    I have the random array but I can't figure out how to make the numbers range from 1 to 1000.
    here is the code.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    #define ARY_SIZE 500
    
    int bldarray (int randN[]);
    void printArray(int data[], int size);
    
    int main(void)
    {
      int randN[ARY_SIZE];
      int R;
    
      R = bldarray(randN);
      printArray(randN, ARY_SIZE);
    
      return(0);
    }
    
    int bldarray (int randN[])
    {
      int i;
      int randNo;
      int h[ARY_SIZE] = {0};
    
      for(i = 0; i < ARY_SIZE; i++)
      {
        do
        {
          randNo = rand() % ARY_SIZE;
        }while (h[randNo] == 1);
        h[randNo] = 1;
        randN[i] = randNo;
    
      }
     return;
    }
    
    
    void printArray(int data[], int size)
    {
      int i;
      int numP = 0;
    
      for(i = 0; i < size; i++)
      {
        numP++;
        printf("%d\n", data[i]);
    
      }
      return;
    }
    Can Anyone Help?
    Last edited by pinkpenguin; 11-15-2005 at 04:41 PM.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Try reading the whole FAQ entry that Dave pointed you to. There's a whole section that starts with "To generate numbers in a specific range, you have to use some simple maths."
    If you understand what you're doing, you're not learning anything.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    First, you'll want this:
    Code:
    int main(void)
    {
      int randN[ARY_SIZE];
      int R;
      srand(time(0));
    And if you want a range of 1000, this will get you 0-999:
    Code:
    int bldarray (int randN[])
    {
      int i;
      int randNo;
      int h[ARY_SIZE] = {0};
    
      for(i = 0; i < ARY_SIZE; i++)
      {
        do
        {
          randNo = rand() % 1000;
        }while (h[randNo] == 1);
        h[randNo] = 1;
        randN[i] = randNo;
    
      }
     return;
    }
    Fix the things your compiler warns you about, too.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making a random array of integers
    By Vidak in forum C# Programming
    Replies: 2
    Last Post: 11-09-2007, 06:00 AM
  2. Create random array
    By Hunter_wow in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2007, 05:29 AM
  3. Creating array of objects w/o default constructor
    By QuestionC in forum C++ Programming
    Replies: 19
    Last Post: 05-02-2007, 08:03 PM
  4. Creating an array of pointers to int[]
    By OkashiiKen in forum C Programming
    Replies: 3
    Last Post: 09-29-2006, 06:48 PM
  5. creating a dynamic 2d array?
    By der dom in forum C Programming
    Replies: 11
    Last Post: 09-28-2004, 05:16 PM