Skip to content

Commit

Permalink
Fix/set same seed should return the same interval (#114)
Browse files Browse the repository at this point in the history
* Fix/wrong get seed

* Test/add same seed test

* bump version to 4.1.2
  • Loading branch information
ishiko732 committed Aug 15, 2024
1 parent eee4f0c commit 65fd676
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
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

0 comments on commit 65fd676

Please sign in to comment.