Answers for "random numbers in 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
9

how to generate a random number in java

import java.util.Random;

Random rand = new Random();
int maxNumber = 10;

int randomNumber = rand.nextInt(maxNumber) + 1;

System.out.println(randomNumber);
Posted by: Guest on August-18-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
4

generate random number in java

import java.lang.Math;

int randomNumber;

int minimum = 1;
int maximum = 10 - minimum;

randomNumber = (int)((Math.random()*maximum) + minimum);

System.out.println("Random Number (Between 1-10): " + randomNumber);
Posted by: Guest on December-04-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

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language