-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
93 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { roundWithStep } from './helpers'; | ||
|
||
type NumberFactory = ((bounds: [number, number, number?, number?], precision?: number) => number) | | ||
((bounds: [number, number, number?], precision?: number) => number); | ||
|
||
function applyMedian(from: number, until: number, median?: number): [number, number] { | ||
if (median === undefined) { | ||
return [from, until]; | ||
} | ||
|
||
if (Math.random() > 0.5) { | ||
return [median, until]; | ||
} | ||
|
||
return [from, median]; | ||
} | ||
|
||
export function createRandomInteger([from, until, median]: [number, number, number?]): number { | ||
[from, until] = applyMedian(from, until, median); | ||
return Math.round(Math.random() * (until - from) + from); | ||
} | ||
|
||
export function createRandomFloat( | ||
[from, until, median, step]: [number, number, number?, number?], | ||
precision?: number, | ||
): number { | ||
[from, until] = applyMedian(from, until, median); | ||
|
||
let result = Math.random() * (until - from) + from; | ||
if (step !== undefined && step !== 0) { | ||
result = roundWithStep(result, step, precision); | ||
} | ||
return result; | ||
} | ||
|
||
export function randomizeMatrix( | ||
count: number, | ||
bounds: [number, number, number?, number?] | [number, number, number?], | ||
numberFactory: NumberFactory, | ||
symmetric: boolean = false, | ||
precision?: number, | ||
): number[][] { | ||
const result: number[][] = []; | ||
for (let i=0; i<count; ++i) { | ||
result.push([]); | ||
for (let j=0; j<count; ++j) { | ||
if (symmetric && i > j) { | ||
result[i].push(result[j][i]); | ||
} else { | ||
result[i].push(numberFactory(bounds as [number, number], precision)); | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
export function normalizeFrequencies(weights: number[]): number[] { | ||
const sum = weights.reduce((a, b) => a + b); | ||
return weights.map((x) => x / sum); | ||
} | ||
|
||
export function getIndexByFrequencies(frequencies: number[]): number { | ||
const normFrequencies = normalizeFrequencies(frequencies); | ||
const rand = Math.random(); | ||
let sum = 0; | ||
for (let i=0; i<normFrequencies.length; ++i) { | ||
sum += normFrequencies[i]; | ||
if (rand <= sum) { | ||
return i; | ||
} | ||
} | ||
return 0; | ||
} |