0% found this document useful (0 votes)
7 views1 page

C++ Random Number Generation Code

The document provides a C++ code snippet for generating a random number using the rand and srand functions. It explains that rand generates a pseudorandom number, while srand sets the starting point for random number generation. The code initializes the random number generator with the current time as the seed and outputs a random number between 0 and 99.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views1 page

C++ Random Number Generation Code

The document provides a C++ code snippet for generating a random number using the rand and srand functions. It explains that rand generates a pseudorandom number, while srand sets the starting point for random number generation. The code initializes the random number generator with the current time as the seed and outputs a random number between 0 and 99.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

//Random Number Generation C++ Code – S@meer Akr@m

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
int num;

srand( time(0) );
num = rand() % 100;

cout << "Random Number Generated = " << num;

cout << endl;


system("pause");
return 0;
}

----------------------------------------------------------------------

The rand function generates a pseudorandom number

int rand( void );

Return Value

rand returns a pseudorandom number, as described above. There is no error return.

Remarks

The rand function returns a pseudorandom number

----------------------------------------------------------------------------------------------------------

srand function sets a random starting point

void srand( unsigned int seed );

Parameters
seed
Seed for random-number generation

Remarks

The srand function sets the starting point for generating a series of pseudorandom integers in the
current thread. To reinitialize the generator, use 1 as the seed argument. Any other value for seed
sets the generator to a random starting point. rand retrieves the pseudorandom numbers that are
generated. Calling rand before any call to srand generates the same sequence as calling srand
with seed passed as 1.

You might also like