Reputation: 479
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!
Upvotes: 0
Views: 421
Reputation: 3654
The Random Number Generation module from the Science Collection has exactly what you are looking for. Here is an example on the DrRacket repl (up to 1e20 per your specification):
Welcome to DrRacket, version 6.3 [3m].
Language: racket; memory limit: 256 MB.
> (require (planet williams/science/random-source))
> (random-integer (expt 10 20))
79219429305569404064
Which runs in under one second!
Upvotes: 1