You Are Here:

Community: Developer Discussion Boards

#1 Old Lightbulb symbian下的随机数的问题 - 2004-12-22, 15:49

Join Date: Dec 2004
Posts: 8
greatone007
Offline
Registered User
我在一个类的constructL中用Math::Rand(a)想为不同的类的实例生成不同的随机数,但是不同实例却得到相同的结果,请问应该怎样才能得到不同的结果?多谢
Reply With Quote

#2 Old 2004-12-23, 01:54

Join Date: Sep 2003
Posts: 694
bluerose's Avatar
bluerose
Offline
Super Contributor
Math::Random()

Math::Rand(TInt64 &aSeed)必须要一个种子数,产生随机数具体的算法我不太清楚,相同的种子数只能产生相同的随机数。


Bluerose
Reply With Quote

#3 Old 2004-12-24, 04:58

Join Date: Apr 2003
Posts: 1,534
Location: Tianjin, China
Send a message via MSN to Maxying
Maxying
Offline
Super Contributor
计算机所产生的随机数并不是真正的随机,而是通过一定的算法而生成的一个数字,这个道理是在任何一本程序语言教科书里都能找到的。想产生不同的随机数就要置不同的随机种子,而为了让程序每次运行的结果不尽相同,一般都将随机种子置为当前时间之类。

Hope this helps!

Regards
Maxying
Reply With Quote

#4 Old 2004-12-25, 14:47

Join Date: Dec 2004
Posts: 8
greatone007
Offline
Registered User
多谢两位大虾,我明白了,多谢,多谢,原理是这样的:
T(n+1)=T(n)*a 之类的,也就是说它通过最初函数中的种子,不断计算出来一串数,也就是说这一串数随着初值已经确定好了,所以初值一样,结果也就一样了。
我还有一个问题,大虾们能否给我介绍一下Symbian中各种类型转换,比如时间类型的转换成为整形的,因为我还没有接触过时间类型,能给我具体介绍一下吗?多谢了
Last edited by greatone007 : 2004-12-25 at 14:57.
Reply With Quote

#5 Old 2004-12-25, 19:35

Join Date: Apr 2003
Posts: 1,534
Location: Tianjin, China
Send a message via MSN to Maxying
Maxying
Offline
Super Contributor
你可能误会我的意思了,我所说的把随机种子置为当前时间,这不需要把时间类型转换为整型,只是调用类似GetTickCount()之类的函数来得到系统的嘀嗒数就可以了。

Regards
Maxying
Reply With Quote

#6 Old 2004-12-26, 02:50

Join Date: Sep 2003
Posts: 694
bluerose's Avatar
bluerose
Offline
Super Contributor
对于随机数的生成,仅仅是将一个差别很小的数进行不断的放大而已,比如4和5仅仅相差1个,如果放大10倍就相差10,对于计算机来说,当算法一定的时候,初始条件相同的话就会产生相同的结果,因此即使是Math::Random()也是有可能产生两个相同的随机数的,但就概率方面来说几率是非常小的,而对于Math::Rand(TInt64 &aSeed)来说,由于种子和算法在刚开始就被确定的,因此相同的种子可以产生相同的随机数,从而达到再现的结果。对于Math::Random()可能使用的是CPU时钟作为随机数种子,我个人这么认为,至于真实情况,我认为没太大必要讨论的那么清楚。


Bluerose
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia