Guest:
Register
|
Login
|
Member List
|
Search
|
Statistics
|
FAQ
Language
----------
Simplifed Chinese
Traditional Chinese
English
CnPack Forum
»
技术板块灌水区
» MicroTip#1:随机生成某一范围的浮点数
‹‹ Last Thread
|
Next Thread ››
Poll
Trade
Reward
Activity
Printable Version
|
Email to Friend
|
Subscription
|
Favorites
Subject: MicroTip#1:随机生成某一范围的浮点数
skyjacker
版主
茶农
UID 2239
Digest Posts
9
Credits 617
Posts 269
点点分 617
Reading Access 100
Registered 2006-6-8
Status Offline
#1
Post at 2007-2-9 10:39
Profile
|
Blog
|
P.M.
|
MicroTip#1:随机生成某一范围的浮点数
// MicroTip#1:随机生成某一范围的浮点数
// Wrtten by SkyJacker
function RandomFloat(L, H: Single): Single;
begin
repeat
Result := Random * H;
until (Result > L);
end;
一壶清茶煮青春.
kendling
(小冬)
高级版主
MyvNet
UID 703
Digest Posts
5
Credits 978
Posts 580
点点分 978
Reading Access 101
Registered 2005-2-18
Location 广东
Status Offline
#2
Post at 2007-2-9 10:45
Profile
|
Site
|
Blog
|
P.M.
|
|
这样应该会单、快捷一点吧?
function RandomFloat(L, H: Single): Single;
begin
Result := Single((H - L + 1) * Random + L)
end;
小冬
http://MyvNet.com
Passion
(LiuXiao)
管理员
UID 359
Digest Posts
19
Credits 6838
Posts 3591
点点分 6838
Reading Access 102
Registered 2004-3-28
Status Offline
#3
Post at 2007-2-9 11:08
Profile
|
Blog
|
P.M.
也是。
第一个不能保证时间性。
第二个方便点,但边界条件不知道如何。
skyjacker
版主
茶农
UID 2239
Digest Posts
9
Credits 617
Posts 269
点点分 617
Reading Access 100
Registered 2006-6-8
Status Offline
#4
Post at 2007-2-9 11:58
Profile
|
Blog
|
P.M.
|
// 随机生成某一范围的浮点数
function RandomFloat(L, H: Single): Single;
begin
repeat
Result := Random * H;
until (Result > L);
end;
function RandomFloatA(L, H: Single): Single;
begin
Result := ((H - L + 1) * Random + L);
end;
procedure TForm1.btnFuncRandomClick(Sender: TObject);
begin
Log(Format(cnFloat4, [RandomFloat(0,1)]));
Log(Format(cnFloat4, [RandomFloatA(0,1)]));
end;
RandomFloatA 有边界问题
0.2729
1.3433
一壶清茶煮青春.
skyjacker
版主
茶农
UID 2239
Digest Posts
9
Credits 617
Posts 269
点点分 617
Reading Access 100
Registered 2006-6-8
Status Offline
#5
Post at 2007-2-9 12:00
Profile
|
Blog
|
P.M.
|
// 随机生成某一范围的浮点数
RandomFloat也需要做边界检查
function RandomFloat(L, H: Single): Single;
begin
if L > =H then
begin
Result := H;
exit;
end;
repeat
Result := Random * H;
until (Result > L);
end;
[
本帖最后由 skyjacker 于 2007-2-9 15:00 编辑
]
一壶清茶煮青春.
kendling
(小冬)
高级版主
MyvNet
UID 703
Digest Posts
5
Credits 978
Posts 580
点点分 978
Reading Access 101
Registered 2005-2-18
Location 广东
Status Offline
#6
Post at 2007-2-9 15:07
Profile
|
Site
|
Blog
|
P.M.
|
|
我那个是从VB里转过来的,可能对整数没这个问题。
小冬
http://MyvNet.com
Poll
Trade
Reward
Activity
CnPack Forum
CnPack English Forum
> CnWizards IDE Wizards
> CVSTracNT
> Announcements & Others
All times are GMT++8, the time now is 2024-11-22 13:18
Powered by
Discuz!
5.0.0
© 2001-2006
Comsenz Inc.
Processed in 0.009625 second(s), 9 queries , Gzip enabled
TOP
Clear Cookies
-
Contact Us
-
CnPack Website
-
Archiver
-
WAP
Member's CP Home
Edit Profile
Credits Transaction
Public User Groups
Buddy List
Main
Page Views
User Agents
Posts History
Top Forums
Top Threads
Post Ranking
Credit Ranking
Online Time
Team
Moderation Stats