Java Random number but not zero

43,886

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.

Share:
43,886
newbieprogrammer
Author by

newbieprogrammer

Java programmer,

Updated on July 30, 2022

Comments

  • newbieprogrammer
    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
    newbieprogrammer about 11 years
    what does maxValue-1 do ? is that the num i specify here ?
  • newbieprogrammer
    newbieprogrammer about 11 years
    so meaning , random.nextInt(num-1) + 1?
  • newbieprogrammer
    newbieprogrammer about 11 years
    this is getting alittle confusing. I just want random number in 10 but not 0 .
  • vszurma
    vszurma about 11 years
    maxValue is the largest random number you will get. because you add one later you have to request maxValue-1
  • Petr Shypila
    Petr Shypila about 11 years
    @newbieprogrammer random.nextInt(maxValue)+1 maxValue set a range from 0 to maxValue. So if maxValue equal 9, your maximum integer will be 10.
  • Lav
    Lav about 11 years
    Let me know what part is confusing you , will try to explain better
  • newbieprogrammer
    newbieprogrammer about 11 years
    let say I want random number from 1 to less than 10. which number should I specify?
  • Lav
    Lav about 11 years
    use Low=1 and High=10 in above code you will have what you desire
  • newbieprogrammer
    newbieprogrammer about 11 years
    so this will get integer between 1-9 ?
  • pjs
    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
    CptSupermrkt over 10 years
    random.nextInt(9) + 1 will give you 1-9.
  • Vlasec
    Vlasec over 9 years
    If maxValue is really the highest number you will get, then the - 1 is wrong, as nextInt(10) returns numbers in range of 0..9. Thus I edit the answer.
  • Joachim Sauer
    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 then nextInt(10) + 1 is what you need to use.
  • Vlasec
    Vlasec over 9 years
    Inclusive, exclusive ... that could be useful in floating points, but here, one can explain it with more clarity. I did.
  • dieresys
    dieresys about 8 years
    Could we edit the answer to denote that nextInt is exclusive and maxValue will never get returned, therefore the "- 1" might not be necessary?
  • poshak
    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