艾达:随机播种 [英] Ada: Seeding Random

查看:90
本文介绍了艾达:随机播种的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为Ada.Numerics.Discrete_Random分配离散值?我看到这样的代码:

 声明
类型Rand_Range的范围为25..75;
包Rand_Int是新的Ada.Numerics.Discrete_Random(Rand_Range);
种子:Rand_Int.Generator;
Num:Rand_Range;
开始
Rand_Int.Reset(seed);
Num:= Rand_Int.Random(seed);
Put_Line(Rand_Range’Image(Num));
结尾;

这会为 Rand_Int和 seed值添加种子,但实际上我找不到任何说明设置种子值。还是我完全以错误的方式看待这个问题?我想将种子值设置为可以控制以观察测试结果的数字(例如4或5)。



谢谢!

解决方案

将第二个 Integer 参数传递给 Reset 。这里是发起人

  Rand_Int.Reset(种子,发起人); 

Ada是使用完整,详细的参考手册和基本原理免费提供。用它!此外,这是更新的Ada版本的标准



另一个注意事项:代码中的变量名称 seed 是一个糟糕的选择。像 state generator 这样的选择会更好。



<注意:Ada在许多方面确实是一种非常好的语言。人们为非常强大,详细的字体系统而苦恼。然后,当系统完成并开始运行时,先尝试一些错误,然后他们神秘地忘记将其归因于Ada。重要的缺点是库的可用性和IDE的成熟度。


How can I seed Ada.Numerics.Discrete_Random with a discrete value? I see code like:

declare
   type Rand_Range is range 25..75;
   package Rand_Int is new Ada.Numerics.Discrete_Random(Rand_Range);
   seed : Rand_Int.Generator;
   Num : Rand_Range;
begin
   Rand_Int.Reset(seed);
   Num := Rand_Int.Random(seed);
   Put_Line(Rand_Range'Image(Num));
end;

which seeds the "Rand_Int" with the "seed" value, but I cannot find any instruction on actually setting the seed value. Or I am completely looking at this the wrong way? I want to set the seed value to a number (like 4 or 5) that I can control to observe test results.

Thanks!

解决方案

Pass a second Integer argument to Reset. Here it's initiator.

Rand_Int.Reset(seed, initiator);

Ada is one of the few languages with complete, detailed reference manual and rationale available free of charge. Use it! Additionally, here is the more recent Ada version's standard.

Another note: the variable name seed in your code is a terrible choice. A choice like state or generator would be much better.

NB: Ada is really a very nice language in many respects. People gripe about the very strong, detailed type system. Then when the system's done and it runs first try with few bugs, they mysteriously forget to attribute it to Ada. The significant down sides are library availability and maturity of IDEs.

这篇关于艾达:随机播种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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