4

次の Prolog プログラムを YAP で実行すると、出力は常に同じ、つまり整数 233 になります。

:- use_module(library(random)).    
x:- random(1,1000,X), writeln(X).

たとえば、次の bash スクリプトを実行すると、出力は常に同じ整数 (233) になります。

for k in `seq 0`
do
  yap -l test.pl << %
  x.
%
done

swipl を使用してこの手順を繰り返すと、出力は毎回異なります。つまり、ランダムです。

誰でもこれを説明できますか?

4

2 に答える 2

4

First things first!

For many good reasons (reproducibility of past results being the most important one) computer programs do not work with actual random numbers, but with pseudo random numbers.

PRNGs are fully deterministic functions that, given the same internal state (a.k.a. "seed") as initialization, produce exactly the same sequence of numbers (from now until eternity).

Quick fix: find yourself a suitable seed (date, time, phase of the moon, ...) and explicitly initialize the PRNG with that seed. Record the seed so you can later deterministically re-run past experiments.

于 2015-10-08T17:28:46.110 に答える
2

通常、乱数発生器には set_seed(SomeReallyRandomValue) 呼び出しのようなものが必要です。C では、seed(time(0)) がよく使用されていました。だから、私は推測します

datime(datime(_Year, _Month, _DayOfTheMonth, _Hour, Minute, Second)),
X is Minute * Second,Y=X,Z=X,
setrand(rand(X,Y,Z)),

働くことができた

于 2015-10-08T17:22:44.127 に答える