DEV Community

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

Posted 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

Top comments (0)