Understanding C++ rand() and srand() function

Filed Under: C++
C Rand() And Srand() Function

Hey, folks! In this article, we will be focusing on the generation of random numbers using C++ rand() and srand() function.


What is C++ rand() function?

The C++ rand() function is used to generate random numbers at compile time. As soon as the system encounters the rand() function, it generates a random set of numbers as output.

Syntax:

rand()

Using rand() function, when we compile and run the program, the system generates the same set of random numbers.


Generating Your First Random Number using rand()

#include<iostream>
#include<cstdlib>
using namespace std;

int main()
{
    int random_num = rand();
    cout<<random_num;
}

Every time you try to run the above code, it will yield the same output random number as below.

Output:

1804289383

Generating Multiple Random Numbers using rand()

#include<iostream>
#include<cstdlib>
using namespace std;

int main()
{ 
   for(int rand_num = 0; rand_num<10; rand_num++) 
        cout<<rand()<<endl;
    return 0; 
}

In this example, we have generated 10 random numbers using a for loop along with rand() function, respectively.

Output:

1804289383
846930886
1681692777
1714636915
1957747793
424238335
719885386
1649760492
596516649
1189641421

What is C++ srand() function?

C++ srand() is considered as Set seed for rand() function.

The srand() function along with rand() function is used to generate random numbers at compile time.

C++ srand() function sets the beginning/starting point for the execution of random numbers by the rand() function. Usually, the srand() function is set and called once before the execution of rand() function in the program.

Syntax:

srand(time())

The time() function returns the current time of the system. So, every time we execute the code, the value passed to the seed function would change according to the time. Thus, a new random set of numbers get generated whenever we execute the program, unlike rand() function.

The standard value to the srand() function is time(0).


Random Numbers With Time as the Seed

#include<iostream>
#include<cstdlib>
using namespace std;

int main()
{
   srand(time(0)); 
   int rand_num = rand();
   cout<<rand_num;
}

Every time we execute the above code, a new random number is generated.

Output:

153261608

Generating Multiple Random Numbers Using srand()

#include<iostream>
#include<cstdlib>
using namespace std;

int main()
{  
   srand(time(0)); 
   for(int rand_num = 0; rand_num<10; rand_num++) 
        cout<<rand()<<endl;
    return 0; 
}

Output:

931265979
1479686946
1788426935
1359145123
582011072
375015354
631128646
1181344226
1858016203
138472912

Performing Mathematical Operations on the Generated Random Number

#include<iostream>
#include<cstdlib>
using namespace std;

int main()
{  
   srand(time(0)); 
   for(int rand_num = 0; rand_num<10; rand_num++) 
        cout<<rand()%100<<endl;
    return 0; 
}

Output:

84
79
50
79
84
74
81
56
77
16

C++ rand() and srand() function at a glance!

  • The rand() function generates numbers randomly.
  • When execute the rand() function in a program, the same random number gets represented.
  • The srand() function along with the rand() function generates random numbers at compile time.
  • The srand() function does not return any value while the rand() function returns the random number generated by it.

Conclusion

Thus, in this article we have understood the working of C++ rand() function and srand() function respectively.


References

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages