How do I generate a random value between two numbers [duplicate] - java

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Java: generating random number in a range
How do I generate a random value between two numbers. Random.nextInt() gives you between 0 and the passed value. How do I generate a value between minValue and a maxValue

Write a method like:
public static int getRandom(int from, int to) {
if (from < to)
return from + new Random().nextInt(Math.abs(to - from));
return from - new Random().nextInt(Math.abs(to - from));
}
This also takes account for facts, that nextInt() argument must be positive, and that from can be bigger then to.

random.nextInt(max - min + 1) + min will do the trick. I assume you want min <= number <= max

Example: Generating a number from 1 to 6
Because nextInt(6) returns a number from 0-5, it's necessary to add 1 to scale the number into the range 1-6
static Random randGen = new Random();
int spots;
. . .
spots = randGen.nextInt(6) + 1;

Related

How toMake Math.random generate number between 1 and 1000? [duplicate]

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);
}
}

Randomly generate odd numbers in the range [duplicate]

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;
}

How to make purely random generation of numbers in certain interval

I have an generation method that generates numbers between 1 and 8. But the problem is that this method often generates the numbers 1-7 and almost never number 8. My question is that how to generate random numbers in certain interval but that all numbers appear approximately same times ?
EDIT:
my number generator
public int generateNumber() {
Random r = new Random();
return r.nextInt(8 - 1) + 1;
}
The random generator generates an "uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)" Please refer to the documentation:
http://docs.oracle.com/javase/7/docs/api/java/util/Random.html
You can do:
random.nextInt(max - min + 1) + min
And it should be fine.
Alternatively,
Random randomGenerator = new Random();
for (int idx = 1; idx <= NUMBER_OF_INTEGERS_YOU_WANT; idx++){
int randomInt = randomGenerator.nextInt(8)+1;
CODE_HERE
}
"almost never number 8": If that generator ever generates 8 there is something wrong.
The nextInt gives a number between 0 and 6 (borders inclusive) and adding one gives the interval [1,7].
To get numbers in [1,8] you can use
r.nextInt(8) + 1
You need something like
return r.nextInt(8) + 1;
Which will return values from 1 to 8. So it starts with 0 and generated number max uptil n-1 i.e. 7 as per this.
In your case, you have r.nextInt(8 - 1) which evaluates to r.nextInt(7) so it would generate numbers from 0 -6 and on top of that you are adding 1 to number from above api, so your range is 1-7 and not 1-8.

Random number between two points, needs to be both negative, positive or a mix [duplicate]

This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 9 years ago.
I'm writing code for a kind of minigame in another game (Minecraft) in Java and I stumbled upon a problem.
I have two ints, int point 1 and int point 2. They are x coordinates, from ex: -100000 to 100000. But also -10000 to -5000, and 10000 to 20000. So negative to positive, but also negative to negative and positive to positive.
And that is the problem. I need to find a solution how to get random numbers from ex -100 to -50. But the same code has to be used with -100 to 50, and 10 to 50.
If you guys can help me, that'd be great!
Greetings,
Jesse.
PS: If you need a code snippet or something, just say it.
Silviu Burcea's comment contains a google search which answers your question. Numbers are numbers. It doesn't matter what sign they have. So going to the first result from the google search gave the answer:
public static void main(String[] args){
Random rand = new Random();
for(int j = 0; j < 100; j++){
// just modified the line from the other thread
// which was "int randomNum = rand.nextInt((max - min) + 1) + min;"
int i = rand.nextInt(-50 - -100 + 1) + -100;
System.out.println("Next value = " + i);
}
}
That prints out pseudo random numbers between -100 and -50. So yeah, your question is a duplicate.
int mix = Math.min(point1, point2) + (int)(Math.abs(point1 - point2) * Math.random()));
EDIT:
int mix = Math.min(point1, point2) + Math.round(Math.abs(point1 - point2) * Math.random()));
EDIT2 :
int mix = Math.min(p1, p2) + (int)Math.round(-0.5f+(1+Math.abs(p1 - p2))*Math.random());
The distirubtion of this expression evaluation seems to be probably better. But this is not an optimised solution in fact.

Getting random numbers in Java [duplicate]

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.

Categories

Resources