HarmonyOS 获取18位随机数没有生效

参考代码:

let random18Str = ''
   //取18位随机数
   let rand = cryptoFramework.createRandom();
   try {
     let randData = rand.generateRandomSync(18);
     if (randData != null) {
       random18Str = randData.data.toString()
       console.info('[Sync]: rand result: ' + randData.data);
     } else {
       console.error("[Sync]: get rand result fail!");
     }
   } catch (error) {
     let e: BusinessError = error as BusinessError;
     console.error(`sync error, ${e.code}, ${e.message}`);
   }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
HarmonyOS
2024-12-27 15:27:16
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

可通过以下代码生成18位随机数。

let ran : number = Math.ceil((Math.pow(10, 18) - 1) * Math.random());
  • 1.
分享
微博
QQ
微信
回复
2024-12-27 16:47:16
相关问题
HarmonyOS 16随机数的方法
649浏览 • 1回复 待解决
HarmonyOS 随机数获取
627浏览 • 1回复 待解决
在Hi3861怎么获取一个随机数
12801浏览 • 1回复 已解决
HarmonyOS 获取系统时间戳没有生效
1106浏览 • 1回复 待解决
HarmonyOS 代码混淆配置没有生效
754浏览 • 1回复 待解决
16进制与结果异常
582浏览 • 1回复 待解决