About srandom and random

Source: Internet
Author: User

Srandom () and random () are used in the VC ++ program. The header file is stdlib. h, but the compilation Error error c3861: "srandom": the identifier cannot be found.
The reason is that the library functions of the VC ++ compiler do not include randomize () and random (), which are replaced by srand () and rand () respectively.
# Include <time. h> // defines the time function.
This header file is usually required when the time (null) (current time) function is used.
# Include <stdlib. h> // defines miscellaneous functions and memory allocation functions.
This header file is usually required when you use the rand () and srand () functions.

Function Name: Random Function: random number generator, that is, a random number is generated.
Usage: int random (INT num );
The random number range is 0 ~ Num-1.

Function Name: randomize
Function: Initialize the random number generator, which is equivalent to allocating random seeds.
Usage: void randomize (void );

#include <iostream>#include <stdlib.h>    // Need random(), srandom()#include <time.h>      // Need time()#include <algorithm>   // Need sort(), copy()#include <vector>      // Need vectorusing namespace std;void Display(vector<int>& v, const char* s);int main(){// Seed the random number generatorsrand(time(NULL));// Construct vector and fill with random integer valuesvector<int> collection(10);for (int i = 0; i < 10; i++)        collection[i] = rand() % 10000;// Display, sort, and redisplayDisplay(collection, "Before sorting");sort(collection.begin(), collection.end());Display(collection, "After sorting");return 0;}// Display label s and contents of integer vector vvoid Display(vector<int>& v, const char* s){cout << endl << s << endl;copy(v.begin(), v.end(),ostream_iterator<int>(cout, "\t"));cout << endl;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.