Java Random number but not zero
Solution 1
The problem with that code is that 1 is twice as likely as other numbers (as your effective result is 1 when nextInt()
returns 0 or 1).
The best solution is to just always add 1 and request random numbers from a smaller range:
int rnd = rand.nextInt(num - 1) + 1;
Solution 2
I guess you are trying to get a random number between 1 and 'num'.
a more generic way can be :
int Low = 1;
int High = 10;
int R = r.nextInt(High-Low) + Low;
This gives you a random number in between 1 (inclusive) and 10 (exclusive). ( or use High=11 for 10 inclusive)
Solution 3
Random random = new Random();
int ran = random.nextInt(9) + 1; //10 is maxRandom value for this code. 1-10
Solution 4
Try this:
int num,max=10,min=1;
Random r=new Random();
num=r.nextInt(max-min)+1;
You'll need this import at the beginning of your file:
import java.util.Random;
Solution 5
Just lower the bound (num variable) by 1 and add 1 to the ran variable
int num = 10;
Random rand = new Random();
int ran = rand.nextInt(num - 1) + 1;
// or decrease num first n-- and then int ran = rand.nextInt(num) + 1
now the bound(limit) is 8 (inclusive for example, the final number is always exclusive) and if it comes 0, it will increase to 1 and if it comes 8, it will increase to 9, which was originally supposed to be the bound.
Comments
-
newbieprogrammer almost 2 years
int num = 10; Random rand = new Random(); int ran = rand.nextInt(num); if (ran==0){ ran= ran+1; } System.out.println("random : "+ran);
This is what i have coded so far, is there a better way to do this? I feel that this is hard coding when random is 0, I added 1.
-
newbieprogrammer about 11 yearswhat does maxValue-1 do ? is that the num i specify here ?
-
newbieprogrammer about 11 yearsso meaning , random.nextInt(num-1) + 1?
-
newbieprogrammer about 11 yearsthis is getting alittle confusing. I just want random number in 10 but not 0 .
-
vszurma about 11 yearsmaxValue is the largest random number you will get. because you add one later you have to request maxValue-1
-
Petr Shypila about 11 years@newbieprogrammer
random.nextInt(maxValue)+1
maxValue
set a range from 0 tomaxValue
. So ifmaxValue
equal 9, your maximum integer will be 10. -
Lav about 11 yearsLet me know what part is confusing you , will try to explain better
-
newbieprogrammer about 11 yearslet say I want random number from 1 to less than 10. which number should I specify?
-
Lav about 11 yearsuse Low=1 and High=10 in above code you will have what you desire
-
newbieprogrammer about 11 yearsso this will get integer between 1-9 ?
-
pjs about 11 years@newbieprogrammer Yes, this will generate numbers between 1 and 9 including both endpoints. If you want to generate values of 10, you need to be like Spinal Tap and bump
HIGH
up by one to 11 because Java's generator doesn't include the actual upper limit. -
CptSupermrkt over 10 yearsrandom.nextInt(9) + 1 will give you 1-9.
-
Vlasec over 9 yearsIf
maxValue
is really the highest number you will get, then the- 1
is wrong, asnextInt(10)
returns numbers in range of0..9
. Thus I edit the answer. -
Joachim Sauer over 9 years@Vlasec: you are correct. The parameter to
nextInt
is an exclusive bound, so if you want numbers from 1 to 10 thennextInt(10) + 1
is what you need to use. -
Vlasec over 9 yearsInclusive, exclusive ... that could be useful in floating points, but here, one can explain it with more clarity. I did.
-
dieresys about 8 yearsCould we edit the answer to denote that nextInt is exclusive and maxValue will never get returned, therefore the "- 1" might not be necessary?
-
poshak almost 5 years@dieresys is correct . -1 is not needed. Eg. to get random nos. from all positive integers(0 excluded), the formula will be : int rnd = rand.nextInt(Integer.MAX_VALUE) + 1