Java Generate Random Number Between Two Given Values
You could use e.g. r.nextInt(101)
For a more generic "in between two numbers" use:
Random r = new Random();
int low = 10;
int high = 100;
int result = r.nextInt(high-low) + low;
This gives you a random number in between 10 (inclusive) and 100 (exclusive)
random integers between 2 values
If you want a value between two integer values (min and max)
Here you go:
Random r = new Random();
int randInt = r.nextInt(max-min) + min;
System.out.println(randInt);
So max would be 5 and min would be 2 if you want a integer between 2 and 5
Need to generate random numbers between two values ignoring the given values
The correct formula for random numbers in a range is Min + (Math.random() * (Max - Min))
(including lower bound and excluding upper bound).
For your numbers the correct way would be
double fv = (-7) + Math.random() * (0 - -7);
Since you used only one -
in the formula the Min
value was assumed to be 7 and not -7 which leads to wrong results.
How to generate a random number between two values then set to TextView?
Random random = new Random();
int value = random.nextInt(max - min) + min;
num1.setText(value+"");
You can't use .setText(value), because integer will be a resource link to /res/strings! Add +"" after number!!!
There are two (or more) methods:
public final void setText (CharSequence text) - To set text
public final void setText (int resid) - to set text from file /res/string/strings.xml
Getting random numbers in Java between two numbers
The static method Math.random()
returns a number between 0 and 1 so you just have to multiply the result with the difference between you minimal and maximal value and add this to your minimal value.
int min = 65;
int max = 122;
int random = (int) min + (max - min) * Math.random();
Generating a Random Number between 1 and 10 Java
As the documentation says, this method call returns "a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)". This means that you will get numbers from 0 to 9 in your case. So you've done everything correctly by adding one to that number.
Generally speaking, if you need to generate numbers from min
to max
(including both), you write
random.nextInt(max - min + 1) + min
getting a random long in range of two values
The delay value will be a random number from 2500 to 4999, since nextInt upper bound is exclusive. From the Java SE Specification:
public int nextInt(int bound)
Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. The general contract of nextInt is that one int value in the specified range is pseudorandomly generated and returned. All bound possible int values are produced with (approximately) equal probability.
rand.nextInt(5000-2500) + 2500
is the same as rand.nextInt(2500) + 2500
(which in my opinion is a cleaner way to write it).
Also int values are converted to long without losing information (long has a wider range than int so no problems there). From the Java SE Specification:
A conversion from type int to type long requires run-time sign-extension of a 32-bit integer value to the 64-bit long representation. No information is lost.
Finally about random not being truly random, you are right it's pseudo-random (which means that all posible numbers has not an exact equal probability, but an approximately equal one). I'm not completely sure what are you using this code for, but I think this will be "random enough" for your needs.
You can have a look at this post about random values in java between a given range.
And this post about Random randomness.
PD: A nice improvement in your code (imho) would be making the rand variable private static
instead of local.
Related Topics
Retrieve Version from Maven Pom.Xml in Code
Ignoring Ssl Certificate in Apache Httpclient 4.3
Why Are Data Transfer Objects (Dtos) an Anti-Pattern
Convert Java.Time.Localdate into Java.Util.Date Type
Naming Threads and Thread-Pools of Executorservice
Jpa: How to Have One-To-Many Relation of the Same Entity Type
Good Reasons to Prohibit Inheritance in Java
@Aspectj Pointcut for All Methods of a Class with Specific Annotation
Declaring an Unsigned Int in Java
How to Find the Sum of All the Numbers in an Array in Java
Why Do We Need Immutable Class
How to Format a Java String with Leading Zero
Converting a Java Keystore into Pem Format
Should I Use Java's String.Format() If Performance Is Important
How to Convert a String to Double in Java Using a Specific Locale