Answers for "create random number java"

90

java random number

import java.util.Random;

Random rand = new Random();

// Obtain a number between [0 - 49].
int n = rand.nextInt(50);

// Add 1 to the result to get a number from the required range
// (i.e., [1 - 50]).
n += 1;
Posted by: Guest on March-09-2020
10

how to create a random number in java

import java.util.Random;

class scratch{
    public static void main(String[] args) {
        Random rand = new Random();
        System.out.println( rand.nextInt(5) );
        //prints a random Int between 0 - 4 (including 0 & 4);
    }
}
Posted by: Guest on January-08-2020
5

Random number generator in java

// java.util.random example
import java.util.Random;
public class JavaRandomClass
{
   public static void main(String[] args)
   {
      Random random = new Random();
      // random integers in range 0 to 999 
      int randInt1 = random.nextInt(1000); 
      int randInt2 = random.nextInt(1000);
      // printing random integers
      System.out.println("Random Integers: " + randInt1); 
      System.out.println("Random Integers: " + randInt2);
      // here we are generating Random doubles
      double randDou1 = random.nextDouble(); 
      double randDou2 = random.nextDouble();
      // printing random doubles
      System.out.println("Random Doubles: " + randDou1); 
      System.out.println("Random Doubles: " + randDou2);
   }
}
Posted by: Guest on December-11-2020
3

random number generator java

import java.util.Random;
class GenerateRandom {
    public static void main( String args[] ) {
      Random rand = new Random(); //instance of random class
      int upperbound = 25;
        //generate random values from 0-24
      int int_random = rand.nextInt(upperbound); 
      double double_random=rand.nextDouble();
      float float_random=rand.nextFloat();
      
      System.out.println("Random integer value from 0 to" + (upperbound-1) + " : "+ int_random);
      System.out.println("Random float value between 0.0 and 1.0 : "+float_random);
      System.out.println("Random double value between 0.0 and 1.0 : "+double_random);
    }
}
Posted by: Guest on December-09-2020
2

java random number generator in range

int rand = ThreadLocalRandom.current().nextInt(x,y);
Posted by: Guest on May-12-2020

Code answers related to "create random number java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language