Scheme: creating a random range [closed]

2019-08-16 00:34发布

问题:

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:

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.