BigInt icon indicating copy to clipboard operation
BigInt copied to clipboard

Add functions that generate a random BigInt

Open faheel opened this issue 8 years ago • 10 comments

The following functions that generate a random BigInt need to be implemented under include/functions/random.hpp:

  • [x] Having specific number of digits:

    friend BigInt big_random(size_t num_digits);
    

    Returns a random BigInt having the specified number of digits. If the number of digits are not specified, use a random value for it.

  • [ ] Within a certain range:

    friend BigInt big_random(const T& low, const T& high);
    

    Returns a random BigInt such that low <= BigInt <= high.

Note: type T can be a BigInt, std::string or long long.

faheel avatar Jan 18 '18 18:01 faheel

on which file I have to work to solve this issue?

Give me the file name.

asas2016SEC avatar Jan 18 '18 18:01 asas2016SEC

The file include/functions/random.hpp doesn't exist yet, you'll have to create it. The "template" would be similar to that of math.hpp (in include/functions/), so you can duplicate and modify math.hpp.

faheel avatar Jan 18 '18 18:01 faheel

For reference, the following is the most appropriate way to generate true random numbers C++:

std::random_device rand_generator;
size_t rand_int = rand_generator();   // get a random number between 0 and UINT_MAX

Using this, one way to generate a random BigInt would be to keep appending randomly generated integers to BigInt.value:

rand_big.value += std::to_string(rand_int);

faheel avatar Jan 18 '18 19:01 faheel

Hi, is this issue resolved yet? As I wish to start contributing to open source projects (this is my first time doing opensource, so I may ask questions alot), and this seems a good point to start.
I will be using an editor in Windows 10 x64 to write code for this, is this okay?

IbrarYunus avatar Feb 01 '18 19:02 IbrarYunus

@IbrarYunus I've been working on the second function and it's almost done.

You can take up #18 if you're interested in contributing.

faheel avatar Feb 02 '18 03:02 faheel

Can I do the function to generate a random number within some range? Have never contributed to any project so i thought some simple function would be good start.

FR4NKESTI3N avatar Jun 09 '18 17:06 FR4NKESTI3N

@FR4NKESTI3N Sure, you can give it a try.

faheel avatar Jun 10 '18 14:06 faheel

Sorry, I was unable to work past 2 weeks. I have studied the results of existing big_random() on octave and they appear to be uniformly distributed. Wouldn't it be better to use this existing function with modulo? Something like:

result = low + big_random(x)%(high - low);

where x can be adjuted to reduce calculations.

There dosen't seem to be a need to write an entirely new function unless normal distribution is required.

Some nice graphics I got in octave

FR4NKESTI3N avatar Jun 26 '18 12:06 FR4NKESTI3N

@faheel Check out branch GH-19-Random. Let me know if this meets the requirements for this issue and I'll create the Pull Request.

stkaufer avatar Oct 05 '18 03:10 stkaufer

Is this task still an open issue? If so, I would like to contribute.

alihaider1264 avatar Oct 21 '21 19:10 alihaider1264