Eric Clack
Eric Clack

Reputation: 1926

How to convert real to exact integer in Racket?

How do I convert a value of the form 20.2 into something that (random ...) accepts?

I've tried these:

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

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

But both return:

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

Upvotes: 13

Views: 11406

Answers (2)

Óscar López
Óscar López

Reputation: 236140

These also work, and according to the documentation they're just shorthands for your approach:

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

Upvotes: 17

Eric Clack
Eric Clack

Reputation: 1926

This article seems to answer the question for Scheme: http://computer-programming-forum.com/40-scheme/674db7a1706960d5.htm

So with code like this I get the result I want:

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

Is this the simplest way?

Upvotes: 5

Related Questions