Sunday, February 15, 2015

Random number generation through X++

AX has buit-in functionality to generate random numbers. You can use Random class (a kernel class, not visible in AOT) for this purpose. There are two more ways to generate random number in X++ One is using RandomGenerate Class which extends Random class and generates values within specified range; Second is using xGlobal::randomPositiveInt32() method.

Example using Random class to generate random numbers (All in CAPITAL letters)

public str generateRandomCode()
{
    str         pass='';
    int         length, counter, randInt,randAscii;
    ;
    length = 4;

    for(counter = 1; counter <= length; counter++)
    {
        randInt = Random.nextInt();
        randAscii = randInt mod 91;

        if((randAscii >= 48 && randAscii <= 57) || (randAscii >= 65 && randAscii <= 90))
        {
            pass += num2char(randAscii);
            continue;
        }
        else
        {
            randAscii = randAscii mod 26 ;
            randAscii += 65 ;
            pass += num2char(randAscii);
            continue;
        }
    }
    return pass;
}

No comments:

Post a Comment

I will appreciate your comments !

How to enable new Microsoft teams - Public Preview!

New Microsoft Teams is just AWESOME, quick but useful post below shows how you have this preview feature to make your life EASY!  Open Micr...