How do I generate a random int
value in a specific range?
I have tried the following, but those do not work:
Attempt 1:
randomNum = minimum + (int)(Math.random() * maximum);
Bug: randomNum
can be bigger than maximum
.
Attempt 2:
Random rn = new Random(); int n = maximum - minimum + 1; int i = rn.nextInt() % n; randomNum = minimum + i;
Bug: randomNum
can be smaller than minimum
.
Related Posts:
- How do I generate random integers within a specific range in Java?
- Getting random numbers in Java [duplicate]
- Generating a Random Number between 1 and 10 Java
- Generating a Random Number between 1 and 10 Java [duplicate]
- How can I properly compare two Integers in Java?
- How to check whether a int is not null or empty?
- Declaring an unsigned int in Java
- Exception in thread “main” java.lang.NumberFormatException: For input string: “S”
- 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
- int cannot be converted to string?
- How can I convert integer into float in Java?
- How to implement infinity in Java?
- Converting String Array to an Integer Array
- Creating a random string with A-Z and 0-9 in Java
- How to check if the value is integer in java?
- Converting a string to an integer on Android
- Creating random numbers with no duplicates
- Generate a random double in a range
- Convert an integer to an array of digits
- How can I check if a value is of type Integer?
- Problems Generating A Math.random Number, Either 0 or 1
- “Integer number too large” error message for 600851475143
- 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
- Cannot invoke toString() on the primitive type int
- Why int[] a = new int[1] instead of just int a?
- Why int[] a = new int[1] instead of just int a?
- Why cannot cast Integer to String in java?
- How do I resolve ClassNotFoundException?
- Editor does not contain a main type in Eclipse [duplicate]
- How to replace � in a string
- How to decrypt a SHA-256 encrypted string?
- What’s the C++ version of Java’s ArrayList
- Bad Operand Types for Binary Operator “>”?
- How can I prevent java.lang.NumberFormatException: For input string: “N/A”?
- Compiled vs. Interpreted Languages
- How to round up to the next integer?
- Spring @PropertySources value not overriding
- Generating random whole numbers in JavaScript in a specific range?
- Array ArrayList python equivalent
- How to check if an int is a null
- How do I apply the for-each loop to every character in a String?
- Session TimeOut in web.xml
- Java – removing first character of a string
- How to send a Map in JSON
- Getting java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory exception
- Explain the use of a bit vector for determining if all characters are unique
- Java Console Prompt for ENTER input before moving on [duplicate]
- Maven: Failed to read artifact descriptor
- The system cannot find the file specified in java
- Causes of getting a java.lang.VerifyError
- “Unmappable character for encoding UTF-8” error
- Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory
- Android – SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
- How to sort an array of objects in Java?
- Filling a 2D array in Java
- How to compress a String in Java?
- java.lang.ClassNotFoundException: org.sqlite.JDBC error in Sample.java program from xerial
- How can I truncate a double to only two decimal places in Java?
- Cannot resolve symbol “Intent”
- What is the default initialization of an array in Java?
- java sort using anonymous class
- Add String Array to ArrayList
- Java Array Sort descending?
- The import javafx cannot be resolved
- Java Copy Constructor ArrayLists
- Implementing an actionlistener to a JTextField
- StringIndexOutOfBoundsException String index out of range: 0
- Cannot resolve method in java
- How to Set JPanel’s Width and Height?
- addMouseListener for a JPanel
- What is the difference between an Abstract Data Type(ADT) and a Data Structure?
- What is the jasperrepots-fonts jar for and how to use it?
- Does Java have an StringStream equivalent?
- Difference between String replace() and replaceAll()
- How do I get the size of a java.sql.ResultSet?
- Java Main Game Loop
- How to fix java.lang.UnsupportedClassVersionError: Unsupported major.minor version
- JPA or JDBC, how are they different?
- Why is Netbeans suggesting I “Flip operands of the binary operators” in my Java code
- How to return nothing from a function that returns value?
- Java simple code: java.net.SocketException: Unexpected end of file from server
- What is the best Java email address validation method?
- Set Date in a single line
- why android studio show error of “Missing constraints in constraintlayout”?
- Change the size of a JTextField inside a JBorderLayout
- java.lang.ArrayIndexOutOfBoundsException: 0
- What’s wrong with overridable method calls in constructors?
- What is the purpose of the default keyword in Java?
- What exactly is Ajax request? Is it different from Servlet Request?
- java ‘jar’ is not recognized as an internal or external command
- A JRE or JDK must be available in order to run Eclipse. No JVM was found after searching the following locations
- java.util.ConcurrentModificationException with iterator
- How to read file from relative path in Java project? java.io.File cannot find the path specified
- How can I view and edit specific aspects of playerdata from my Minecraft server?
- String index out of bounds exception java