Skip to main content

Notifications

Dynamics 365 Community / Blogs / The Dynamics 365 Library / Random number generation th...

Random number generation through X++

Faisal Fareed Profile Picture Faisal Fareed 10,794 User Group Leader
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;
}

Comments

*This post is locked for comments