I have very definitively come across the 'simulating a 6-faced die' (which produces a random integer between 1 and 6, all outcomes are equally probable) in Java, Python, Ruby and Bash. However, I am yet to see a similar program in Ada. Has anyone开发者_Python百科 come across one?
See Random Number Generation (LRM A.5.2) for packages to assist with doing this. Either Ada.Numerics.Float_Random for uniform random number generation (range 0.0 .. 1.0) which you can then scale on your own, or instantiate Ada.Numerics.Discrete_Random with a suitable (sub)type (works for d4, d10, d12, and d20s as well!).
You might enjoy this simulation of the children's card game of war, which uses an instance of Ada.Numerics.Discrete_Random
.
subtype Card_Range is Positive range 1 .. 52;
package Any_Card is new Ada.Numerics.Discrete_Random(Card_Range);
G : Any_Card.Generator;
…
N : Card_Range := Any_Card.Random(G);
With Ada 95, a random number generator was defined as part of the standard library making it a required component of every Ada 95 compilation system.
Therefore, yes you can simulate a 6-faced die in Ada quite easily.
RossetaCode.org usually have these kind of typical programs. You can find a simple 6-faced dice implementation in Pig the dice game.
These are the relevant parts of that program for a dice implementation.
You define the wanted range in a type:
type Dice_Score is range 1 .. 6;
instantiate Ada.Numerics.Discrete_Random with your type:
with Ada.Numerics.Discrete_Random;
package RND is new Ada.Numerics.Discrete_Random(Dice_Score);
Use the instantiation to get a random value in the range:
Gen: RND.Generator;
P.Recent_Roll := RND.Random(Gen);
精彩评论