Scheme: creating a random range [closed]

2019-08-16 00:22发布

In scheme I have to use random to define a procedure that accepts no arguments and returns an integer in the range 1 to 10, inclusive and i cant use if. im lost =(

1条回答
Explosion°爆炸
2楼-- · 2019-08-16 01:02

If your Scheme provides a random function, you want either

(define (1-10-rand)
    (+ 1 (random 10)))

or

(define (1-10-rand)
    (floor (* 10 (random))))

depending on whether you have (random n) --> integer in [0, n-1]) or (random) -> float in [0,1]

Be advised that this isn't standards-compliant. For absolute portability, write your own RNG.

查看更多
登录 后发表回答