Random number generation through X++

Random number generation through X++

MCP · Azure DevOpsYour delivery board, readable by an agentMCP · DataverseOne consent, and the data layer opensMCP · Dynamics 365 F&OConnecting F&O to an agent Microsoft hasn’t documented
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)

Code
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;
}
Previous article← Exchange rate cannot be found for exchange rate type default between currenies USD & NZD
Discussion

Ask a question or leave feedback

This is a review interaction. The WordPress version will store, thread and moderate comments.

Thanks — submitted for moderation in this review.
Search the whole site · Ctrl/⌘ + K