c和c的时间函数和随机函数总结与范例.pdf

上传人:zh****71 文档编号:126718813 上传时间:2020-03-27 格式:PDF 页数:17 大小:31.24KB
返回 下载 相关 举报
c和c的时间函数和随机函数总结与范例.pdf_第1页
第1页 / 共17页
c和c的时间函数和随机函数总结与范例.pdf_第2页
第2页 / 共17页
亲,该文档总共17页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《c和c的时间函数和随机函数总结与范例.pdf》由会员分享,可在线阅读,更多相关《c和c的时间函数和随机函数总结与范例.pdf(17页珍藏版)》请在金锄头文库上搜索。

1、c c 中时间函数和随机函数的总结 C 的随机函数和时间函数 随机函数 一 C 中不能使用 random 函数 random 函数不是 ANSI C标准 不能在 gcc vc等编译器下编译通过 可改 用 C 下的 rand 函数来实现 1 C 标准函数库提供一随机数生成器rand 返回 0 RAND MAX之间均 匀分布的伪随机整数 RAND MAX必须至少为 32767 rand 函数不接受参数 默认以 1 为种子 即起始值 随机数生成器总是以相同的种子开始 所以形 成的伪随机数列也相同 失去了随机意义 但这样便于程序调试 2 C 中另一函数 srand 可以指定不同的数 无符号整数变元 为

2、 种子 但是如果种子相同 伪随机数列也相同 一个办法是让用户输入种子 但是仍然不理想 3 比较理想的是用变化的数 比如时间来作为随机数生成器的种子 time 的值每时每刻都不同 所以种子不同 所以 产生的随机数也不同 C 随机函数 VC program include include include using namespace std define MAX 100 int main int argc char argv srand unsigned time NULL srand 函数产生一个以当前时间开始的 随机种子 应该放在 for 等循环语句前面不然要很长时间等待 for int i

3、 0 i 10 i cout rand MAX endl MAX为最大值 其随机域为0 MAX 1 return 0 二 rand 的用法 rand 不需要参数 它会返回一个从0 到最大随机数的任意整数 最大随 机数的大小通常是固定的一个大整数 这样 如果你要产生0 10的 10 个整数 可以表达为 int N rand 11 这样 N 的值就是一个 0 10的随机数 如果要产生1 10 则是这样 int N 1 rand 11 总结来说 可以表示为 a rand n 其中的 a 是起始值 n 是整数的范围 a rand b a 1 就表示 之间的一个随机数 若要 0 1 的小数 则可以先取得

4、0 10的整数 然后均除以10 即可得到随机到 十分位的 10 个随机小数 若要得到随机到百分位的随机小数 则需要先得到 0 100的 10 个整数 然后均除以100 其它情况依 此类推 通常 rand 产生的随机数在每次运行的时候都是与上一次相同的 这是有 意这样设计的 是为了便于程序的调试 若要产生每次不同的随机数 可以使 用 srand seed 函数进行随机化 随着seed的不同 就能够产生不同的随机数 如大家所说 还可以包含time h 头文件 然后使用srand time 0 来使用 当前时间使随机数发生器随机化 这样就可以保证每两次运行时可以得到不同 的随机数序列 只要两次运行的

5、间隔超过1 秒 函数名 random 功 能 随机数发生器 用 法 int random int num 程序例 include include include prints a random number in the range 0 to 99 int main void randomize printf Random number in the 0 99 range d n random 100 return 0 函数名 randomize 功 能 初始化随机数发生器 用 法 void randomize void 程序例 include include include int main

6、 void int i randomize printf Ten random numbers from 0 to 99 n n for i 0 i 10 i printf d n rand 100 return 0 rand 产生随机数 相关函数 srand 表头文件 include 定义函数 int rand void 函数说明 rand 会返回一随机数值 范围在0 至 RAND MAX 间 在调用此函数产生随机 数前 必须先利用srand 设好随机数种子 如果未设随机数种子 rand 在调 用时会自动设随机数种子为1 关于随机数种子请参考srand 返回值 返回 0 至 RAND MAX

7、之间的随机数值 RAND MAX定义在 stdlib h 其值为 2147483647 范例 产生介于 1 到 10 间的随机数值 此范例未设随机数种子 完整的随机数产 生请参考 srand include main int i j for i 0 i 10 i j 1 int 10 0 rand RAND MAX 1 0 printf d j 执行 9 4 8 8 10 2 4 8 3 6 9 4 8 8 10 2 4 8 3 6 srand 设置随机数种子 相关函数 rand 表头文件 include 定义函数 void srand unsigned int seed 函数说明 srand

8、 用来设置 rand 产生随机数时的随机数种子 参数seed必须是个整数 通常可以利用 geypid 或 time 0 的返回值来当做 seed 如果每次 seed都设相同 值 rand 所产生的随机数值每次就会一样 返回值返回 0 至 RAND MAX之间的随机数值 RAND MAX定义在 stdlib h 其 值为 2147483647 范例 产生介于 1 到 10 间的随机数值 此范例未设随 机数种子 完整的随机数产生请参考srand include main int i j for i 0 i 10 i j 1 int 10 0 rand RAND MAX 1 0 printf d j

9、 返回值 范例 产生介于 1 到 10 间的随机数值 此范例与执行结果可与rand 参照 include include main int i j srand int time 0 for i 0 i 10 i j 1 int 10 0 rand RAND MAX 1 0 printf d j 执行 5 8 8 8 10 2 10 8 9 9 2 9 7 4 10 3 2 10 8 7 在指定的两个数之间产生随机数 include include include 返回 a 和 b 之间的随机数 用时间做种子 int rand between a b int a int b int c re t

10、emp time t t if a b temp a a b b temp c a b srand unsigned time 使用时间做种子数 re b rand c return re int main int re re rand between a b 35 98 printf d n re getch return 0 注 srand 配置随机数种子 srand unsigned time NULL 产生伪随机数数列 rand 产生随机数 C库里的 Rand函数用的是伪随机数生成算法 你不种一个新种子进去 生成的 随机数序列都是一样的 伪随机的意思就是不随机 种子相同的情况下 等次数

11、地获得的 随机 结果 都是一样的 函数嘛 为了让他真随机 就得找个现成的随机量作初值 一般选时间作为 srand的参数 也就是seed 时间函数 一 获取日历时间 time t 是定义在 time h 中的一个类型 表示一个日历时间 也就是从1970 年 1 月 1 日 0 时 0 分 0 秒到此时的秒数 原型是 typedef long time t time value 可以看出 time t 其实是一个长整型 由于长整型能表示的数值有限 因此它能 表示的最迟时间是2038 年 1 月 18 日 19 时 14 分 07 秒 函数 time 可以获取当前日历时间时间 time 的定义 ti

12、me t time time t include include using namespace std int main void time t nowtime nowtime time NULL 获取当前时间 cout nowtime endl return 0 输出结果 1268575163 二 获取本地时间 time t 只是一个长整型 不符合我们的使用习惯 需要转换成本地时间 就要 用到 tm 结构 time h 中结构 tm 的原型是 struct tm int tm sec seconds after the minute 0 59 int tm min minutes afte

13、r 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 可以看出 这个机构定义了年 月 日 时 分 秒 星期 当年中的某一

14、天 夏令时 可以用这个结构很方便的显示时间 用 localtime 获取当前系统时间 该函数将一个time t 时间转换成 tm 结构表示 的时间 函数原型 struct tm localtime const time t 使用 gmtime 函数获取格林尼治时间 函数原型 struct tm gmtime const time t 为了方便显示时间 定义了一个函数void dsptime const struct tm include include using namespace std void dsptime const struct tm 输出时间 int main void tim

15、e t nowtime nowtime time NULL 获取日历时间 cout nowtime endl 输出 nowtime struct tm local gm local localtime 获取当前系统时间 dsptime local gm gmtime 获取格林尼治时间 dsptime gm return 0 void dsptime const struct tm ptm char pxq 日 一 二 三 四 五 六 cout tm year 1900 年 tm mon 1 月 tm mday 日 cout tm hour tm min tm sec cout 星期 tm wd

16、ay 当年的第 tm yday 天 endl 输出结果 1268575163 2010年 3 月 14 日 21 59 23 星期日 当年的第 72 天 2010年 3 月 14 日 13 59 23 星期日 当年的第 72 天 三 输出时间 C C 语言提供了用字符串格式表示时间的函数 char asctime const struct tm char ctime const time t 这两个函数返回值都是一个表示时间的字符串 区别在于传入的参数不同 include include using namespace std int main void time t nowtime nowtime time NULL 获取日历时间 cout nowtime endl 输出 nowtime struct tm local local localtime 获取当前系统时间 cout asctime local cout ctime return 0 输出结果 1268575163 Sun Mar 14 13 59 23 2010 Sun Mar 14 21 59 23 2010 四 计算

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 其它文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号