The first solution is to use the java.util.Random
class:
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;
Another solution is using Math.random()
:
double random = Math.random() * 49 + 1;
or
int random = (int)(Math.random() * 50 + 1);
Related Posts:
- Generating a Random Number between 1 and 10 Java
- How do I generate random integers within a specific range in Java?
- Generating a Random Number between 1 and 10 Java [duplicate]
- How do I generate random integers within a specific range in Java?
- Generate a random double in a range
- possible lossy conversion from long to int?
- Java Generate Random Number Between Two Given Values
- Random shuffling of an array
- Creating a random string with A-Z and 0-9 in Java
- Creating random numbers with no duplicates
- Generate a random double in a range
- Problems Generating A Math.random Number, Either 0 or 1
- Java generating non-repeating random numbers
- Simple Coin Toss using random class in Java. The do while loop doesn’t seem to generate random results
- IllegalArgumentException: Bound must be positive
- What is the proper way to handle a NumberFormatException when it is expected?
- What causes “Unable to access jarfile” error?
- Where is JRE 11? [duplicate]
- How to convert/parse from String to char in java?
- How to set a Timer in Java?
- How to parse JSON in Java
- Can’t execute jar- file: “no main manifest attribute”
- What’s the simplest way to print a Java array?
- How to avoid java.util.ConcurrentModificationException when iterating through and removing elements from an ArrayList
expected Error in java Compilation [duplicate] - “Javac” doesn’t work correctly on Windows 10
- Best way to convert an ArrayList to a string
- Does a Java to C++ converter/tool exist?
- How change the color arrowIcon to JMenu when it is selected
- String interpolation in Java 14 or 15
- Round a double to 2 decimal places [duplicate]
- How to make a deep copy of Java ArrayList
- What does request.getParameter return?
- DB2 jdbc connection error Connection refused. ERRORCODE=-4499, SQLSTATE=08001
- element not interactable exception in selenium web automation
- How do I save a String to a text file using Java?
- method in class cannot be applied to given types
- HTTP Status 405 – Request method ‘POST’ not supported (Spring MVC)
- “NoClassDefFoundError: Could not initialize class” error
- Implements vs extends: When to use? What’s the difference?
- Ubuntu: OpenJDK 8 – Unable to locate package
- The @ symbol in java
- Getting Keyboard Input
- LocalDate to java.util.Date and vice versa simplest conversion?
- What is a Question Mark “?” and Colon “:” Operator Used for?
- How to remove single character from a String
- org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when ‘hibernate.dialect’ not set
- Is there a concurrent List in Java’s JDK?
- org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when ‘hibernate.dialect’ not set
- Why am I still getting a cannot find Java SE Runtime Environment?
- error: unreported exception FileNotFoundException; must be caught or declared to be thrown
- implicit super constructor Person() is undefined. Must explicitly invoke another constructor?
- java.net.SocketTimeoutException: Read timed out under Tomcat
- How can I create an utility class?
- difference between ‘static int’ and ‘int’ in java
- How to use PrintWriter and File classes in Java?
- Error message “unreported exception java.io.IOException; must be caught or declared to be thrown”
- cannot make a static reference to the non-static field
- Double cannot be dereferenced?
- Encrypt Password in Configuration Files?
- Java Enum return Int
- Java, Simplified check if int array contains int
- Converting a string to an integer on Android
- Error: Generic Array Creation
- How to center the text in a JLabel?
- What is the use of a private static variable in Java?
- Call a Class From another class
- Installing WindowBuilder on Eclipse Neon
- Java: how to initialize String[]?
- What is method hiding in Java? Even the JavaDoc explanation is confusing
- Error: class X is public should be declared in a file named X.java
- How can I remove a substring from a given String?
- Java get String CompareTo as a comparator object
- What does super.paintComponent(g) do?
- Union or intersection of Java Sets
- Get an OutputStream into a String
- Difference between string object and string literal
- Best way to create enum of strings?
- Downloading a file from spring controllers
- Java Error Exception in thread “AWT-EventQueue-0” java.lang.StackOverflowError
- Write a program that reads an unspecified number of integers, determines pos, neg, total, average
- Parsing JSON string in Java
- java attempting to assign weaker access privilege error
- Getting a “no interface expected here” when I try to subclass ServiceConnection [duplicate]
- How to determine day of week by passing specific date?
- Python-like list comprehension in Java
- Can an int be null in Java?
- What’s the difference between HashSet and Set?
- Netbeans – Error: Could not find or load main class
- Eclipse IDE – Error: Build path specifies execution environment Java SE 1.7
- Why int[] a = new int[1] instead of just int a?
- ArithmeticException: “Non-terminating decimal expansion; no exact representable decimal result”
- When to use a Map instead of a List in Java?
- How do I replace a character in a string in Java?
- Java Swing setting JPanel Size
- how to use “tab space” while writing in text file
- Change Name of Import in Java, or import two classes with the same name
- set background color: Android
- Class JavaLaunchHelper is implemented in two places
- No Main class found in NetBeans