fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. private static Random rnd=new Random();
  11. public static int getRandom(){
  12. return rnd.nextInt(10000);
  13. }
  14. public static void main(String args[]){
  15. int r=getRandom();
  16. System.out.println("As number: "+r);
  17. System.out.printf("As 4-digit code, just printing: %04d\n",r);
  18. String s=String.format("%04d",r);
  19. System.out.println("As 4-digit code, in separate string: "+s);
  20. }
  21. }
Success #stdin #stdout 0.11s 36516KB
stdin
Standard input is empty
stdout
As number: 6186
As 4-digit code, just printing: 6186
As 4-digit code, in separate string: 6186