This question already has answers here:
How to get a random between 1 - 100 from randDouble in Java?
(6 answers)
Closed 2 years ago.
So I am trying to make 100 random integers from 1-100 print out. I can make doubles of this but not integers. Please show me how to make this into integers, here is the code.
for(i = 1; i <= 100; i++) {
double r = Math.random();
double in = r * 100;
System.out.print(in + ", ");
}
Please show me what I messed up or if I need to use an alternate technique for this
You can use Random object instead. from java.util package
Random random = new Random();
int value = random.nextInt(100);
Use ThreadLocalRandom:
for (int i = 1; i <= 100; i++) {
System.out.print(ThreadLocalRandom.current().nextInt(100) + ", ");
}
Import the class java.util.Random
Make the instance of the class Random, i.e., Random rand = new Random()
Invoke one of the following methods of rand object:
nextInt(upperbound) generates random numbers in the range 0 to upperbound-1.
nextFloat() generates a float between 0.0 and 1.0.
nextDouble() generates a double between 0.0 and 1.0
For example if you want to generate a random number between 1 to 100 the the code will be.
import java.util.Random;
Random r = new Random();
Int number = r.nextInt(100);
So variable number will be a random number between 1 to 100
Whats wrong is the statement double in = r * 100;
It must be
int b = (int) (Math.random()*100)%100; //range [0-99]
Just add 1 to b if you strictly want b in the range [1-100]
Related
This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 9 months ago.
So I'm trying to figure out how to print out a random number between 1 to 1000.
I tried:
double a = 1+ (Math.random()*1000);
System.out.println(a);
But when I try this i get numbers with a bunch of decimals. I do not want any decimals. Anyone can help? I want to get a value like 50 or 289 or 294. I do not want to get a number like 234.5670242
or 394.220345. Help if you can. will appreciate it. Thank you.
Try this:
public static void main(String args[])
{
// define the range
int max = 1000;
int min = 1;
int range = max - min + 1;
// generate random numbers within 1 to 1000
for (int i = 0; i < 1000; i++) {
int rand = (int)(Math.random() * range) + min;
// Output
System.out.println(rand);
}
}
Is there a way to generate random double value outside a specified range?
I know there is one within the range:
Random r = new Random();
double randomValue = rangeMin + (rangeMax - rangeMin) * r.nextDouble();
I would require one that is outside the range e.g
the range is 20 - 50 and I would like a number below 20 or higher than 50.
Could someone please advise?
Maybe something like (for numbers 1-20 and 50-100):
Random r = new Random();
double randomValue = r.nextDouble()*70;
if(randomValue>20) randomValue+=30;
It is not resource expensive and easy to understand.
You can try somethink like this :
Random rnd = new Random();
double x=0;
do{
x = rnd.nextDouble()*100;
}while(x>20 && x<50 );
System.out.println(x);
}
You generate a random double ( need multiply by 100 because generate double return value between 0 and 1 ) and loop while result >20 and <50
If you want a double, any double, outside a specific range, then you you can take advantage of the fact that a double is represented by 64 bits, and you can convert any 64-bit value to a double using the Double.longBitsToDouble method:
public static void main(String[] args) {
Random r = new Random();
double d;
do {
d = Double.longBitsToDouble(r.nextLong());
} while (d >= 20 && d <= 50);
System.out.println(d);
}
First of all you always need some upper bound for the number you're generating, so 'above rangeMax' won't really do. What you basically want is to have a number generated that falls into one of two ranges [0,minRange] or [maxRange, maxValue].
You can either go with the 'lazy approach' which is just generating a value between 0 and maxValue and generate a new one until you get on that does not fall into the [minRange,maxRange] range or you could do a two step generation process, i.e. generate a random number that determines whether you generate a number in the lower range or the upper range, for instance:
public static void main(String[] args) {
double result = (new Random().nextInt(2)) == 0 ? generateInRange(0, 20) : generateInRange(50, Double.MAX_VALUE);
}
private static double generateInRange(double min, double max) {
return new Random().nextDouble() * (max-min) + min;
}
This does give you a 50/50 chance of ending up in the lower and upper range, so you might want to tweak that.
This question already has answers here:
Generating an Odd Random Number between a given Range
(10 answers)
Closed 7 years ago.
I'm trying to generate odd numbers randomly. I tried this, but it generates even numbers also:
int coun=random.nextInt();
for(int i=1; i<100; i++){
if(i%2==1){
coun=random.nextInt(i);
}
}
How can I generate odd numbers randomly?
You could add 1 to even numbers
int x=(int) (Math.random()*100);
x+=(x%2==0?1:0);
or multiply the number by 2 and add one
int x=(int) (Math.random()*100);
x=x*2+1;
a lot of possible solutions.
All numbers of the form 2*n + 1 are odd. So one way to generate a random odd number would be, to generate a random integer, multiply it by 2, and add 1 to it:
int n = random.nextInt();
int r = 2 * n + 1; // Where r is the odd random number
For each random number n, there is a unique odd random number r generated (in other words, it is a bijection) - thus ensuring unbiasedness (or at least, as much unbiasedness as the function random.nextInt()).
There is 50 odd numbers between 0 and 100. To select one of them you can do
int n = random.nextInt(50);
to get the n-th odd number you can
int odd = n * 2 + 1;
Putting it all together
int odd = random.nextInt(max / 2) * 2 + 1;
One solution would be to test wheter the random integer value is odd or not. If it is not, you can add or subtract one with half probability.
Random random = new Random();
int i = random.nextInt();
if (i % 2 == 0) {
i += random.nextBoolean() ? 1 : -1;
}
I need to make a random number between 1 and 20, and based on that number (using "If - Then" statements), I need to set the image of an ImageView.
I know that in Objective-C, it goes like this:
int aNumber = arc4Random() % 20;
if (aNumber == 1) {
[theImageView setImage:theImage];
}
How can I do this in Java? I have seen it done this way, but I do not see how I can set the range of numbers (1-20, 2-7, ect).
int aNumber = (int) Math.random()
Docs are your friends
Random rand = new Random();
int n = rand.nextInt(20); // Gives n such that 0 <= n < 20
Documentation:
Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.
Thus, from this example, we'll have a number between 0 and 19
Math.random() returns an double from [0,1[.
Random.nextInt(int) returns an int from [0, int[.
You can try:
int aNumber = (int) (20 * Math.random()) + 1;
or
Random rand = new Random();
int n = rand.nextInt(20) + 1;
You can use Math.random() to generate a double between 0 and 1 non-inclusive. Android Javadoc here.
This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 3 years ago.
I would like to get a random value between 1 to 50 in Java.
How may I do that with the help of Math.random();?
How do I bound the values that Math.random() returns?
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);
int max = 50;
int min = 1;
1. Using Math.random()
double random = Math.random() * 49 + 1;
or
int random = (int )(Math.random() * 50 + 1);
This will give you value from 1 to 50 in case of int
or 1.0 (inclusive) to 50.0 (exclusive) in case of double
Why?
random() method returns a random
number between 0.0 and 0.9..., you
multiply it by 50, so upper limit
becomes 0.0 to 49.999... when you add 1, it becomes 1.0 to 50.999..., now when you truncate to int, you get 1 to 50. (thanks to #rup in comments). leepoint's awesome write-up on both the approaches.
2. Using Random class in Java.
Random rand = new Random();
int value = rand.nextInt(50);
This will give value from 0 to 49.
For 1 to 50: rand.nextInt((max - min) + 1) + min;
Source of some Java Random awesomeness.