如何在 Racket 中将实数转换为精确整数?

How to convert real to exact integer in Racket?

如何将 20.2 形式的值转换为 (random ...) 接受的值?

我试过这些:

;; x defined by some earlier maths and of form 20.2

(random (round x))
(random (floor x))

但两者都 return:

random: contract violation
expected: (or/c (integer-in 1 4294967087) pseudo-random-generator?)
given: 20.0

这篇文章似乎回答了 Scheme 的问题: http://computer-programming-forum.com/40-scheme/674db7a1706960d5.htm

所以使用这样的代码我得到了我想要的结果:

(random (inexact->exact (round x)))
(random (inexact->exact (floor x)))

这是最简单的方法吗?

这些也有效,根据 documentation 它们只是您方法的缩写:

(random (exact-round x))
(random (exact-floor x))