[C++基础] 取系统时间和随机数
2006-07-19 22:10
274 查看
获取系统时间
#include <iostream>
#include <ctime>
using namespace std;
int main(void)
{
struct tm *times; //定义时间指针变量.....(只能用指针)
time_t t; //定义系统时间变量,供提供系统时间
t = time(0); //当前系统时间给变量t
times = localtime(&t); //把系统时间变量强制转换成tm结构体形式时间
cout << (times->tm_year+1900)<<endl;
cout << times->tm_mon<<endl;
cout << times->tm_mday<<endl;
cout << times->tm_hour<<endl;
cout << times->tm_min<<endl;
cout << times->tm_sec<<endl;
return 0;
}
输出:2006 6 19 22 21 22
struct tm {
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [0,59] */
int tm_hour; /* hours since midnight - [0,23] */
int tm_mday; /* day of the month - [1,31] */
int tm_mon; /* months since January - [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday - [0,6] */
int tm_yday; /* days since January 1 - [0,365] */
int tm_isdst; /* daylight savings time flag */
};
获得随机数
#include <iostream>
#include <ctime>
using namespace std;
void main( void )
{
int i;
/* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );
/* Display 10 numbers. */
for( i = 0; i < 10;i++ )
{
cout<<rand()%10<<endl;
}
}
#include <iostream>
#include <ctime>
using namespace std;
int main(void)
{
struct tm *times; //定义时间指针变量.....(只能用指针)
time_t t; //定义系统时间变量,供提供系统时间
t = time(0); //当前系统时间给变量t
times = localtime(&t); //把系统时间变量强制转换成tm结构体形式时间
cout << (times->tm_year+1900)<<endl;
cout << times->tm_mon<<endl;
cout << times->tm_mday<<endl;
cout << times->tm_hour<<endl;
cout << times->tm_min<<endl;
cout << times->tm_sec<<endl;
return 0;
}
输出:2006 6 19 22 21 22
struct tm {
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [0,59] */
int tm_hour; /* hours since midnight - [0,23] */
int tm_mday; /* day of the month - [1,31] */
int tm_mon; /* months since January - [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday - [0,6] */
int tm_yday; /* days since January 1 - [0,365] */
int tm_isdst; /* daylight savings time flag */
};
获得随机数
#include <iostream>
#include <ctime>
using namespace std;
void main( void )
{
int i;
/* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );
/* Display 10 numbers. */
for( i = 0; i < 10;i++ )
{
cout<<rand()%10<<endl;
}
}
相关文章推荐
- C++产生m到n之间的随机数,产生0到100之间的随机数,以系统时间作为随机种子
- 基础Android应用开发 Button显示系统时间+TextView显示随机数+简单计算器
- C++利用系统时间产生的随机数
- 【C/C++】获取当前系统时间
- C++中的srand(time(null))利用时间设置随机种子产生随机数
- Cocos2d-2.x_使用系统时间来获取随机数
- C++获得系统时间,以及1970年1月1日到现在的毫秒数
- Fortran 随机数产生的子程序(调用系统时间单位为毫秒)
- C++ 获取系统时间并且转换成字符串
- [C++基础]随机数,随机种子数
- 蓝桥杯练习系统试题集(二)--基础练习(含C/C++答案)
- C++调用系统时间
- C++获取系统时间的方法
- C++实现调用系统时间简单示例
- OCCI 获取系统时间函数 C++
- matlab根据读取系统时间产生比较靠谱的随机数
- c++ 获得系统时间
- Windows下C/C++获取当前系统时间
- c/c++获取系统时间函数
- C++时间标准库时间time和系统时间的使用