Question

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!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top