vendredi 24 avril 2015

Ada: How to define a random constant?

I am currently working through John English' "Ada 95: The Craft of Object-Oriented Programming". I am at task 5.1:

Write a program to play a simple guessing game. Define an integer type with a range of values from 1 to 1000 and declare a secret value as a constant of this type, and then give the user ten chances to guess its value.

What I wrote (as a stub) was now

procedure je_5_1 is
  type guess_value is new Integer range 1..1000;
  secret : guess_value;
  package random_guess_value is new Ada.Numerics.Discrete_Random(guess_value);
  Gen : random_guess_value.Generator;
begin
  random_guess_value.Reset(Gen);
  secret := random_guess_value.Random(Gen);
end je_5_1;

Obviously this does not implement the requirement to declare a secret value as a constant. But since I have to call Reset(Gen) before I can assign a randomly generated value to secret, I cannot define the variable secret as a constant before begin.

Is it still possible to define secret as a random constant?




Aucun commentaire:

Enregistrer un commentaire