mercredi 28 novembre 2018

Creating a large random number in Racket

I'm trying to generate a very large random number in Racket, something between 0 and 1e20.

(random) has the limitation set in the range of 1 and 4294967087.

I've created a hack-y function that tries to generate a random number, but only does so based on order-of-magnitude, not the actual number. Here's that function:

define (l-random [min 0] [max 10])
  (define length (random (number-length min) (number-length max)))
  (define string "")
  (for ([i length])
    (set! string (format "~a~a" string (random 0 10))))
  (string->number string))

And here's how I calculate order of magnitude:

(define (number-length number)
  (cond [(= 0 number) 1]
        [else (+ 1 (exact-floor (log (abs number) 10)))]))

Do you have any suggestions or solutions? Thanks!




Aucun commentaire:

Enregistrer un commentaire