当前位置:网站首页>[C language] random number generation and `include < time. H > 'learning
[C language] random number generation and `include < time. H > 'learning
2022-07-28 20:03:00 【An ran_】
Catalog
One 、 Random number generation
3、 ... and 、 Some broken thoughts
One 、 Random number generation
1. Pseudo random number 【rand The application of function 】
The functionality : It can be produced randomly 0~rand_max The random number , Different definition types , The maximum value is also different .
The header file :#include <stdlib.h>
It is called pseudo-random number , Because it is calculated according to a number and a formula , There is a high probability that the same number will be generated many times .
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a=rand();
printf("%d",a);
return 0;
}
2. Random number generation 【srand Function summary 】
The function prototype :void srand (usigned int seed);
The functionality : Initialization function of random number generator ( Don't talk to people , It's very quiet )
Function header file :#include <stdlib.h> But it also needs #include <time.h> The main srand The parameters inside should be time function
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
srand((unsigned)time(NULL));
int n=rand();
printf("%d\n",n);
return 0;
}
3. Generate a specific range of random numbers
(1) Produce a random number range 0~m(int type )
int a=rand()%m;
(2) Produce a random number range m~n
int a=rand()%(n-m+1)+m;
Reference blog :http://t.csdn.cn/05x1B
Two 、#include <time.h> The header file
1. Two data types
clock_t: Measured time value ( It can only represent the time interval )—— For program timing
time_t: Calculated distance UTC Time (1970-01-01 00:00:00) The number of seconds
Add : A structure ---->struct tm: It can be called decomposition time , Divide time into seconds 、 branch 、 Isochronous . Usually when storing locally .
2. Several commonly used functions
(1)clock: Get the processor time occupied after the program starts execution , Return value clock_t.
(2)time: Get current system time (UTC Time ) Of time_t value .
(3)ctime: take time_t value transformation Is... Representing local time character string .
(4)gmttime: take time_t value transformation by Express GMT String of time .
(5)localtime: take time_t transformation by Indicates local time strunct tm structure .
(6)mktime: take Express Local time Of struct tm** Convert to time_t**.
(7)asctime: take struct tm** transformation In string form **.
(8)difftime: obtain Two calendar hours And The difference between the two .
(9)strftime: Customize the structure tm Date and time information Convert to the specified format .
Add : The functions of this header file can be divided into two categories
(1) Time processing functions
(2) Time conversion function

3、 ... and 、 Some broken thoughts
Random numbers have been seen before , I used it a little once / two , Know it's useful , But never in-depth understanding , Just think of something you know , Guide a learning partner to ask me how to output and generate random numbers in a specific range , I just froze ……
As for this time.h The header file , I saw this random number generation again when I did the project experiment a few days ago , Sprout the library file you want to further master , To do .
But random number generation is done , But this time.h Look at the function in , Currently, there is no application scenario ( Or I can't use it ), Plus, it's basically useless. It looks like a big nod , So it's just a simple understanding of the outline , I know there are several types , I know several functions , And I know , among time(NULL) It can be used to grow seeds .
Let's call it a day !
边栏推荐
- How does app automated testing achieve H5 testing
- C+ + core programming
- Failed to install app-debug. apk: Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]
- Common APIs in string
- Leetcode Day5 delete duplicate email
- KPMG China: insights into information technology audit projects of securities fund management institutions
- Application skills of programming rising and falling edge instructions of botu 1200/1500plc (bool array)
- JS batch add event listening onclick this event delegate target currenttarget onmouseenter OnMouseOver
- High beam software has obtained Alibaba cloud product ecological integration certification, and is working with Alibaba cloud to build new cooperation
- Can China make a breakthrough in the future development of the meta universe and occupy the highland?
猜你喜欢

Digital filter design matlab

Integration and implementation of login click graphic verification code in personal blog system

Deploy ZABBIX automatically with saltstack

KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书

Deploy LNMP automatically with saltstack

Implementation of markdown editor in editor.md

XOR operation and its usage

Android section 13 03xutils detailed explanation of database framework (addition, deletion and modification)

Theoretical knowledge of digital image (I) (personal analysis)

adb remount of the / superblock failed: Permission denied
随机推荐
Basic knowledge of communication network 01
Advanced notes (Part 2)
[network] cross area network communication learning classification and calculation of IPv4 address
Source insight project import and use tutorial
【经验之谈】关于维修电子设备的几点建议和经验
Overcome the "fear of looking at teeth", and we use technology to change the industry
通信网络基础知识01
基于QTGUI图像界面的空战游戏设计
Basic knowledge of C language
KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书
Read how to deploy highly available k3s with external database
Use Hal Library of STM32 to drive 1.54 inch TFT screen (240*240 st7789v)
How does app automated testing achieve H5 testing
Article translation software - batch free translation software supports major translation interfaces
Const pointer of C language and parameter passing of main function
冲刺金九银十丨熬夜半个月汇集大厂Android岗1600道面试真题
How openocd directly downloads programs to STM32 board through stlink (solved)
Common modules of saltstack
Find the memory occupied by the structure
C language operators and input and output