srand()函数 [英] srand()

查看:73
本文介绍了srand()函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这对我来说似乎是一个C问题。如果不是,我向b $ b道歉。


这个命令:awk''BEGIN {srand(); print srand()}''

将给出自纪元,现在时间以来的秒数。


你们任何人都可以告诉我如何获得秒数

自上世纪后的任意日期开始以来,如同

1980年1月25日13:34 GMT?


如何修改该命令来执行此操作? br />

非常感谢!


Jeremy


-

我思故我在。我想。

解决方案

>这个命令:awk''BEGIN {srand(); print srand()}''

将给出自纪元,现在时间以来的秒数。


不符合我系统上awk的手册页。 srand()在

awk中返回前一个种子。 C中的srand()返回void。这些都没有与当前时间有任何关系。

你们中的任何人都可以告诉我如何获得自纪元以来的秒数
任意的后纪元日期如1980年1月25日13:34 GMT?




在C中无法保证time_t代表的数字

<任何时间单位>自从< epoch>。 *如果您的平台确实碰巧

符合time_t的POSIX要求,那么

a time_t中的值就是您想要的。

填写一个struct tm(解析一个日期的文本字符串以获得一个

填充结构tm留给读者练习)和

上面调用mktime()会给你一个time_t值作为mktime()的返回值

。 (看,这个问题实际上有一点点与b $ b相关。)


Gordon L. Burditt

----- BEGIN PGP签名消息-----

哈希:SHA1


Jeremy Holdstadt写道:
< blockquote class =post_quotes>这对我来说似乎是一个C问题。如果不是,我道歉。

这个命令:awk''BEGIN {srand(); print srand()}''
将给出秒数时代,现在。


这是comp.lang.c的主题


awk不是C,也不是awk内置函数C.而且,as你读了
comp.unix.shell,这个技巧取决于awk'内置srand的实现方式。

第一个srand()来自当前时钟,自Unix纪元以来以
秒为单位测量,第二个srand()将用作

种子的值返回到第一个srand(),从而提供Unix纪录时间打印。


FWIW,Unix纪元时间与C无关。

你们有谁能告诉我怎么样获得自1980年1月25日格林威治标准时间19:34格林威治标准时间之后的任意时代以来的秒数



这里有一些话题。


您需要time.h标头和asctime()或ctime()函数。

如何修改该命令来执行此操作?




这也是关于comp.lang.c的主题


- -

Lew Pitcher

IT顾问,企业应用架构,

企业技术解决方案,道明银行金融集团


(表达的意见是我自己的,而不是我的雇主'')
-----开始PGP签名-----

版本:GnuPG v1.2.4(MingW32)

iD8DBQFAjUtpagVFX4UWr64RAm4tAKDeP / TisblwE8xDYyPQbXMQVo3QMACfQ50K

DDTtuG78ZZQR1mmzKlHPXHw =

= aepT

----- END PGP SIGNATURE -----


Jeremy Holdstadt写道:


这似乎对我来说是一个C问题。如果不是,我道歉。

这个命令:awk''BEGIN {srand(); print srand()}''



^^^

[...]


也许comp.lang.awk可以回答这个问题吗?


-

+ ------------------------- + ------------ -------- + ----------------------------- +

| Kenneth J. Brody | www.hvcomputer.com | |

| kenbrody at spamcop.net | www.fptech.com | #include< std_disclaimer.h> |

+ ------------------------- + -------------- ------ + ----------------------------- +


This seems to be a C question to me. If it is not, I
apologize.

This command: awk ''BEGIN {srand();print srand()}''
will give the number of seconds since the epoch, present time.

Can any of you tell me how to get the number of seconds
since the epoch for an arbitrary post-epoch date like
Jan 25 1980 13:34 GMT?

How to modify that command to do this?

Thanks a lot!

Jeremy

--
I think, therefore I am. I think.

解决方案

>This command: awk ''BEGIN {srand();print srand()}''

will give the number of seconds since the epoch, present time.
Not according to the manual page for awk on my system. srand() in
awk returns the previous seed. srand() in C returns void. Neither
of these have anything to do with the current time.
Can any of you tell me how to get the number of seconds
since the epoch for an arbitrary post-epoch date like
Jan 25 1980 13:34 GMT?



There is no guarantee in C that a time_t represents a number of
<any time unit> since <epoch>. *IF* your platform does happen to
conform to the POSIX requirements for a time_t, then the value in
a time_t is what you want.

Filling in a struct tm (parsing the text string of a date to get a
filled-in struct tm is left as an exercise for the reader) and
calling mktime() on it would give you a time_t value for that time
as the return value of mktime(). (See, there actually is a little
C relevance for this question.)

Gordon L. Burditt


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jeremy Holdstadt wrote:

This seems to be a C question to me. If it is not, I
apologize.

This command: awk ''BEGIN {srand();print srand()}''
will give the number of seconds since the epoch, present time.
This is off-topic for comp.lang.c

Awk is not C, nor are awk builtin functions C. And, as you read in
comp.unix.shell, this trick depends on how awk''s builtin srand is implemented.
The first srand() seeds itself from the current clock, which is measured in
seconds since the Unix epoch, and the second srand() returns the value used as a
seed to the first srand(), thus providing the Unix epoch time to print.

FWIW, Unix epoch time doesn''t have anything to do with C either.
Can any of you tell me how to get the number of seconds
since the epoch for an arbitrary post-epoch date like
Jan 25 1980 13:34 GMT?
Some topicality here.

You want the time.h header and the asctime() or ctime() functions.
How to modify that command to do this?



That''s also off topic for comp.lang.c

- --
Lew Pitcher
IT Consultant, Enterprise Application Architecture,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers'')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFAjUtpagVFX4UWr64RAm4tAKDeP/TisblwE8xDYyPQbXMQVo3QMACfQ50K
DDTtuG78ZZQR1mmzKlHPXHw=
=aepT
-----END PGP SIGNATURE-----


Jeremy Holdstadt wrote:


This seems to be a C question to me. If it is not, I
apologize.

This command: awk ''BEGIN {srand();print srand()}''


^^^
[...]

Perhaps comp.lang.awk can answer this?

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody at spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+


这篇关于srand()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆