2
* Copyright (c) 2024 Huawei Device Co., Ltd.
3
* Licensed under the Apache License, Version 2.0 (the "License");
4
* you may not use this file except in compliance with the License.
5
* You may obtain a copy of the License at
7
* http://www.apache.org/licenses/LICENSE-2.0
9
* Unless required by applicable law or agreed to in writing, software
10
* distributed under the License is distributed on an "AS IS" BASIS,
11
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
* See the License for the specific language governing permissions and
13
* limitations under the License.
16
let rand: XorRand|undefined = undefined
18
const MASK = 0x7FFFFFFF
20
export function initRNG() {
23
console.log(`Use IDLIZE_SEED env variable to set a seed value.`)
24
console.log(`Set IDLIZE_SEED env variable to RANDOM to use a random seed value.`)
25
console.log(`IDLIZE_SEED=${process.env.IDLIZE_SEED}`)
27
if (process.env.IDLIZE_SEED === undefined) {
29
console.log(`Env IDLIZE_SEED variable is not.`)
30
console.log(`Use predefined seed: ${seed}.`)
31
} else if (process.env.IDLIZE_SEED == "RANDOM") {
32
seed = Math.floor(Math.random() * 4096)
33
console.log(`Use random seed: ${seed}`)
35
seed = Number(process.env.IDLIZE_SEED)
36
console.log(`Use seed: ${seed}`)
39
rand = new XorRand(seed)
45
constructor(seed: number) {
50
x ^= (x << 13) & MASK;
51
x ^= (x >> 17) & MASK;
59
export function randInt(max: number, min: number = 0) {
60
return Math.floor(rand!.random() * (max - min)) + min;
63
function randChar(minChar: string, range: number) {
64
return String.fromCharCode(minChar.charCodeAt(0) + randInt(range))
67
export function randString(max: number): string {
69
let array: string[] = []
70
for (let i = 0; i < max; i++) {
71
const range = randInt(3)
83
export function pick<K, V>(keys: K[], gen: (key: K) => string[], pickedNumbers: number = 3): string[] {
85
let values: string[][] = keys.map(it => gen(it))
86
let picked: string[] = []
88
if (values.map(it => it.length).some(it => it == 0)) {
89
console.log("Empty arguments!")
93
for (let _ = 0; _ < pickedNumbers; _++) {
95
for (let i = 0; i < values.length; i++) {
96
let len = values[i].length
97
let index = randInt(len)
98
let elem = values[i][index]
101
picked.push(v.join(","))
104
return [...new Set(picked)]
107
export function pickArray<T>(values: T[], maxLen: number, pickedNumbers: number = 3): string[] {
109
let picked: string[] = ["[]"]
111
for (let _ = 0; _ < pickedNumbers; _++) {
112
let len = randInt(maxLen)
114
for (let i = 0; i < len; i++) {
115
p.push(values[randInt(values.length)])
117
picked.push(`[${p.join(",")}]`)
120
return [...new Set(picked)]