//#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.