Solution: Use the Math.random() to generate the random number.
Example:
To generate 1 digit random number,use the code below
Integer oneDigitRandomNumber = Integer.valueof((Math.random() * 10));
system.debug('1 Digit Random Number :'+oneDigitRandomNumber);
Output:
To generate 4 digit random number,use the code below
Integer fourDigitRandomNumber = Integer.valueof((Math.random() * 10000));
if(String.valueOf(fourDigitRandomNumber).length() != 4) {
fourDigitRandomNumber = Integer.valueOf(String.valueOf(fourDigitRandomNumber)+'0');
}
system.debug('4 Digit Random Number :'+fourDigitRandomNumber);
Output:
To generate 6 digit random number,use the code below
Integer sixDigitRandomNumber = Integer.valueof((Math.random() * 1000000));
if(String.valueOf(sixDigitRandomNumber).length() != 6) {
sixDigitRandomNumber = Integer.valueOf(String.valueOf(sixDigitRandomNumber)+'0');
}
system.debug('6 Digit Random Number :'+sixDigitRandomNumber);
Output:
No comments:
Post a Comment