-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from PAIR-code/all-items
All items added, with their images, seed the database with 5 random pairs
- Loading branch information
Showing
9 changed files
with
202 additions
and
57 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,4 @@ | |
"devDependencies": { | ||
"@types/node": "^20.12.7" | ||
} | ||
} | ||
} |
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
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,55 @@ | ||
/** Random utilities that support seeding. */ | ||
|
||
// ********************************************************************************************* // | ||
// SEED // | ||
// ********************************************************************************************* // | ||
|
||
// Seed shared by all random functions. | ||
let RANDOM_SEED = 0; | ||
|
||
/** Initialize the seed with a custom value */ | ||
export const seed = (value: number) => { | ||
RANDOM_SEED = value; | ||
}; | ||
|
||
/** Update the seed using a Linear Congruential Generator */ | ||
const next = (a = 1664525, b = 1013904223, m = 2 ** 32) => { | ||
RANDOM_SEED = (a * RANDOM_SEED + b) % m; | ||
return RANDOM_SEED; | ||
}; | ||
|
||
// ********************************************************************************************* // | ||
// RANDOM FUNCTIONS // | ||
// ********************************************************************************************* // | ||
|
||
/** Returns a random floating point value in [0, 1] */ | ||
export const random = () => next() / 2 ** 32; | ||
|
||
/** Returns a random integer in [min, max] */ | ||
export const randint = (min: number, max: number) => Math.floor(random() * (max - min + 1)) + min; | ||
|
||
/** Chooses a random value from an array. The array is not modified. */ | ||
export const choice = <T>(array: readonly T[]): T => { | ||
if (array.length === 0) { | ||
throw new Error('Cannot choose from an empty array'); | ||
} | ||
|
||
return array[randint(0, array.length - 1)]; | ||
}; | ||
|
||
/** Chooses n random distinct values from an array. The array is not modified. */ | ||
export const choices = <T>(array: readonly T[], n: number): T[] => { | ||
if (array.length <= n) { | ||
throw new Error(`Cannot choose ${n} distinct values from an array of length ${array.length}`); | ||
} | ||
|
||
const copy = [...array]; | ||
const result: T[] = []; | ||
for (let i = 0; i < n; i++) { | ||
const index = randint(0, copy.length - 1); | ||
result.push(copy[index]); | ||
copy.splice(index, 1); | ||
} | ||
|
||
return result; | ||
}; |
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