C++:标准C函数(随机数,时间函数)
介绍
ANSI组织定义了C标准和标准库函数。
使用标准C函数优点:
使用标准C函数在任何平台上都支持,使得同一个源码,在Windows编译运行的结果和Linux上编译运行结果相同,无需更改代码。
随机数(rand)
产生指定范围内随机数(1~100)
#include <stdio.h>
#include <stdlib.h>
int main()
{
for (int i=0; i<10; i++)
{
printf("%d\n", rand()%100);
}
}
每次运行会发现得到的是个随机数一样,为了解决这个问题,使用srand设置一个种子(seed),每次启动保证种子不同。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time(NULL));
for (int i=0; i<10; i++)
{
printf("%d\n", rand()%100);
}
}
时间函数(time)
获取当前时间戳(单位:s),时间戳即为距离1970-01-01 00:00:00的秒数
#include <stdio.h>
#include <time.h>
int main()
{
time_t ts = time(NULL);
printf("%d\n", (int)ts);
}
通过时间戳获取年月日,时分秒,周几
#include <stdio.h>
#include <time.h>
int main()
{
time_t ts = time(NULL);
tm time = *localtime(&ts);
int year = time.tm_year + 1900;
int month = time.tm_mon + 1;
int day = time.tm_mday;
int hour = time.tm_hour;
int min = time.tm_min;
int sec = time.tm_sec;
int week = time.tm_wday ;
return 1;
}
通过年月日,时分秒,获取time_t 时间戳
#include <stdio.h>
#include <time.h>
int main()
{
//时间为2017-07-15 21:38:30
tm time = {0};
time.tm_year = 2017 - 1900;
time.tm_mon = 7 -1;
time.tm_mday = 15;
time.tm_hour = 21;
time.tm_min = 38;
time.tm_sec = 30;
time_t ts = mktime(&time);
//获得该天为周几
tm time1 = *localtime(&ts);
printf("周%d\n", time1.tm_wday);
return 1;
}
欢迎加群交流:C/C++开发交流

C++:标准C函数(随机数,时间函数)的更多相关文章
- Mysql日期函数,时间函数使用的总结
一.MySQL 获得当前日期时间 函数 1.1 获得当前日期+时间(date + time)函数:now() mysql> select now();+--------------------- ...
- 0513JS数组内置方法、数学函数、时间函数
|数组中常用的内置方法|-push()与pop()|--push()是往数组的尾部添加,同时返回新数组的长度 var attr = [1,2,3,4,5];var attr2 = [6,7,8,9,0 ...
- MySQL:日期函数、时间函数总结(MySQL 5.X)
http://www.cnblogs.com/she27/archive/2009/01/16/1377089.html 原文:http://www.51sdj.com/phpcms/picture/ ...
- 【转】MySQL:日期函数、时间函数总结(MySQL 5.X)
转自:http://www.cnblogs.com/she27/articles/1377089.html 一.MySQL 获得当前日期时间 函数1.1 获得当前日期+时间(date + time)函 ...
- 日期函数、时间函数总结(MySQL 5.X)
一.MySQL 获得当前日期时间 函数1.1 获得当前日期+时间(date + time)函数:now()mysql> select now(); +---------------------+ ...
- MySQL日期函数、时间函数总结(MySQL 5.X)
一.获得当前日期时间函数 1.1 获得当前日期+时间(date + time)函数:now() select now(); # :: 除了 now() 函数能获得当前的日期时间外,MySQL 中还有下 ...
- SqlServer 查询的几种方式以及数字函数、时间函数的应用总结(回归基础)
--语法:select * from 表名 *表示查询所有字段数据 select * from Class select * from Student select * from RankingLis ...
- Oracle内置函数:时间函数,转换函数,字符串函数,数值函数,替换函数
dual单行单列的隐藏表,看不见 但是可以用,经常用来调内置函数.不用新建表 时间函数 sysdate 系统当前时间 add_months 作用:对日期的月份进行加减 写法:add_months(日期 ...
- MySQL:日期函数、时间函数总结
MySQL 获得当前日期时间 函数 获得当前日期+时间(date + time)函数:now() mysql> select now(); +---------------------+ | n ...
随机推荐
- manjaro xfce4 使用super+D快捷键显示桌面(以及使用super+方向键调整窗口)设置无效
xfce4 有两个地方设置快捷键:Keyboard -> application shortcuts 和 window manager -> keyboard. window manage ...
- Ubuntu 14.04 查看指定端口的服务
查看已经连接的服务端口(ESTABLISHED) netstat -a 查看所有的服务端口(LISTEN,ESTABLISHED) netstat -ap 查看指定端口,可以结合grep命令: net ...
- MySql通过Data恢复数据库数据
公司的服务器开不了机,把硬盘装在其他机器上面,文件是没有丢失,可是数据库开不了了.上网查了下,用DATA恢复了数据. 1.先通过MYSQL目录下的my.ini配置文件找到 datadir的文件夹地址. ...
- Eclipse中Git的操作
一.工程初始化为本地库 工程→右键→Team→Share Project→Git 点击Finish即可 二.Eclipse 中忽略不必追踪控制的文件 列如: .classpath 文件 .projec ...
- Java判断整数溢出
开题报告第一版写完发给老师了,熬了两周终于搞出来了,等着被怼了之后再改吧.晚上选了Leetcode一道简单的题,整数反转,就是将一个int类型的数反转.原本确实很简单,最后出现个问题有意思--整数溢出 ...
- sql data compare
https://documentation.red-gate.com/sdc14 About SQL Data Compare With SQL Data Compare, you can compa ...
- Java 中的JSON 字符串
类库选择 Java中并没有内置JSON的解析,因此使用JSON需要借助第三方类库. 下面是几个常用的 JSON 解析类库: Gson: 谷歌开发的 JSON 库,功能十分全面. FastJson: 阿 ...
- 【Oracle】往Oracle11g的某表插入近千万条记录,耗时略超一小时
和MySql的对比下,两者有数量级的差距. 表ddl: CREATE TABLE tb04 ( "ID" ,) not null primary key, "NAME&q ...
- Assertion failure in -[UISectionRowData refreshWithSection:tableView:tableViewRowData:]
最近在项目中遇到了 Assertion failure in -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] 这个 ...
- Tips for Conda
管理环境 创建环境 基于 python3.6 创建一个名为test_py3的环境 conda create -n test_py3 python=3.6 基于 python2.7 创建一个名为test ...