-6
package com.nusecond.Code;
import java.util.*;

public class Code {

    public static void main(String args[]){

        Random random=new Random();
        for(int j=10;j<=99;j++){        
           int num=random.nextInt(100) ;
           //ssLong i=System.currentTimeMillis();
           //String result = String.format("%04d", i % 10000);
           System.out.println("Code generated:"  +num);
        }
    }
}

I want to print two digit random numbers between 10 and 99.

Note : I want only one number every time I run the program.

akash
  • 22,664
  • 11
  • 59
  • 87
rakeshchowdary
  • 19
  • 1
  • 2
  • 2

4 Answers4

4

With random numbers, if you need to start at a certain number, just add the random number to that lower limit. The upper limit passed to the random number will be the upper bounds minus the lower limit.

int num = random.nextInt(90) + 10;
James Cootware
  • 381
  • 2
  • 9
1

You can use this method:

private static int showRandomInteger(int aStart, int aEnd, Random aRandom){
    if (aStart > aEnd) {
      throw new IllegalArgumentException("Start cannot exceed End.");
    }
    //get the range, casting to long to avoid overflow problems
    long range = (long)aEnd - (long)aStart + 1;
    // compute a fraction of the range, 0 <= frac < range
    long fraction = (long)(range * aRandom.nextDouble());
    int randomNumber =  (int)(fraction + aStart);    
    return randomNumber;
}

The parameters you have to pass to this method are:

  • aStart : your starting value(i.e. 10 in you case)
  • aEnd : your ending value(i.e 99 in your case)
  • aRandom : this would be an object of the Random class.(Random random=new Random();)
akash
  • 22,664
  • 11
  • 59
  • 87
Adarsh Bhat
  • 159
  • 1
  • 12
0

Random random = new Random();

// generate a random integer from 10 to 90 i.e 2 digit random no.

int abc =  10+random.nextInt(90);
Colthurling
  • 75
  • 1
  • 1
  • 7
0

java code to generate random two digit number.

    import java.util.*;

public class RandomNumbers{
        public static void main(String[] args){
                Random rand = new Random();
                int random=rand.nextInt(90)+10; //generates random no. between 10 and 100
                System.out.println(random);
        }
}
dust
  • 171
  • 1
  • 5