Skip to content

🎥 Make videos programmatically with React

further enhance random function

Jonny Burgerd582f6862905
diff --git a/packages/core/src/random.ts b/packages/core/src/random.ts
index 7e55d26cdc..1085f37bab 100644
--- a/packages/core/src/random.ts
+++ b/packages/core/src/random.ts
@@ -5,6 +5,28 @@ function mulberry32(a: number) {
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
}
-export const random = (num: number) => {
- return mulberry32(num * 10000000000);
+function hashCode(str: string) {
+ let i = 0;
+ let chr = 0;
+ let hash = 0;
+
+ for (i = 0; i < str.length; i++) {
+ chr = str.charCodeAt(i);
+ hash = (hash << 5) - hash + chr;
+ hash |= 0; // Convert to 32bit integer
+ }
+ return hash;
+}
+
+export const random = (input: number | string, dummy?: unknown) => {
+ if (dummy !== undefined) {
+ throw new TypeError('random() takes only one argument');
+ }
+ if (typeof input === 'string') {
+ return mulberry32(hashCode(input));
+ }
+ if (typeof input === 'number') {
+ return mulberry32(input * 10000000000);
+ }
+ throw new Error('random() argument must be a number or a string');
};
diff --git a/packages/core/src/test/random.test.ts b/packages/core/src/test/random.test.ts
index 1b1858d85e..e935196e4f 100644
--- a/packages/core/src/test/random.test.ts
+++ b/packages/core/src/test/random.test.ts
@@ -7,20 +7,44 @@ test('Random should be deterministic', () => {
});
test('Random should be uniform', () => {
- const ITEM_COUNT = 10000;
+ const ITEM_COUNT = 100000;
const mapped = new Array(ITEM_COUNT).fill(true).map((a, i) => {
return random(i);
});
+
const average = mapped.reduce((a, b) => a + b, 0) / mapped.length;
+
const tenthPercentile = mapped.filter((m) => m <= 0.1);
const twentiethPercentile = mapped.filter((m) => m <= 0.2 && m >= 0.1);
const ninetiethPercentile = mapped.filter((m) => m <= 0.9 && m >= 0.8);
- expect(average).toBeLessThan(0.501);
- expect(average).toBeGreaterThan(0.499);
+ expect(average).toBeLessThan(0.51);
+ expect(average).toBeGreaterThan(0.49);
expect(tenthPercentile.length).toBeLessThan(ITEM_COUNT * 0.105);
expect(tenthPercentile.length).toBeGreaterThan(ITEM_COUNT * 0.095);
expect(twentiethPercentile.length).toBeLessThan(ITEM_COUNT * 0.105);
expect(twentiethPercentile.length).toBeGreaterThan(ITEM_COUNT * 0.095);
expect(ninetiethPercentile.length).toBeLessThan(ITEM_COUNT * 0.105);
expect(ninetiethPercentile.length).toBeGreaterThan(ITEM_COUNT * 0.095);
+
+ const distances = mapped
+ .map((a, i) => {
+ if (i === 0) {
+ return null;
+ }
+ return Math.abs(mapped[i] - mapped[i - 1]);
+ })
+ .filter((f) => f !== null) as number[];
+ const averageDistance =
+ distances.reduce((a, b) => a + b, 0) / distances.length;
+ expect(averageDistance).toBeGreaterThan(0.3);
+});
+
+test('Random string should be uniform', () => {
+ const alphabet = 'abcdefghijlkmnopqrstuvwxyz0123456789';
+ const array = new Array(alphabet.length)
+ .fill(true)
+ .map((_, i) => random(alphabet[i]));
+ const average = array.reduce((a, b) => a + b, 0) / array.length;
+ expect(average).toBeLessThan(0.55);
+ expect(average).toBeGreaterThan(0.45);
});