chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-users] u8vector to numbers bignum


From: chi
Subject: [Chicken-users] u8vector to numbers bignum
Date: Wed, 27 May 2015 15:53:39 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0

How would I convert a u8vector to a bignum?

I'd like a good large random number, and there's srfi 27 for decent random
sources, and there's 'numbers' for bignum support, but srfi-27 only produces
fixnums or u8vectors. Logically I can't imagine a bignum isn't represented under
the hood by a block of bytes somehow, so it should be easy to turn a random byte
vector into a random bignum?

numbers itself only provides (random ...) which uses rand(3), and that's not
what I would call a good random number. Random sources don't really seem like
something a bignum library should worry about, anyway.

I could always do (number->string (u8vec_to_hex (random-u8vector #x20)) #x10)
but having a double sized hexadecimal intermediate isn't terribly appealing,
especially for a random number I might have to generate for hundreds of peers a
second in a high volume scenario. (Okay now I really /am/ being optimistic.)

Looking at the numbers source, it seems to be analagous to a vector whose first
element is a magic number indicating it's a bignum, and the rest of the vector
is the digits. I'm not qualified to say how hard that would be to turn a vector
into that, but it really doesn't seem like it would be too much trouble for
someone who was. Of course it would be seen as native endian, but since it's a
random number it doesn't matter too much.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]