(min: number, max: number, count: number, total: number, isEven: boolean)
| 25 | |
| 26 | |
| 27 | export const randVal = (min: number, max: number, count: number, total: number, isEven: boolean): number[] => { |
| 28 | |
| 29 | const arr: number[] = Array(count).fill(total / count); |
| 30 | if (isEven) return arr |
| 31 | |
| 32 | if (max * count < total) |
| 33 | throw new Error("Invalid input: max * count must be greater than or equal to total.") |
| 34 | if (min * count > total) |
| 35 | throw new Error("Invalid input: min * count must be less than or equal to total.") |
| 36 | const average = total / count |
| 37 | // Randomize pairs of elements |
| 38 | for (let i = 0; i < count; i += 2) { |
| 39 | // Generate a random adjustment within the range |
| 40 | const adjustment = Math.random() * Math.min(max - average, average - min) |
| 41 | // Add adjustment to one element and subtract from the other |
| 42 | arr[i] += adjustment |
| 43 | arr[i + 1] -= adjustment |
| 44 | } |
| 45 | // if (count % 2) arr.pop() |
| 46 | return arr; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | export const saveDataToFile = (newData: Data[], filePath: string = "data.json") => { |
nothing calls this directly
no outgoing calls
no test coverage detected