如何解决random: expects (or/c (integer-in 1 4294967087) pseudo-random-generator?) or a pseudo-random-generator, given 0
错误?
(require 2htdp/universe)
(require 2htdp/image)
(define-struct sample(x))
(define BACKGROUND (empty-scene 100 100))
(define CIRCLE (ellipse 10 10 "solid" "red"))
(define (rand d)
(random d))
(define (randoll d)
(cond [(even? (rand d))(rand d)]
[(< (rand d) 6)7]
[(= (rand d) 0)7]
[else 7]))
(define (main ws)
(big-bang ws
(on-tick randoll)
(to-draw render)))
(define (render d)
(place-image CIRCLE (randoll d) 6 BACKGROUND))
 Â(主è¦100)   random:期望(或/ c(整数为1 4294967087的整数)伪éšæœºå‘生器?)或伪éšæœºå‘生器,给定0
   Â期望它以å¶æ•°è¿žç»å˜åŒ–çš„ä½ç½®ï¼Œä½†æ˜¯è¿™ç¡®å®žå‘生了
ç”案 0 :(得分:1)
如何将1
æ·»åŠ åˆ°ä¼ é€’ç»™random
çš„å‚æ•°ä¸ï¼Ÿåªè¦è¾“入为>= 0
å³å¯æ£å¸¸å·¥ä½œ-您收到的错误åªæ˜¯è¯´æ˜Žæ‚¨æ— 法将0
作为å‚æ•°ä¼ é€’ç»™random
:
(define (rand d)
(random (+ d 1)))
顺便说一下,randoll
看起æ¥ä¸æ£ç¡®ã€‚为什么è¦æµ‹è¯•éšæœºå€¼æ˜¯å¦ä¸ºå¶æ•°ï¼Œåªæ˜¯ä¸ºäº†è¿”回å¦ä¸€ä¸ªæˆ‘们ä¸çŸ¥é“它是å¦ä¸ºå¶æ•°çš„éšæœºå€¼ï¼Ÿçœ‹æ¥ï¼Œå¦‚æžœéšæœºå€¼ä¸ºé›¶ï¼Œæˆ–者在所有其他情况下,您åªæƒ³è¿”回7
。如果符åˆæ‚¨çš„æ„图,请å°è¯•ä»¥ä¸‹æ–¹æ³•ï¼š
(define (randoll d)
(let ([r (rand d)]) ; call `rand` exactly once
(cond [(zero? r) 7] ; 0 is even, so check this first
[(even? r) r] ; return `r` only if it's even
[else 7]))) ; default case
我ä¸ç¡®å®šrandoll
的期望值范围是多少,但是在我看æ¥ï¼Œå¤§å¤šæ•°æ—¶å€™å®ƒåªä¼šè¿”回7
ã€‚è¿™å°±æ˜¯ä½ æƒ³è¦çš„å—?