如何过滤éšæœºæ•°ç”Ÿæˆå™¨ä¸”ä¸åŒ…å«0

时间:2019-02-08 03:19:00

标签: racket

如何解决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

     

期望它以å¶æ•°è¿žç»­å˜åŒ–çš„ä½ç½®ï¼Œä½†æ˜¯è¿™ç¡®å®žå‘生了

1 个答案:

答案 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。这就是你想è¦çš„å—?