0

I need to randomly pick 10 numbers from 1 to 2000. It is very important that this rng cannot be predicted/hacked in any way. Would this be a safe/proper way to do it:

int randomInt;
SecureRandom random = new SecureRandom();
for (int i = 1; i <= 10; i++)
{
    randomInt = random.nextInt(2000) + 1;
    String generated = generated+String.valueOf(randomInt)+",";
}

I need it to securely generate those random numbers both on windows and android devices. The devices can be offline too if it helps. So would this be safe?

EDIT: I'm especially worried about android version, because I've heard bad stories in the past where people got hacked due to a bug with SecureRandom on android.

2
  • This will now generate numbers in the range 1...2001
    – Ja1024
    Jun 4 at 2:02
  • Fixed it. I was initially doing it with int[] array but then switched to this and messed up the numbers
    – Cannon 190
    Jun 4 at 2:19

1 Answer 1

0

This will generate numbers within 0..2000, not 1...2000, but, yes, SecureRandom is the proper class here.

Use random.nextInt(2000) + 1 for the correct range.

2
  • Oh you're right, my bad
    – Cannon 190
    Jun 4 at 1:51
  • Note the upper bound when you add 1: It should be 2000, not 2001.
    – Ja1024
    Jun 4 at 2:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .