艾达 - Discrete_Random 例子

Ada - Discrete_Random example

John Barnes 的 "Programming in Ada 2012" 第 53 页分享了一段不完整的代码片段,我无法开始工作。

我想出了一个完整的程序来扩展书中的代码...

with Ada.Numerics; use Ada.Numerics;
with Ada.Text_IO; use Ada.Text_IO;

procedure Main is
  type Coin is (Heads, Tails);
  package Random_Coin is new Discrete_Random(Coin);
  use Random_Coin;

  G : Generator;
  C : Coin;
begin

  for i in 1 .. 20 loop
    C := Random(G);
    Put (C'Image);
  end loop;  

end Main;

"GPS"IDE我在使用时报错如下:

IDE 确实给了我 "intellisense"(使用 Visual Studio 中的一个术语)表明 Discrete_Random 实际上是可见的并且可用 "with" 和 "use" 我添加的语句。

有人可以帮我解决我犯的愚蠢错误吗?

问题在于,与 Ada.Numerics.Pi 不同,PiAda.Numerics 的组成部分,Discrete_RandomAda.Numerics 的子项。

一旦你说了 use Ada.Numerics,你可以在你的程序中只写 Pi,但你实际上必须 with Ada.Numerics.Discrete_Random 才能使它可用。

事实上,你不需要withuseAda.Numerics,这样就可以了:

with Ada.Numerics.Discrete_Random;
with Ada.Text_IO; use Ada.Text_IO;

procedure Main is
   type Coin is (Heads, Tails);
   package Random_Coin is new Ada.Numerics.Discrete_Random(Coin);