前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[salesforce apex] apex 中生成随机码

[salesforce apex] apex 中生成随机码

原创
作者头像
Merry he
修改2023-04-26 10:12:29
4260
修改2023-04-26 10:12:29
举报
文章被收录于专栏:salesforce学习salesforce学习

如果想要生成一次性访问code,可以参考如下:

返回String方式:

代码语言:txt
复制
 * @param length The number of digits to return
 * @return a random number the length of the input param length
 * @description one limitation, will never start with 0
 */
public static String generateRandomNumber(Integer length) {
    String result = '';
    while(result.length() < length){
       //Math.abs used to cast Crypto.getRandomLong() to a positive number
       result += String.valueOf(Math.abs(Crypto.getRandomLong()));
    }
    return result.substring(0,length);
}

返回Integer方式

代码语言:txt
复制
 * @param length The number of digits to return
 * @return a random number the length of the input param length
 * @description one limitation, will never start with 0
 */
public static String generateRandomNumber(Integer length) {
    String result = '';
    while(result.length() < length){
       //Math.abs used to cast Crypto.getRandomLong() to a positive number
       result += String.valueOf(Math.abs(Crypto.getRandomLong()));
    }
    return Integer.valueOf(result.substring(0,length));
}

使用 Math.Random() 生成随机数

代码语言:txt
复制
//Generate a random Double between 0 and 1
System.debug(Math.Random());
//Prints e.g. 0.3128804447173097 

//Generate a random Double between 0 and 10
System.debug(Math.Random() * 10);
//Prints e.g. 5.235707559078412

//Generate a random Integer between 0 and 10
System.debug(Integer.valueOf(Math.Random() * 10));
//Prints e.g. 5

//Generate a random Integer between 0 and 100
System.debug(Integer.valueOf(Math.Random() * 100));

使用加密类生成随机整数

代码语言:txt
复制
System.debug(Crypto.getRandomInteger());
//prints e.g. -1608518142

此文章link:

https://www.levelupsalesforce.com/apex-generate-random-number

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档