Random generation number, excerpt from algorithmic competition get started classic p120-p123 test STL.

Source: Internet
Author: User

//#include <bits/stdc++.h>#include <cstring>#include<iostream>#include<cstdio>#include<time.h>///the header file that calls time. #include <algorithm>#include<vector>#include<cstdlib>///call Rand's header file. #include <assert.h>///assert the header file. #defineLL Long Longusing namespacestd;voidFill_random_int (vector<int>&v,intCnt///randomly generates integers in the [0,rand_max] range. {v.clear ();  for(inti =0; I < CNT; i++) {V.push_back (rand ()); }}intMain () {Srand (Time (NULL));///to avoid generating the same random sequence every time, put the time seed. Each time the program runs only once, put it at the beginning. cout<<rand_max<<endl;///different compiler values may be different, typically 2^15-1 = 32767vector<int>v; intCNT =Ten;    Fill_random_int (V,CNT);  for(inti =0; I < v.size (); i++) cout<<v[i]<<" "; cout<<Endl; intn =Ten;  for(inti =0; I < v.size (); i++) {cout<< (int) (v[i]*1.0/rand_max*n) <<endl;///This is the way to get a larger number than Rand_max sometimes. ///V[i]*1.0/rand_max = floating-point number in the range of [0,1]. Then multiply N. is the floating-point number in the [0,n] range. Then take the whole. } sort (V.begin (), V.end ());  for(inti =0; I < v.size ()-1; i++) {assert (V[i]<=v[i+1]);///assert: Assert (expression), the expression is true when it runs normally, for the time being, terminates the program, and prints out the error message.     }    return 0;}

Random generation number, excerpt from algorithmic competition get started classic p120-p123 test STL.

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.