C++

How to Use Srand Function in C++

In this article, we will be discussing how we can use the srand() function in C++ while writing programs in Ubuntu 20.04. The srand() function is the seed that the rand() function uses to generate random numbers. Both these functions are defined in the stdlib header. As we use the srand() function to set the seed in the rand() function, it is linked to the rand() function. The srand() function takes the value of the seed in an unsigned integer type. The seed value is the starting place for the rand() function’s random number generation, for example, srand(5). No value is returned by the srand() function. The default seed value of the srand function is set as “1”. So, if we do not call the srand() function before the rand() function, it will be the same as writing “srand(1)”.

Understanding the relation between rand() and srand() function:

When we use the rand() function in our code, we always get the same random number whenever we compile it. Let us look at an example where we will better understand how the rand function works without srand().

Using rand() function without the srand() function in Ubuntu 20.04 :

Open the terminal and make a .cpp file by writing and executing the command “touch” with the filename and extension of .cpp. Then, Locate and open your .cpp file. Now write a simple code for generating random numbers without using the srand() function.

The file should be saved and then closed. Open the terminal again and compile the file with this command “g++” along with your file name and extension. This will generate an output for your file after compiling, usually with the extension of “.out.” Now execute the output file by writing this command “./” along with your Output filename

As you can see in the above example, the first and second outputs are the same because we did not use the srand() function. So even if we repeat the process multiple times, the output will be the same because the seed value is by default set to 1.

We will look at how the rand() function will work with the srand() function.

Rand() function with the use of srand() function in Ubuntu 20.04:

Open the terminal and make a .cpp file by writing and executing the command “touch” with the filename and extension of .cpp. Then, Locate and open your .cpp file. We will now write a code for generating random numbers and use the srand() function with different seed values.

The file should be saved and then closed. Open the terminal again and compile the file with this command “g++” along with your file name and extension. This will generate an output for your file after compiling, usually with the extension of “.out.” Now execute the output file by writing this command “./” along with your Output filename.

Now, we can see that using the srand() function with different seed values gives a different random number. So, if we keep changing the seed value that is the parameter of the srand() function, the random numbers will always be different from one another. The value of seed is set as “1” in the first output line, so the random number generated by this seed value is 1804289383, and when we change the seed value to 5, the random number generated is now changed to 590011675, so this change happened because of the difference in the seed value.

After looking at this example, we can conclude that the rand() function without the srand() function does not perform up to the standard of the PRNG (Pseudo Random Number Generator) program, which generates random numbers as per the seed value accreditation, in the C++ language. This program includes the rand() and srand() functions to generate random numbers in C++ programming. So, we should always use the srand() function while using the rand() function to generate distinct random numbers.

The srand() number with different seed values keeps changing the generated random numbers, thus making the rand() function more efficient. But we must keep changing the seed value every time as the seed value must be given before calling the rand() function. So the best way to keep doing that is using the time(0) function, which gives the exact time in seconds in the unsigned integer form the same as the parameter of the srand() function. So, the output of time(0) will always keep changing, and thus the output of the rand() function will also keep changing every second.

Using srand() function with time(0) as the parameter:

As we discussed earlier that the seed value of the function is set as “1,” so we must change the seed value constantly and before the rand() function is called. These points are vital for the efficiency of generating random functions, so we have to think of such a way that reduces the thought process of the coder and the processing power of the device as well. This issue is resolved by the time() function in the C++ language, which gives the exact time at the given moment at which you are executing the task in your device, also known as the time stamp. So, if we add the time(0) function as the parameter of the srand() function, that is its seed value, it will give the total time in seconds as an integer to the srand() function and will keep changing at every time we use it. The time() function is defined in the ctime header, so we will have to add this in our code. Now we will implement this on our code and try to understand better how the srand() function works with the time() function as its parameter.

Using srand() function with time function as its parameter in Ubuntu 20.04:

Open the terminal and make a .cpp file by writing and executing the command “touch” with the filename and extension of .cpp. Then, Locate and open your .cpp file. We will now write a code for generating random numbers and use the time() function to provide different seed values for distinct randomly generated numbers.

The file should be saved and then closed. Open the terminal again and compile the file with this command “g++” along with your file name and extension. This will generate an output for your file after compiling, usually with the extension of “.out.” Now execute the output file by writing this command “./” along with your Output filename

As we can see that after executing the files multiple times, the random number is changing at every execution, so we have achieved efficiency by this method, and we will keep on getting different values every time we execute the srand() function with time(0) as its parameter.

Conclusion:

In this article, we have learned the relationship between the rand() and srand() function and how the srand() function helps the rand() function to generate random numbers efficiently by the use of the time() function. We implemented all these concepts’ examples in ubuntu and gave a detailed stepwise elaboration on how to perform them on Ubuntu 20.04.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.