-4

I'm trying to create a Java application that receives a string and uses that string to determine how many integers should be randomly selected from a range from 1 - x where x is an integer value. I understand that "True" random generation is difficult/impossible using modern computing technology, but a github repository gained my interest that uses jpeg images as a source of random number generation (https://github.com/prgpascal/bluerand).

The generator seems to create a random amount of bytes, however, and so I was wondering if any of you may have any indication of how one might use this to somehow generate integers within a range.

Of course, if any of you know any other generators without a quota limit, be it from a website with an api or a library that works on a local computer that can do this task and I've missed it, I would be happy to learn of it and move my attention to it!

Thank you for your time.

  • 1
    Using the same JPEG would generate the same seed, true random is really impossible as for now until we have a dice inside our processors. – Marcos Vasconcelos Jul 28 '18 at 02:26
  • You are asking off-site resource. https://stackoverflow.com/help/on-topic – Jswq Jul 28 '18 at 02:27
  • 5
    Why do you think you need a "true" random number generator? How do the `java.util.Random` and `java.security.SecureRandom` classes fail to meet your needs? – Solomon Slow Jul 28 '18 at 02:28
  • SecureRandom can use a source of entropy in your computer to be even more random. – Peter Lawrey Jul 28 '18 at 06:45
  • SecureRandom uses an entropy source so is as close to random as you will get https://stackoverflow.com/questions/137212/how-to-solve-slow-java-securerandom – Peter Lawrey Jul 28 '18 at 06:52

1 Answers1

1

Here is one way to turn an array of "random" bytes into a random number in a range 0 ... N - 1:

  1. Take the first 8 bytes, and convert them into a long; for example

      l = (b[0] & 0xffL) | (b[1] & 0xffL << 8 ) | 
          (b[2] & 0xffL << 16 ) | ... (b[7] & 0xffL << 56 );
    
  2. Calculate the number as

      n = Math.floorMod(l, N);
    

If any of you know any other generators without a quota limit, be it from a website ...

  1. Asking about external resources is off-topic.

  2. It is a bad idea to make an application dependent on a website that might go down, might go away, might cost someone network changes, etc.

  3. It is a bad idea to get random numbers from an untrusted source:

    • they may be recording them
    • the numbers may actually not be really random (despite what they say)
    • the numbers may be biased (accidentally, deliberately).

Also, note that the "truly" random numbers you get from images via the BlueRand library are not random at all .... unless you have a good source of random images; e.g. a live feed if pictures of something with motion.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216