GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::srand(3) C++ Standard Libary std::srand(3)

std::srand - std::srand


Defined in header <cstdlib>
void srand( unsigned seed );


Seeds the pseudo-random number generator used by std::rand() with the value seed.


If std::rand() is used before any calls to srand(), std::rand() behaves as if it was
seeded with srand(1).


Each time std::rand() is seeded with the same seed, it must produce the same
sequence of values.


srand() is not guaranteed to be thread-safe.


seed - the seed value


(none)


Generally speaking, the pseudo-random number generator should only be seeded once,
before any calls to rand(), at the start of the program. It should not be repeatedly
seeded, or reseeded every time you wish to generate a new batch of pseudo-random
numbers.


Standard practice is to use the result of a call to std::time(0) as the seed.
However, std::time returns a std::time_t value, and std::time_t is not guaranteed to
be an integral type. In practice, though, every major implementation defines
std::time_t to be an integral type, and this is also what POSIX requires.

// Run this code


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


int main()
{
std::srand(std::time(0)); //use current time as seed for random generator
int random_variable = std::rand();
std::cout << "Random value on [0 " << RAND_MAX << "]: "
<< random_variable << '\n';
}


Random value on [0 2147483647]: 1373858591


rand generates a pseudo-random number
(function)
RAND_MAX maximum possible value generated by std::rand
(macro constant)
reseed reseeds the per-thread random engine
(function)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.