4

I'm new to using BigInteger, so I'm trying my best reading through the documentation for it. I'm still confused however. I need to generate 500 random integers between the range of 2^70 and 2^80 and I don't know how to set a range for BigIntegers.

I'm getting the possible duplicate messages, so I guess I should add that I've already looked at the solutions in this one and still don't understand how to solve my issue: How to generate a random BigInteger value in Java?

Community
  • 1
  • 1
Jasmine
  • 313
  • 2
  • 23
  • Possible duplicate of [How to generate a random BigInteger value in Java?](http://stackoverflow.com/questions/2290057/how-to-generate-a-random-biginteger-value-in-java) I think that question should answer yours. – DonyorM Oct 29 '16 at 03:09
  • possible duplicate : http://stackoverflow.com/questions/2290057/how-to-generate-a-random-biginteger-value-in-java – thetraveller Oct 29 '16 at 03:09

1 Answers1

1

There is a built-in method to generate a random BigInteger between 0 and 2^n - 1.

You can use that in a loop to generate numbers up to 2^80.

The chance that a number will fall below 2^70 is very small ( ~ 0.1%). If that happens (and with 500 iterations it might very well), just draw another one.

This will give you a uniform distribution of random numbers between 2^70 and 2^80.

The chance that a number will repeat is almost nonexistent. If you feel that it cannot be ignored, check your previous numbers for duplicates, and draw again.

Thilo
  • 257,207
  • 101
  • 511
  • 656
  • Is it possible to set the minimum range value though? For the case of my assignment, it needs to be set within that specific range. – Jasmine Oct 29 '16 at 04:18
  • 1
    What is the minimum range? If it is 2^70, the above will work very well. If it is 2^78, better use a different method, such as http://stackoverflow.com/a/23054579/14955. – Thilo Oct 29 '16 at 04:41