Answers for "how to use random function in java"

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
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
13

random java

import java.util.Random();
Random <name> = new Random();
<variable> = <name>.nextInt(<excuslive top limit>);
Posted by: Guest on December-28-2019
0

funzione random in java

int n = (int) (Math.random() * 51);
Posted by: Guest on April-17-2020
1

Random number generator in java

// example on Math.random() method in java
public class RandomMethodExample
{
   public static void main(String[] args)
   {
      // here we are generating random doubles 
      System.out.println(Math.random()); 
      System.out.println(Math.random());
   }
}
Posted by: Guest on December-11-2020

Code answers related to "how to use random function in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language