I am looking to get random number in between 1000 to 8192000. The random number should be like 1000 , 2000,3000 to 8192000.
Following is the code that i have tried but did not got any success.
ran.nextInt(8192000 - 1000)%1000;
What should I change in order to get number in term of 1000, 2000, 3000...
The easiest approach would seem to generate a random number between 1 and 8192 and just multiply it by 1000:
Random randomGenerator = new Random();
long randomNumber = (1 + randomGenerator.nextInt(8192)) * 1000L;
If you want 8192000 inclusive try:
Random random = new Random();
for (int i = 0; i < 10; i++) {
System.out.println((random.nextInt(8192) + 1) * 1000);
}
Here you get values: 1000, 2000, ..., 8192000
Related
I'm using Java's Random to generate random numbers: 1.0, 1.1 - 10
Random random = new Random();
return (double) ((random.nextInt(91) + 10) / 10.0);
When I printed a lot of these numbers (2000), I noticed 1.0 and 10 are significant less printed than all others (repeated 20 times, happened every time). Most likely because 0.95-0.99 and 10.01-10.04 aren't generated.
Now I have read a lot of threads about this, but it still leaves me to the following question:
If these numbers would represent grades for example, you can't get lower than a 1 and higher than a 10 here, would it be legit to extend the range from 0.95 up to 10.04?
Random random = new Random();
return Double.valueOf((1005-95) / 100);
To generate a random value between 1.1 and 10 use the following code:
double min = 1.1d;
double max = 10d;
Random r = new Random();
double value = min + (max - min) * r.nextDouble();
Afterwarsds you can use Math.floor(value) too round your result
This premise
Most likely because 0.95-0.99 and 10.01-10.04 aren't
generated.
is wrong. You generate random ints from 10 inclusive to 100 inclusive. Lower fractions and rounding of values does not play into it. Random nextInt is random in the interval; the end cases is not discriminated against.
I think your method
Random random = new Random();
return (double) ((random.nextInt(91) + 10) / 10.0);
Looks correct. I would suggest measuring the anomaly you are experiencing, maybe it is a human bias from when you are merely looking at the output.
Here is some code that measures the actual random generation of the 91 values. It is before the conversion to double which is not ideal.(but I do not see how dividing by 10 does anything else than map values as 10 -> 1.0, 11 -> 1.1 ... 99 -> 9.9 and 100 -> 10.0. A measure of the final result would of course be more desirable)
Random random = new Random();
int[] measure = new int[101];
for (int i = 0; i < 10000; i++) {
int number = (random.nextInt(91) + 10);
measure[number]++;
}
for (int i = 0; i < 101; i++) {
System.out.println(i + " count: " + measure[i]);
}
Looking at the results from that code the 10 and 100 values seem to come up as often as any other.
Anybody know how to create random number between 2 numbers but it just change in a range ?
For instance, create random number between 10 - 100, with the change every time is in range [-5,+5].
Ex: if the first random is 17, the after random number will be in range [12, 22]
Thank you!
This algorithm picks an initial random value x, in range [0, 100]. After that every new random value y will be within 5 of the previous random value x.
int maximum = 100;
int minimum = 0;
Random rn = new Random();
int range = maximum - minimum + 1;
int randomNum = rn.nextInt(range) + minimum;
System.out.println(randomNum);
for (int i=0; i< 100; i++) {
maximum = randomNum + 5;
minimum = randomNum - 5;
range = maximum - minimum + 1;
randomNum = rn.nextInt(range) + minimum;
System.out.println(randomNum);
}
I think i understand your question, but little bit confused with your sequence on example. This is one of the possible answer:
In java, you can generate like that:
Random rn = new Random();
int range = maximum - minimum + 1;
int randomNum = rn.nextInt(range) + minimum;
This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 9 years ago.
how would I create up to 15-20 random numbers between 100-200 in java?
I have this atm but it creates any random numbers but I want the numbers to be between 100 and 200 but I don't know how I would go about adding this to the code below. please can someone help.
Random rand = new Random();
int Randnum;
for(int i = 0; i <=20; i++) {
System.out.println(Randnum + " ");
}
}
This has been answered before, but use rand.nextInt(int n). This will generate a number between 0 (inclusive) and n (exclusive). In your case, use rand.nextInt(101)+100 to generate a number between (and including) 100 and 200.
Random rand = new Random();
int Randnum;
for(int i = 0; i <=20; i++) {
Randnum = rand.nextInt(101)+100;
System.out.println(Randnum + " ");
}
}
Random rand = new Random();
int Randnum;
for (int i = 0; i <= 20; i++) {
Randnum =rand.nextInt(101) + 100;
System.out.println(Randnum + " ");
}
nextInt(n) method of Random class returns a number between 0(inclusive) and n(exclusive).
In your case, you need a number between 100 and 200, so fetch a number using nextInt with values ranging from 0 to 101 (you get numbers from 0 to 100) and add 100 to it to get numbers from 100 to 200.
You can either use Random or Math#random
use Math.random()
You can do something like:
int[] randnum = new int[20];
for(int i = 0; i <20; i++)
{
randnum[i] = (int)((Math.random() * 101)+100) ;
}
now you have 20 integers between 100 and 200.
Java How do I create a random number mod 5 ?
I need only random numbers 0-100, divisible by 5
something like RandomNumber.nextInt(100) % 5
Do this:
int randomMultipleOf5 = 5*random.nextInt(21);
21 is needed to get an integer in the range 0-20 (inclusive). When multiplied by 5 you get a number in the range 0-100 (inclusive).
You can just do:
Random r = new Random();
int randomMultipleOfFive = r.nextInt(21)*5; //generates a number between 0 and 20 inclusive then *5
How about;
int number = RandomNumber.nextInt(21) * 5;
To clarify, nextInt(21) generates a number from 0-20 making 100 a possible generated number, while nextInt(20) would only max generate 95.
int random = new Random().nextInt(21) * 5
Try this:
Random random = new Random();
for(int i=0; i<50; ++i){
System.out.println(random.nextInt(21) * 5);
}
Okay, I'm still fairly new to Java. We've been given an assisgnment to create a game where you have to guess a random integer that the computer had generated. The problem is that our lecturer is insisting that we use:
double randNumber = Math.random();
And then translate that into an random integer that accepts 1 - 100 inclusive. I'm a bit at a loss. What I have so far is this:
//Create random number 0 - 99
double randNumber = Math.random();
d = randNumber * 100;
//Type cast double to int
int randomInt = (int)d;
However, the random the lingering problem of the random double is that 0 is a possibility while 100 is not. I want to alter that so that 0 is not a possible answer and 100 is. Help?
or
Random r = new Random();
int randomInt = r.nextInt(100) + 1;
You're almost there. Just add 1 to the result:
int randomInt = (int)d + 1;
This will "shift" your range to 1 - 100 instead of 0 - 99.
The ThreadLocalRandom class provides the int nextInt(int origin, int bound) method to get a random integer in a range:
// Returns a random int between 1 (inclusive) & 101 (exclusive)
int randomInt = ThreadLocalRandom.current().nextInt(1, 101)
ThreadLocalRandom is one of several ways to generate random numbers in Java, including the older Math.random() method and java.util.Random class. The advantage of ThreadLocalRandom is that it is specifically designed be used within a single thread, avoiding the additional thread synchronization costs imposed by the other implementations. Therefore, it is usually the best built-in random implementation to use outside of a security-sensitive context.
When applicable, use of ThreadLocalRandom rather than shared Random objects in concurrent programs will typically encounter much less overhead and contention.
Here is a clean and working way to do it, with range checks! Enjoy.
public double randDouble(double bound1, double bound2) {
//make sure bound2> bound1
double min = Math.min(bound1, bound2);
double max = Math.max(bound1, bound2);
//math.random gives random number from 0 to 1
return min + (Math.random() * (max - min));
}
//Later just call:
randDouble(1,100)
//example result:
//56.736451234
I will write
int number = 1 + (int) (Math.random() * 100);
double random = Math.random();
double x = random*100;
int y = (int)x + 1; //Add 1 to change the range to 1 - 100 instead of 0 - 99
System.out.println("Random Number :");
System.out.println(y);