Answers for "random 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
2

java random

import java.util.Random;
//If there is a need to reproduce the same sequence of random numbers, 
//we have to specify a seed to the constructor, like 100000
Random random = new Random(100000);    //using a seed
System.out.println(random.nextInt(5)); // print one random 0, 1, 2, 3, 4
System.out.println(random.nextInt(5));
Posted by: Guest on October-17-2021
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

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language