DEV Community

桃花镇童长老
桃花镇童长老

Posted on • Edited on

RandomUtil, random tool class

Introduction and description of harmony-utils


harmony-utils A HarmonyOS tool library with rich features and extremely easy to use, with the help of many practical tools, is committed to helping developers quickly build Hongmeng applications. Its encapsulated tools cover APP, device, screen, authorization, notification, inter-thread communication, pop-up frames, toast, biometric authentication, user preferences, taking photos, albums, scanning codes, files, logs, exception capture, characters, strings, numbers, collections, dates, random, base64, encryption, decryption, JSON and other functions, which can meet various development needs.

picker_utils It is a sub-store split by harmony-utils, including PickerUtil, PhotoHelper, and ScanUtil.

Download and install

ohpm i @pura/harmony-utils

ohpm i @pura/picker_utils

  //Global initialization method, initialized in the onCreate method of UIAbility AppUtil.init()
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    AppUtil.init(this.context);
  }
Enter fullscreen mode Exit fullscreen mode

API methods and usage


getRandomBoolean generates random Boolean values
let bl = RandomUtil.getRandomBoolean();
Enter fullscreen mode Exit fullscreen mode
getRandomInt generates random integers (the range can be specified)
let n1 = RandomUtil.getRandomInt();
Enter fullscreen mode Exit fullscreen mode
getRandomNumber Generates a random number within a specified range
let n3 = RandomUtil.getRandomNumber(10000, 1000000);
Enter fullscreen mode Exit fullscreen mode
getRandomLimit generates a random number within the specified range [0,limit)
let n2 = RandomUtil.getRandomLimit(1000);
Enter fullscreen mode Exit fullscreen mode
getRandomChineseChar generates a random Chinese character
let str1 = RandomUtil.getRandomChineseChar();
Enter fullscreen mode Exit fullscreen mode
getRandomChinese generates random Chinese characters
let str2 = RandomUtil.getRandomChinese(12);
Enter fullscreen mode Exit fullscreen mode
getRandomStr randomly generates strings of specified length according to the specified string.
let r1 = RandomUtil.getRandomStr(15, "ACCDEFGHIJKMNL");
Enter fullscreen mode Exit fullscreen mode
getRandomDataBlob Generates DataBlob of randomly specified length
let dataBlob = RandomUtil.getRandomDataBlob(8);
Enter fullscreen mode Exit fullscreen mode
getRandomUint8Array generates a Uint8Array of randomly specified length
let unit8Array = RandomUtil.getRandomUint8Array(8);
Enter fullscreen mode Exit fullscreen mode
getRandomColor generates random colors in hexadecimal
let c3 = RandomUtil.getRandomColor();
Enter fullscreen mode Exit fullscreen mode
generateUUID36 generates 36-bit UUID with -
let uuid36 = RandomUtil.generateUUID36();
Enter fullscreen mode Exit fullscreen mode
generateUUID32 generates 32-bit UUID with -
let uuid32 = RandomUtil.generateUUID32();
Enter fullscreen mode Exit fullscreen mode
generateRandomUUID generates a random RFC 4122 version 4 string type UUID using the Encrypted Secure Random Number Generator
let uuid1 = RandomUtil.generateRandomUUID();
Enter fullscreen mode Exit fullscreen mode
generateRandomBinaryUUID generates random RFC 4122 version 4 Uint8Array type UUID using Encrypted Secure Random Number Generator
let uuid2 = RandomUtil.generateRandomBinaryUUID();
Enter fullscreen mode Exit fullscreen mode

Full Code Example


import { router } from '@kit.ArkUI';
import { MockSetup } from '@ohos/hamock';
import { LogUtil, RandomUtil, StrUtil } from '@pura/harmony-utils';
import { TitleBarView } from '../../component/TitleBarView';
import { DescribeBean } from '../../model/DescribeBean';
import { Utils } from '../../utils/Utils';

/**
 * 随机工具类
 */
@Entry
@Component
struct Index {
  private scroller: Scroller = new Scroller();
  @State describe: DescribeBean = router.getParams() as DescribeBean;

  @MockSetup
  mock() {
    this.describe = new DescribeBean("RandomUtil", "随机工具类");
  }

  build() {
    Column() {
      TitleBarView({ describe: this.describe })
      Divider()
      Scroll(this.scroller) {
        Column() {
          Button("getRandomInt()\ngetRandomLimit()\ngetRandomNumber()")
            .labelStyle({ maxLines: 3 })
            .type(ButtonType.Normal)
            .borderRadius(10)
            .padding({ top: 10, bottom: 10 })
            .btnStyle()
            .onClick(() => {
              let n1 = RandomUtil.getRandomInt();
              let n2 = RandomUtil.getRandomLimit(1000);
              let n3 = RandomUtil.getRandomNumber(10000, 1000000);
              let txtStr = `随机数1:${n1}\n随机数2:${n2}\n随机数3:${n3}`;
              LogUtil.error(txtStr);
              Utils.showSheetText(txtStr);
            })
          Button("getRandomChineseChar()\ngetRandomChinese()")
            .labelStyle({ maxLines: 3 })
            .type(ButtonType.Normal)
            .borderRadius(10)
            .padding({ top: 10, bottom: 10 })
            .btnStyle()
            .onClick(() => {
              let str1 = RandomUtil.getRandomChineseChar();
              let str2 = RandomUtil.getRandomChinese(12);
              let txtStr = `随机汉字:${str1}\n随机汉字:${str2}`;
              LogUtil.error(txtStr);
              Utils.showSheetText(txtStr);
            })
          Button("getRandomBoolean()\ngetRandomColor()")
            .labelStyle({ maxLines: 3 })
            .type(ButtonType.Normal)
            .borderRadius(10)
            .padding({ top: 10, bottom: 10 })
            .btnStyle()
            .onClick(() => {
              let b1 = RandomUtil.getRandomBoolean();
              let b2 = RandomUtil.getRandomBoolean();
              let c3 = RandomUtil.getRandomColor();
              let txtStr = `随机Boolean值1:${b1}\n随机Boolean值2:${b2}\n随机颜色:${c3}`;
              LogUtil.error(txtStr);
              Utils.showSheetText(txtStr);
            })
          Button("getRandomStr()")
            .btnStyle()
            .onClick(() => {
              let r1 = RandomUtil.getRandomStr(15, "ACCDEFGHIJKMNL");
              let r2 = RandomUtil.getRandomStr(12, "accdefghijkmnl");
              let r3 = RandomUtil.getRandomStr(16, "1234567890");
              let r4 = RandomUtil.getRandomStr(10, "哈呵嘿哼噢哦呸嗷");
              let txtStr = `随机1:${r1}\n随机2:${r2}\n随机3:${r3}\n随机4:${r4}`;
              LogUtil.error(txtStr);
              Utils.showSheetText(txtStr);
            })
          Button("getRandomDataBlob()\ngetRandomUint8Array()")
            .labelStyle({ maxLines: 3 })
            .type(ButtonType.Normal)
            .borderRadius(10)
            .padding({ top: 10, bottom: 10 })
            .btnStyle()
            .onClick(() => {
              let dataBlob = RandomUtil.getRandomDataBlob(8);
              let unit8Array = RandomUtil.getRandomUint8Array(8);
              let txtStr = `dataBlob:\n${dataBlob.data}\n\n3unit8Array:\n${unit8Array}`;
              LogUtil.error(txtStr);
              Utils.showSheetText(txtStr);
            })

          Button("generateUUID36()\ngenerateUUID32()")
            .labelStyle({ maxLines: 3 })
            .type(ButtonType.Normal)
            .borderRadius(10)
            .padding({ top: 10, bottom: 10 })
            .btnStyle()
            .onClick(() => {
              let uuid36 = RandomUtil.generateUUID36();
              let uuid32 = RandomUtil.generateUUID32();
              let txtStr = `36位UUID:\n${uuid36}\n\n32位UUID:\n${uuid32}`;
              LogUtil.error(txtStr);
              Utils.showSheetText(txtStr);
            })
          Button("generateRandomUUID()\ngenerateRandomBinaryUUID")
            .labelStyle({ maxLines: 3 })
            .type(ButtonType.Normal)
            .borderRadius(10)
            .padding({ top: 10, bottom: 10 })
            .btnStyle()
            .onClick(() => {
              let uuid1 = RandomUtil.generateRandomUUID();
              let uuid2 = RandomUtil.generateRandomBinaryUUID();
              let txtStr = `使用加密安全随机数生成器生成随机的string类型UUID:\n${uuid1}\n\n使用加密安全随机数生成器生成随机的Uint8Array类型UUID:\n${uuid2}`;
              LogUtil.error(txtStr);
              Utils.showSheetText(txtStr);
            })

          Blank().layoutWeight(1)
        }
        .margin({ top: 5, bottom: 5 })
      }
      .layoutWeight(1)
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Start)
    .backgroundColor($r('app.color.main_background'))
  }
}


@Styles
function btnStyle() {
  .width('90%')
  .margin({ top: 10, bottom: 5 })
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)