This commit is contained in:
2023-08-05 16:03:31 +00:00
parent 92a67315f3
commit 743ab3e1aa
16 changed files with 138 additions and 49 deletions
+19 -19
View File
@@ -5,30 +5,30 @@ export class RandUtil {
}
public nextFloat1(): number {
public nextFloat1(): Promise<number> {
return this.rng.nextFloat1();
}
public nextRange(min: number, max: number): number {
public async nextRange(min: number, max: number): Promise<number> {
const range = max - min;
return this.nextFloat1() * (range) + min;
return await this.nextFloat1() * (range) + min;
}
public nextRangeInt(min: number, max: number): number {
public async nextRangeInt(min: number, max: number): Promise<number> {
const range = max - min;
return this.rng.nextInt(range) + min;
return await this.rng.nextInt(range) + min;
}
public nextInt(max?: number): number {
public nextInt(max?: number): Promise<number> {
return this.rng.nextInt(max);
}
public nextBit(): boolean {
const view = new DataView(this.rng.nextBits(1) as ArrayBufferLike);
public async nextBit(): Promise<boolean> {
const view = new DataView(await this.rng.nextBits(1) as ArrayBufferLike);
return view.getUint8(0) != 0;
}
public nextBool(prob = 0.5): boolean {
public async nextBool(prob = 0.5): Promise<boolean> {
if (prob >= 1) {
return true;
}
@@ -38,10 +38,10 @@ export class RandUtil {
if (prob <= 0){
return false;
}
return this.nextFloat1() < prob;
return await this.nextFloat1() < prob;
}
public shuffle<T>(srcArray: T[]): T[] {
public async shuffle<T>(srcArray: T[]): Promise<T[]> {
const cnt = srcArray.length;
if(cnt === 0){
return [];
@@ -52,7 +52,7 @@ export class RandUtil {
const result: T[] = Array.from(srcArray);
for (let srcIdx = 0; srcIdx < cnt; srcIdx += 1) {
const destIdx = this.rng.nextInt(cnt - srcIdx - 1) + srcIdx;
const destIdx = await this.rng.nextInt(cnt - srcIdx - 1) + srcIdx;
if(srcIdx === destIdx){
continue;
}
@@ -64,12 +64,12 @@ export class RandUtil {
//Object는 integer key에 예외가 있어 shuffleAssoc은 없음
public choice<T>(items: T[] | Record<string | number, T> | Set<T>): T {
public async choice<T>(items: T[] | Record<string | number, T> | Set<T>): Promise<T> {
if (items instanceof Array) {
if(items.length === 0){
throw new Error('Empty items');
}
const idx = this.rng.nextInt(items.length - 1);
const idx = await this.rng.nextInt(items.length - 1);
return items[idx];
}
@@ -77,10 +77,10 @@ export class RandUtil {
return this.choice(Array.from(items.values()));
}
return items[this.choice(Array.from(Object.keys(items)))];
return items[await this.choice(Array.from(Object.keys(items)))];
}
public choiceUsingWeight(items: Record<string | number, number>): string | number {
public async choiceUsingWeight(items: Record<string | number, number>): Promise<string | number> {
if(Object.keys(items).length === 0){
throw new Error('Empty items');
}
@@ -92,7 +92,7 @@ export class RandUtil {
sum += value;
}
let rd = this.nextFloat1() * sum;
let rd = await this.nextFloat1() * sum;
for (const [item, value] of Object.entries(items)) {
if (value <= 0) {
@@ -111,7 +111,7 @@ export class RandUtil {
throw new Error('Unreacheable');
}
public choiceUsingWeightPair<T>(items: [T, number][]): T {
public async choiceUsingWeightPair<T>(items: [T, number][]): Promise<T> {
if(items.length === 0){
throw new Error('Empty items');
}
@@ -123,7 +123,7 @@ export class RandUtil {
sum += value;
}
let rd = this.nextFloat1() * sum;
let rd = await this.nextFloat1() * sum;
for (const [item, value] of items) {
if (value <= 0) {