Scheme: creating a random range [closed]

谁说胖子不能爱 提交于 2019-12-12 05:08:11

问题


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.



来源:https://stackoverflow.com/questions/12334891/scheme-creating-a-random-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!