How to generate random code with limited range

selece28selece28 Member Posts: 316
edited 2012-10-03 in NAV Tips & Tricks
Hi Nav Masters
I have a problems, i need to generate 8 digit Code which each digit is random code consist of : number 2 to 9 (without 0 and 1) or A to Z or a to z

Can we do this?
How to do it in navision c/al code?

Thanks in advance,
______________

Regards,
Steven

Comments

  • ara3nara3n Member Posts: 9,256
    RandomDigit is of type text. length 50
    RandomDigit := CREATEGUID;
    RandomDigit := DELCHr(RandomDigit,'=','{}-01');
    RandomDigit := copystr(RandomDigit,1,8);
    Message(RandomDigit)
    
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • ara3nara3n Member Posts: 9,256
    You can also use


    randomize;
    RandomDigit := format(random(7)+2);

    but the problem is that randomize needs a seed that is unique every time you call it.
    otherwise it uses system time, and so if you call more than once within a second it will return the same value.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • selece28selece28 Member Posts: 316
    Hi Rashed,
    Thanks for the reply.
    I think the 1st one is great, but what is the : CREATEGUID used for?
    I don't understand, can you explain a little?

    Ow and one more, random cannot randomize char right?

    Thanks
    ______________

    Regards,
    Steven
  • David_SingletonDavid_Singleton Member Posts: 5,479
    ara3n wrote:
    RandomDigit is of type text. length 50
    RandomDigit := CREATEGUID;
    RandomDigit := DELCHr(RandomDigit,'=','{}-01');
    RandomDigit := copystr(RandomDigit,1,8);
    Message(RandomDigit)
    

    =D> cool idea.
    David Singleton
  • kinekine Member Posts: 12,562
    selece28 wrote:
    Hi Rashed,
    Thanks for the reply.
    I think the 1st one is great, but what is the : CREATEGUID used for?
    I don't understand, can you explain a little?

    CRATEGUID is generating GUID which is "Globally Unique Identifier". You can google more about that. It is some "code" which you can use as unique, because the chance, that somebody will generate same GUID on the planet is small. It means that the GUID is "random" and the code use this to generate random data and taking some character from the result as random...

    Ow and one more, random cannot randomize char right?

    Thanks

    Char is just number presented as text. And because you can convert number to char and back, you can "randomize" char... Just random integer and assign it into Char variable...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • krikikriki Member, Moderator Posts: 9,112
    [Topic moved from Navision forum to Navision Tips & Tricks forum]
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • mabl4367mabl4367 Member Posts: 143
    If you use the GIUD sugestion you may get unlucky and get a string with less then 8 characters if the GIUD generated containes less then 8 valid characters.

    Instead create a character array that holds all the valid characters. Then generate 8 radom numbers between 1 and the length of the array and add the corresponding array element to your codestring.
    validChar[1]:='2';
    validChar[2]:='3';
    .
    .
    .
    validChar[7]:='9';
    validChar[8]:='a';
    validChar[9]:='b';
    .
    .
    .
    validChar[32]:='z';
    validChar[33]:='A';
    validChar[34]:='B';
    .
    .
    .
    validChar[57]:='Z';
    
    codeString:='';
    RANDOMIZE();
    WHILE STRLENGTH(codeString)<8 DO BEGIN
      codeString:=codeString+validChar[RANDOM(57)];  
    END;
    
    
Sign In or Register to comment.