Reputation: 19
I'm trying to generate 1000 random numbers between 13 and 100. So far it's only generating 75% of what I want repeatedly a thousand times. Here's what I have so far:
Random rand = new Random();
for (int j = 0; j < 1000; j++)
{
int pick = rand.nextInt((87) + 13);
pick++;
}
Why isn't it working?
Upvotes: 1
Views: 3491
Reputation: 1316
Pay attention to nextInt()
covering the 0 inclusively and the specified value exclusively! So it has to be rand.nextInt(88)
to make the highest int
generated be 87. Here is what you want:
Random rand = new Random();
for (int j = 0; j<1000; j++)
{
int pick = rand.nextInt(88)+13;
}
Upvotes: 4
Reputation: 4000
I use this in my codes:
public static int randomInteger(int min, int max)
{
java.security.SecureRandom rand = new java.security.SecureRandom();
//get bounded [0, max) from nextInt()
int randomNum = rand.nextInt(max) + min;
return randomNum;
}
value = randomInteger(13,100); //13..99
value = randomInteger(13,101); //13..100
Upvotes: 0
Reputation: 1511
rand.nextInt(88) + 13;
should give you numbers between 13 and 100, and you just put it in your loop.
Upvotes: 1
Reputation: 12484
The line :
So far its only generating 75% of what i want repeatedly a thousand times
Really doesn't add up to me. It might be a seeding issue you're having though. Make sure to always re-seed the random number, using time.
But I agree with Abdul , you need to take the +13
out of that parenthesis:
rand.nextInt(87) + 13;
Because rand.nextInt((87) + 13)
is the same as rand.nextInt((67) + 23)
as rand.nextInt((1) + 99)
But if you want more "true" randomness, look into something called buzzhash (though that is for hashing ; yet may be modded for number)
Upvotes: 0