How to generate random number using Apex?

Integer len = 5;
String str = string.valueof(Math.abs(Crypto.getRandomLong()));
String randomNumber = str.substring(0, len);
system.debug('Random Number-' + randomNumber);

Leave a Reply