samedi 2 décembre 2017

ADA: "bug" when generating a random string with two functions

I have a problem with my code.

Basically, what I'm doing is trying to generate a random string of character, while being able to decide easily of its length and of which character I want to include in it. In order to do that, I have a first function that return an integer in a choosen interval.
Then I have a second function, which concatenates different arrays ( each containing a different set of characters ) when modifying its boolean parameters, and then uses the first function to choose a random character in the new created array.

with ada.text_io, ada.integer_text_io, ada.float_text_io, ada.numerics.discrete_random;
use  ada.text_io, ada.integer_text_io, ada.float_text_io;

Procedure test4  is

    function RandomInt (a,b: integer) return integer is
        Type Int is new integer range a..b;
        package RandomInteger is new ada.numerics.discrete_random (Int);
        G: RandomInteger.Generator;
    begin
        RandomInteger.reset (G);
        return (integer(RandomInteger.random(G)));
    end RandomInt;

    function RandomStr (a: integer; Low, Up, Num: boolean := true; Hex, Sp: boolean := false) return string is
        Type Table is array (integer range <>) of character;
        S   : Table (1..32) := ('!','"','#','$','%','&',''','(',')','*','+',',','-','.','/',':',';','<','=','>','?','@','[','\',']','^','_','`','{','|','}','~');
        U   : Table (1..26) := ('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
        L   : Table (1..26) := ('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
        N   : Table (1..10) := ('0','1','2','3','4','5','6','7','8','9');
        O   : Table (0.. 0) ;

        T   : Table   :=  ((if Low then ( if Hex then L(1..4) else L) else O)
                         & (if Up  then ( if Hex then U(1..4) else U) else O) 
                         & (if Num then N else O) 
                         & (if Sp  then S else O));

        Str : string (1..a);
        car : character;
    begin
        for i in 1..a loop
            Str(i) := T(RandomInt(1, T'length));
        end loop;
        return (Str);
    end RandomStr;

    ID: string (1..5);

begin

    ID := RandomStr(5, Hex => true);
    put (ID);

end test4;

So the code compile, no problem about it, but the size of the string is not always the size it should be ( 5 in my example ). It some times is 4, sometimes 3, I've even seen 2 characters only and don't understand why.

I tried to add some small debug lines, in the first function,

put (integer(RandomInteger.random(G)));

shows that there's no problem with generating random integer. In the second function,

put (T'length);
for j in 1..T'length loop
    Put ( T(j) );
end loop;

shows that there is no problem with concatenating the arrays ( whether Hex is true or not ) and calculating its size. But when I

Put (T(RandomInt(1, T'length)));

It doesn't always return a character. Can someone see why?




Aucun commentaire:

Enregistrer un commentaire