Answers for "math.random in java"

8

java math.random

int rand = (int)(Math.random() * range) + min;
Posted by: Guest on February-04-2021
2

math.random java

// int rand = (int)(Math.random() * range) + min; 
// Java program to demonstrate Math.random() working 
// of java.lang.Math.random() method 
import java.lang.Math; 
  
class Gfg2 { 
  
    // driver code 
    public static void main(String args[]) 
    { 
        // define the range 
        int max = 10; 
        int min = 1; 
        int range = max - min + 1; 
  
        // generate random numbers within 1 to 10 
        for (int i = 0; i < 10; i++) { 
            int rand = (int)(Math.random() * range) + min; 
  
            // Output is different everytime this code is executed 
            System.out.println(rand); 
        } 
    } 
}
Posted by: Guest on March-02-2021
0

java math random

// Math.random() returns a random number between 0.0-0.99.
double rnd = Math.random();

// rnd1 is an integer in the range 0-9 (including 9).
int rnd1 = (int)(Math.random()*10);

// rnd2 is in the range 1-10 (including 10). The parentheses are necessary!
int rnd2 = (int)(Math.random()*10) + 1;

// rnd3 is in the range 5-10 (including 10). The range is 10-5+1 = 6.
int rnd3 = (int)(Math.random()*6) + 5;

// rnd4 is in the range -10 up to 9 (including 9). The range is doubled (9 - -10 + 1 = 20) and the minimum is -10.
int rnd4 = (int)(Math.random()*20) - 10;
Posted by: Guest on July-09-2021
0

Generate Random number using Math.random in Java

class GenerateRandom {
    public static void main( String args[] ) {
      int min = 50;
      int max = 100;
        
      //Generate random int value from 50 to 100 
      System.out.println("Random value in int from "+min+" to "+max+ ":");
      int random_int = (int)Math.floor(Math.random()*(max-min+1)+min);
      System.out.println(random_int);
    }
}
Posted by: Guest on August-25-2021
0

java math random

(int)(Math.random() * max) + min; inclusive
Posted by: Guest on July-15-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language