-3

I'm working with an algorithm and would like to do some statistics on it with some very large random numbers. I would like the numbers generated to come from the interval [2^255, 2^256], but it would be equivalent to generate a number in [0,2^255].

I'm working in C++ but I'm very new to programming and only know the basics. Is this even a plausible thing to do (in terms of memory and computing power)? I've seen other posts dealing with 2^32 and 2^64, but haven't found anything quite this big. The only computations I'll be doing with these numbers are comparison, division, and subtraction.

If so, how would I write the code to accomplish such a task?

kless135
  • 43
  • 2
  • 2
    "such a task" - which part of the task? currently it is a very broad question... – Karoly Horvath May 18 '16 at 16:07
  • 1
    Do you need actual integer values, or would approximate values be okay? If approximate values are usable, then doubles should handle it, otherwise you need an arbitrary precision number library. http://stackoverflow.com/questions/310276/how-to-handle-arbitrarily-large-integers – user1937198 May 18 '16 at 16:07
  • What sort of datatype will you use manipulating such a large number? – Sean May 18 '16 at 16:07
  • 3
    There's a variety of [random number functions in the Standard Library](http://en.cppreference.com/w/cpp/numeric/random). Have you evaluated any of these? If you use the right one for your problem, like one that generates 64 bits worth of pure random data with an even distribution, you could concatenate multiple smaller random numbers together to build a much larger one. – tadman May 18 '16 at 16:08
  • 1
    “Is it plausible” — yes, cryptography libraries need such large, random numbers as par for the course. However, they use special maths libraries to deal with this range. – Konrad Rudolph May 18 '16 at 16:11
  • _"If so, how would I write the code to accomplish such a task?"_ That's definitely _too broad_. Stack Overflow isn't a code writing service! Period. I don't get which moron upvoted that question. – πάντα ῥεῖ May 18 '16 at 16:17
  • You could just use the random number functions of the GMP library : https://gmplib.org/manual/Integer-Random-Numbers.html – Jesper Juhl May 18 '16 at 16:53

1 Answers1

1

This task, in itself, is pretty trivial. The hardest part is probably just choosing how to represent your large number. For the moment, let's assume an array of 4 unsigned long long (each of which is guaranteed to hold at least 64 bits, so 4 must be at least 256 bits).

struct big_num { 
    unsigned long long data[4];
};

big_num gen_random() { 
    big_num ret;
    std::mt19937_64 gen;

    for (int i=0; i<4; i++)
        ret[i] = gen();
    return ret;
}

As is probably obvious from this, the size is pretty much irrelevant. We're basically just producing some number of random bits (64 of them at a time) and pushing them into some sort of container, then returning the container.

The only trick to using this basic idea with a different "container" of your choice is getting to the container's representation of the bits, so you can manipulate the individual long, long long (or whatever) components used to store the value. Since the largest type (guaranteed to be) built into C or C++ is 64 bits, it's pretty much guaranteed that any way of storing a number larger than that is going to do it by storing some number of smaller objects that can be represented with a native type.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111