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 !

Download large bacpac (sandbox database) to DEV environment much faster

As the LCS website gets slower and slower and the database backups get bigger and bigger.  Use AZCopy to download objects out of LCS asset l...