-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
d3f1b3e
commit 2ad5ef0
Showing
7 changed files
with
69 additions
and
16 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,46 @@ | ||
import { sample, shuffle } from '../random' | ||
|
||
describe('shuffle', () => { | ||
it('basic test', () => { | ||
const a = [0] | ||
for (let i = 0; i < 100; i++) { | ||
expect(shuffle(a)).toEqual(a) | ||
} | ||
|
||
const b = [0, 1, 2, 3, 4, 5] | ||
const outOfPosition = new Set<number>() | ||
testb: for (let i = 0; i < 100; i++) { | ||
const shuffled = shuffle(b) | ||
for (let j = 0; j < shuffled.length; j++) { | ||
if (j !== shuffled[j]) { | ||
outOfPosition.add(j) | ||
if (outOfPosition.size === b.length) { | ||
break testb | ||
} | ||
} | ||
} | ||
} | ||
expect(outOfPosition.size).toBe(b.length) | ||
}) | ||
}) | ||
|
||
describe('sample', () => { | ||
it('basic test', () => { | ||
const a = [0] | ||
for (let i = 0; i < 100; i++) { | ||
expect(sample(a)).toBe(0) | ||
} | ||
|
||
const b = [0, 1, 2, 3, 4, 5] | ||
const sampled = new Set<number>() | ||
for (let i = 0; i < 100; i++) { | ||
const v = sample(b) | ||
expect(v).toBeDefined() | ||
sampled.add(v) | ||
if (sampled.size === b.length) { | ||
break | ||
} | ||
} | ||
expect(sampled.size).toBe(b.length) | ||
}) | ||
}) |
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,16 @@ | ||
export function shuffle<T>(items: T[]): T[] { | ||
const res = items.slice() | ||
let prev = Math.floor(Math.random() * items.length) | ||
for (let i = 0; i < items.length; i++) { | ||
const next = Math.floor(Math.random() * items.length) | ||
const t = res[prev] | ||
res[prev] = res[next] | ||
res[next] = t | ||
prev = next | ||
} | ||
return res | ||
} | ||
|
||
export function sample<T>(items: T[]): T { | ||
return items[Math.floor(Math.random() * items.length)] | ||
} |
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