Srand在Raku,是否期望吗?
我正在尝试运行一个包含srand
函数的代码:
srand(1);
my @x = (1..1000).roll(100);
my @y = (200..7000).roll(100);
say sum(@x);
say sum(@y);
say $*KERNEL
从文档中,很明显srand
是 平台依赖。 当我在Windows 10中对其进行测试时,
46221
375477
win32
当我在glot.io
50941
405340
linux
在tio.run中oreferrer“ title =” perl 6 - 在线尝试”>在这里,我得到了
47784
354115
linux (5.2.11.100.fc.29.x.86._.64)
51496
362664
linux
在raku irc频道中,它的
50941
405340
linux
,有无数的产出。这是预期的吗?
为什么我在这里问这个问题是因为我无法在连续集成(例如github操作)中测试以下代码,因为这种可变性即使在linux
中也是如此。以下代码在github操作中失败:
use Test;
if $*KERNEL ~~ 'win32' {
srand(1); # From Raku docs: Note that srand is called with a platform dependent value when a Raku program is started.
# This is tested on my own Windows 10
my @x = (1..1000).roll(100);
my @y = (200..7000).roll(100);
is sum(@x), 46221;
is sum(@y), 375477;
}
elsif $*KERNEL ~~ 'linux' {
srand(1); # From Raku docs: Note that srand is called with a platform dependent value when a Raku program is started.
my @x = (1..1000).roll(100);
my @y = (200..7000).roll(100);
is sum(@x), 50941;
is sum(@y), 405340;
}
else {
skip "Not tested on Mac !", 1;
}
我想在执行连续集成时使该测试工作。
I am trying to run a code containing srand
function:
srand(1);
my @x = (1..1000).roll(100);
my @y = (200..7000).roll(100);
say sum(@x);
say sum(@y);
say $*KERNEL
From the docs its pretty clear that srand
is platform dependent.
When I test this in my Windows 10, I get
46221
375477
win32
When I test it in glot.io here, I get
50941
405340
linux
in tio.run here, I get
47784
354115
linux (5.2.11.100.fc.29.x.86._.64)
51496
362664
linux
in raku irc channel, its
50941
405340
linux
So even when the platform is linux
i.e $*KERNEL ~~ 'linux'
, there are myriads of output. Is this expected ?
Why I am asking this here is because I cannot test the following code in continuous integration (e.g. github actions) due to this variability even in linux
. The following code fails in GitHub Actions:
use Test;
if $*KERNEL ~~ 'win32' {
srand(1); # From Raku docs: Note that srand is called with a platform dependent value when a Raku program is started.
# This is tested on my own Windows 10
my @x = (1..1000).roll(100);
my @y = (200..7000).roll(100);
is sum(@x), 46221;
is sum(@y), 375477;
}
elsif $*KERNEL ~~ 'linux' {
srand(1); # From Raku docs: Note that srand is called with a platform dependent value when a Raku program is started.
my @x = (1..1000).roll(100);
my @y = (200..7000).roll(100);
is sum(@x), 50941;
is sum(@y), 405340;
}
else {
skip "Not tested on Mac !", 1;
}
I want to make this test work while doing continuous integration.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,srand 是一个低级 nqp 运算符。它针对对
nqp::rand_n< 的调用进行了测试/code>
所以如果你这样做完全相同次数的调用一定会得到相同的结果。恐怕
roll
涉及随机数量的调用,因此即使您在同一平台上执行此操作,也不会得到相同的结果:测试获得的序列是,如果我可能,这不是测试随机性的好策略。有很多可能的策略可以实现这一点,其中一些策略在这里介绍用于随机算法的 TDD 一般来说,你应该检查你想要从中得到什么随机特征:如果你想要一个(粗略的)均匀分布,你应该检查它,或者如果你想要其他东西,同上。在这种情况下,您可能想要的是总和的高斯分布,也许最简单的事情是检查直方图是否大致遵循钟形曲线。
Well, srand is a low-level nqp operator. It's tested against calls to
nqp::rand_n
So if you do exactly the same number of calls you're bound to get the same result. I'm afraid thatroll
involves a random number of calls so you will not have the same result even if you do it in the same platform:Testing the sequences obtained is, if I may, not a good strategy for testing randomness. There are many possible strategies for doing it, some of them are featured here TDD for a random algorithm In general, you should check for what random feature you want out of it: if you want a (rough) uniform distribution, you should check for that, or if you want anything else, ditto. In this case it's likely that what you want is to have a gaussian distribution of sums, maybe the easiest thing would be to check that the histogram follows roughly a bell curve.
我认为唯一设置
srand < / code>可以保证您的事情是在硬件 / OS /软件的特定组合上重复的随机序列。就是这样。
也许Raku模块可以更好地满足您的需求。
I think the only thing setting
srand
guarantees you, is a repeatable random sequence on that particular combination of hardware / OS / software. That's it.Perhaps a Raku module can serve your need better.