generate random number java
// JAVA - WITH A RANGE
(Math.random() * ((max - min) + 1)) + minimport 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;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);
}
}public class GuessTheNumber {
public static void main(String[] args) {
int randomInt = (int)(20.0 * Math.random());
int num1 = randomInt;
int num2 = 10;
if(num1 == num2){
System.out.println("Correct!");
} else if(num1 > num2){
System.out.println("Too low!");
} else{
System.out.println("Too high!");
}
}
}int rand = ThreadLocalRandom.current().nextInt(x,y);package com.example.guessthenumber;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
int randomNumber;
public void generateRandomNumber(){
Random rand = new Random();
randomNumber = rand.nextInt(20) + 1;
}
public void GuessTheNumber(View view){
EditText numberET = (EditText) findViewById(R.id.numerEditText);
int guessValue = Integer.parseInt(numberET.getText().toString());
String message;
if(guessValue > randomNumber)
{
message = "Lower!";
}
else if (guessValue < randomNumber)
{
message = "Higher!";
}
else
{
message = "Good Guess!!! Try Again";
generateRandomNumber();
}
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
Log.i("Entered Text", numberET.getText().toString());
Log.i("Random Number", Integer.toString(randomNumber));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
generateRandomNumber();
}
}