[215_18] 实现 (liii random) 和 (srfi srfi-27) 随机数模块#666
Merged
Conversation
Contributor
Greptile Summary本 PR 实现了 主要变更:
发现的问题:
Confidence Score: 3/5存在一个 SRFI-27 规范违反(unit 参数被忽略)和两个测试逻辑问题,建议修复后合并。 核心随机数生成功能实现正确,但 random-source-make-reals 的 unit 参数被接受和校验后未实际使用,违反 SRFI-27 规范;random-real-test.scm 末尾范围检查用了闭区间与规范矛盾;randomize 测试存在时间竞争条件。这些问题影响了规范符合度和测试可靠性,因此扣分。 重点关注 goldfish/srfi/srfi-27.scm(unit 参数逻辑 bug、初始 seed 问题)以及 tests/liii/random/random-real-test.scm 和 tests/liii/random/random-source-randomize-test.scm(测试逻辑问题)。 Important Files Changed
Sequence DiagramsequenceDiagram
participant U as 用户代码
participant LR as (liii random)
participant S27 as (srfi srfi-27)
participant S7 as s7 random 内置
U->>LR: (import (liii random))
LR->>S27: re-export all symbols
U->>S27: (make-random-source)
S27->>S7: (random-state 0)
S7-->>S27: state 对象
S27-->>U: random-source 记录
U->>S27: (random-source-pseudo-randomize! s i j)
S27->>S7: (random-state seed carry)
S7-->>S27: 新 state
S27->>S27: set! state 更新闭包变量
U->>S27: (random-source-make-integers s)
S27-->>U: (lambda (n) (random n state))
U->>S7: rand-int 调用 → (random n state)
S7-->>U: 随机整数 [0, n-1]
U->>S27: (random-source-state-ref s)
S27->>S7: (random-state->list state)
S7-->>U: (random-source-state seed carry)
U->>S27: (random-source-state-set! s saved)
S27->>S7: (random-state seed carry)
S27->>S27: set! state 恢复闭包变量
Reviews (1): Last reviewed commit: "[215_18] 实现 (liii random) 和 (srfi srfi-2..." | Re-trigger Greptile |
Comment on lines
+155
to
+177
| (lambda args | ||
| (let ((unit #f)) | ||
| (if (pair? args) | ||
| (begin | ||
| (set! unit (car args)) | ||
| (unless (and (real? unit) (< 0 unit 1)) | ||
| (error 'wrong-type-arg "random-source-make-reals: unit must be a real in (0,1)" unit) | ||
| ) ;unless | ||
| ) ;begin | ||
| ) ;if | ||
| (lambda () | ||
| (let ((r (random 1.0 state))) | ||
| ;; random returns [0.0, 1.0), but SRFI-27 requires (0, 1) | ||
| ;; s7's random for reals already returns (0, 1) when n > 0 | ||
| ;; But we need to ensure we never return 0 or 1 | ||
| (if (zero? r) | ||
| 0.0000000000000001 ; smallest positive value | ||
| r | ||
| ) ;if | ||
| ) ;let | ||
| ) ;lambda | ||
| ) ;let | ||
| ) ;lambda |
Contributor
Comment on lines
+44
to
+53
| ; 多次随机化产生不同状态 | ||
| (let ((s (make-random-source))) | ||
| (random-source-randomize! s) | ||
| (let ((state1 (random-source-state-ref s))) | ||
| (random-source-randomize! s) | ||
| (let ((state2 (random-source-state-ref s))) | ||
| (check (not (equal? state1 state2)) => #t) | ||
| ) | ||
| ) | ||
| ) |
Contributor
Comment on lines
+42
to
+44
| (check (>= r 0.0) => #t) | ||
| (check (<= r 1.0) => #t) | ||
| ) |
Contributor
Comment on lines
+99
to
+100
| (define (make-random-source) | ||
| (let ((state (random-state 0))) ; Create initial state with seed 0 |
Contributor
Comment on lines
+170
to
+173
| (if (zero? r) | ||
| 0.0000000000000001 ; smallest positive value | ||
| r | ||
| ) ;if |
Contributor
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
摘要
实现 SRFI-27 随机数生成器及相关功能:
(srfi srfi-27)模块:提供符合 SRFI-27 规范的随机数生成接口(liii random)模块:提供更便捷的随机数 API新增文件
goldfish/liii/random.scm- liii random 模块实现goldfish/srfi/srfi-27.scm- SRFI-27 标准实现devel/215_18.md- 开发文档tests/liii/random-test.scm- 基础测试tests/liii/random/*.scm- 各功能详细测试测试计划
🤖 Generated with Claude Code