Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/set same seed should return the same interval #114

Merged
merged 3 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions __tests__/fixed/same-seed.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { createEmptyCard, fsrs, Rating } from '../../src/fsrs'

describe('fuzz same seed', () => {
const MOCK_NOW = new Date(2024, 7, 15)
const size = 100

// https://github.com/open-spaced-repetition/ts-fsrs/issues/113
it('should be the same[short-term]', () => {
const { card } = fsrs().next(createEmptyCard(), MOCK_NOW, Rating.Good)
const scheduler = fsrs({ enable_fuzz: true })
const MOCK_TOMORROW = new Date(2024, 7, 16)

const timestamp: number[] = []
for (let count = 0; count < size; count++) {
setTimeout(() => {
const _card = scheduler.next(card, MOCK_TOMORROW, Rating.Good).card
timestamp.push(_card.due.getTime())
if (timestamp.length === size) {
expect(timestamp.every((value) => value === timestamp[0])).toBe(true)
}
}, 50)
}
})

it('should be the same[long-term]', () => {
const { card } = fsrs({ enable_short_term: false }).next(
createEmptyCard(),
MOCK_NOW,
Rating.Good
)
const scheduler = fsrs({ enable_fuzz: true, enable_short_term: false })
const MOCK_TOMORROW = new Date(2024, 7, 18)

const timestamp: number[] = []
for (let count = 0; count < size; count++) {
setTimeout(() => {
const _card = scheduler.next(card, MOCK_TOMORROW, Rating.Good).card
timestamp.push(_card.due.getTime())
if (timestamp.length === size) {
expect(timestamp.every((value) => value === timestamp[0])).toBe(true)
}
}, 50)
}
})
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-fsrs",
"version": "4.1.1",
"version": "4.1.2",
"description": "ts-fsrs is a versatile package based on TypeScript that supports ES modules, CommonJS, and UMD. It implements the Free Spaced Repetition Scheduler (FSRS) algorithm, enabling developers to integrate FSRS into their flashcard applications to enhance the user learning experience.",
"main": "dist/index.cjs",
"umd": "dist/index.umd.js",
Expand Down
2 changes: 1 addition & 1 deletion src/fsrs/algorithm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export class FSRSAlgorithm {
**/
apply_fuzz(ivl: number, elapsed_days: number, enable_fuzz?: boolean): int {
if (!enable_fuzz || ivl < 2.5) return Math.round(ivl) as int
const generator = alea(this.seed)
const generator = alea(this._seed) // I do not want others to directly access the seed externally.
const fuzz_factor = generator()
const { min_ivl, max_ivl } = get_fuzz_range(
ivl,
Expand Down
2 changes: 1 addition & 1 deletion src/fsrs/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const default_w = [
export const default_enable_fuzz = false
export const defualt_enable_short_term = true

export const FSRSVersion: string = 'v4.1.1 using FSRS V5.0'
export const FSRSVersion: string = 'v4.1.2 using FSRS V5.0'

export const generatorParameters = (
props?: Partial<FSRSParameters>
Expand Down