rand () function
rand () function prototype int rand (void), it returns an integer of from 0 to RAND_MAX (RAND_MAX C language standard library <stdlib.h> is defined in a macro 0x7fff i.e. 32767)
But only with rand () returns the result is constant, since the rand () function is based on a number (called seed) by a fixed formula computed from, but computer is turned on, the seed value is given up, so the results are unchanged
Providing C srand () had its prototype void srand (int a) function is to initialize rand () function of the initial value
But for a seed, we can not produce every time a random number, then manually enter it once a
So we have to time your computer as a seed, then the seeds will own changed (time () function explanation see below)
#include <bits / STDC ++ H.> the using namespace STD; int main () { srand (Time (NULL)); // Time (NULL) is the system time COUT << RAND () << endl; return 0 ; }
How that generates random numbers within a certain range of it, it is easy to think of using "%" and "+" to achieve
Since 0 <= rand ()% (nm + 1) <= nm
So m <= rand () n% (n-m + 1) + m <=
#include <bits / STDC ++ H.> the using namespace STD; int main () { int n-, m; CIN >> m >> n-; srand (Time (NULL)); COUT << RAND ()% (n-- m + . 1 ) + m << endl; // generates a random integer between [m, n-] return 0 ; }
time.h library related
time.h header file defines the four types of variables, and various operation functions of two macros date and time.
Four variables
size_t | Is an unsigned integer type, it is the result of sizeof keyword |
clock_t | This is a type of memory for processor time, unsigned long type |
time_t | This is a type suitable for storing calendar time |
struct tm | This is a location to save the time and date of construction |
tm structure is defined as follows:
Two macro
NULL | This macro is a null pointer value constant |
CLOCKS_PER_SEC | It represents the number of clock cycles within a second CPU running Results clock () function is used to convert an amount in seconds But the specific value of this amount is related to the operating system, typically 1000 |
Part of the library functions
difftime (): to calculate two seconds as the difference between the target time_t calendar time (time_end - time_beg), if time_end
the representative prior to the time_beg
time point, the result is negative. Function prototype: double difftime (time_t time2, time_t time1)
mktime (): Function Prototype: time_t mktime (struct tm * timeptr ), the structure pointed to convert timeptr based on a local time zone value time_t
time (): returns the encoded into time_t object's current calendar time, and the storage arg
points time_t object (except arg
for the null pointer, get the current calendar time when the timer = NULL (from 00:00:00 to 1970-01-01 now the number of seconds), the time when the timer = value, to set the calendar time, is an unsigned long type time_t function prototype:. time_t time (time_t * timer )
#include <bits/stdc++.h> using namespace std; int main() { time_t seconds; seconds = time(NULL); printf("Hours since 1970-01-01 = %ld\n", seconds / 3600); return 0; }
clock (): returns from performing clock function (usually at the beginning of the program), the clock time of the processor used, typically is used to calculate the certain period of execution time of the program or function prototype: clock_t clock (void)
#include <bits / STDC ++ H.> the using namespace STD; int main () { of clock_t START_TIME, finish_time; START_TIME = Clock (); // running a test program can be run this time finish_time = Clock (); Double TOTAL_TIME = ( Double ) (finish_time - START_TIME) / CLOCKS_PER_SEC; // converts the time in seconds to milliseconds if you want to give to CLOCKS_PER_SEC deleting the printf ( "the Program of the time running iS : F% \ n- " , TOTAL_TIME); return 0 ; }
asctime (): The time variable is defined as tm structure represented by the string representation out function prototype: char * asctime (struct tm * ptr)
#include <bits/stdc++.h> using namespace std; int main() { struct tm t; t.tm_sec = 10; t.tm_min = 50; t.tm_hour = 16; t.tm_mday = 6; t.tm_mon = 7; t.tm_year = 119; t.tm_wday = 2; printf("%s\n", asctime(&t));//2019/8/6 16:50:10 星期二 return (0); }
localtime (): function prototype: struct tm * localtime ( const time_t * Time ), conversion (at a given time from the start of epoch time
is directed values time_t) to form struct tm calendar time and the local time expression
#include <bits/stdc++.h> using namespace std; int main() { time_t timer; struct tm *Now; time(&timer); //获取当前时间 Now = localtime(&timer); //填充struct tm 结构 printf("Current local time and date:%s", asctime(Now)); return (0); }
ctime (): Converts the specified time from the start of the era to the cost of calendar time, and then into the text display, as calls asctime ( localtime ( Time ) ), the function prototype: char * ctime (const time_t * timer)
#include <bits/stdc++.h> using namespace std; int main() { time_t curtime; time(&curtime); printf("current time = %s", ctime(&curtime)); return (0); }