Thread: srand confusion

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    10

    srand confusion

    Following along with some C programming training, the instructor show us a solution to a lesson that will randomly generate array values and then construct them in a barchart. Focusing on the part that generates the random numbers, the instructor said to setup a function like the one at line 25. I noticed I could not see anything that srand was doing in the for loop. I then commented it out and get the same exact result when I compile. How or why is the srand function important?

    Code:
    //Function prototypes
    void showBarChart(void);
    void showOneBar(int barLength);
    
    #include <time.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define DATA_SIZE 10
    
    //Function prototypes
    void showBarChart(void);
    void showOneBar(int barLength);
    void setDataValues(void);
    
    int dataSet[DATA_SIZE];
    
    int main(void) {
        setDataValues();
        showBarChart();
        return EXIT_SUCCESS;
    }
    
    //---- FUNCTION DEFINITIONS ----
    void setDataValues(void){
        /*
         * Generates a set of random data points between 0 and 49
         * and populates the dataSet array
         */
        srand(time(NULL));
        for(int i=0; i<DATA_SIZE; i++){
            dataSet[i] = rand() % 50;  //random integers between 0 and 49
        }
    }
    
    void showBarChart(void){
        /*
         * Loops through the dataSet array, reads each data point
         * and calls the showOneBar function with that value
         */
        for(int i=0; i<DATA_SIZE; i++){
            showOneBar(dataSet[i]);
        }
    }
    Last edited by pctechtv; 09-01-2015 at 02:34 PM. Reason: Could not get line number until full post

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    These FAQs should answer your question:
    FAQ > Generate random numbers? - Cprogramming.com
    Question 13.17

    If you're still not clear on something, feel free to ask.

  3. #3
    Registered User
    Join Date
    Aug 2015
    Posts
    10
    Quote Originally Posted by Matticus View Post
    These FAQs should answer your question:
    FAQ > Generate random numbers? - Cprogramming.com
    Question 13.17

    If you're still not clear on something, feel free to ask.
    Yes, I am still not clear on what srand is doing? It seems like it could be left out. Thanks

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by pctechtv View Post
    Yes, I am still not clear on what srand is doing? It seems like it could be left out. Thanks
    Computers cannot truly generate random values, but they can appear to by using certain formulas and algorithms.

    "rand()" will always start from the same place, and follow the same formula for each successive call to "rand()". This means that each time such a program is run, the output is exactly the same as the previous run. This is hardly "random" behavior.

    You can use "srand()" to seed the random number generator. This means that the RNG will start from a "different place" than if the RNG was not seeded.

    However, using the same seed will get you the same results. To get different results each time the program is run (i.e. the appearance of randomness), you need to seed the RNG with a value that varies between runs. One such value is the current time, as illustrated in the FAQ I linked above.

  5. #5
    Registered User
    Join Date
    Aug 2015
    Posts
    10
    Got it! I missed it the first time, but caught it the second time reading it, by then you graciously answered with another good explanation. This helps a lot! Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with srand()
    By ~C_Student~ in forum C Programming
    Replies: 10
    Last Post: 12-22-2009, 10:55 AM
  2. srand()
    By bikr692002 in forum C++ Programming
    Replies: 35
    Last Post: 09-03-2006, 09:13 PM
  3. srand()
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 07-06-2002, 05:13 PM
  4. When to srand()?
    By Imperito in forum C++ Programming
    Replies: 1
    Last Post: 05-12-2002, 12:20 AM
  5. srand()
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 03-19-2002, 11:10 AM

Tags for this Thread