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 am very new to Java and I am stuck on this, I am using the formula:
min + (int)(Math.random()*(max-min+1))
and I have to write statements that assign random integers to the variable x in the following ranges
1 < x <= 8
1 being min and 8 being max
Am I correct that it would be 1 + (int)(Math.random()*(8-1+1))?
-5 < x <= 3
3 being min and -5 being max
and this would be 3 + (int)(Math.radom()*(-5-3+1))?
Any help would be greatly appreciated.
You want a formula to take a real number in the range [0..1), and return an integer in the range [1..8].
When random() picks a real number it the range [0..1),
and you multiply it by 8,
you get a value in the range [0.0 .. 8.0).
Then you then convert to (int), you have an integer in the range [0 .. 7],
because conversion to (int) rounds using the 'floor' step function.
Add one.
use following code. It will generate nos between 0 and max value supplied.
Random generator = new Random(); //Creates a new random number generator
for(int i=0; i<100; i++){
/**
*Returns a pseudorandom, uniformly distributed int value
* between 0 (inclusive) and the specified value (exclusive), drawn from
* this random number generator's sequence
*/
System.out.println(generator.nextInt(100));
}
Get random value with range ( min, max ] , this is the same with the question.
If JDK 1.7 version is used, then you can use one line code to implement such function:
ThreadLocalRandom.current().nextInt(min, max)+1
If JDK version is under 1.7, we can implement it using the following way.
Random random = new Random();
random.nextInt(max - min) + min +1;
2.Get random value with range [ min, max )
Use,
ThreadLocalRandom.current().nextInt(min, max)
or
random.nextInt(max - min) + min ;
Math.random() gives you between 0 and 1. Multiply this by your range, the size of the range, not the absolute value. If you want 1-10 that's 9, if you want 0-10 that's 10, if you want 5-7 that's 2, etc.
Then add or subtract to go from 0 to the starting value.
If you want 0-9 then you're done (you should have multiplied by 9 in the previous step)
If you want 1-10 then add 1
If you want -5 to 5 then subtract 5
If you want 5-7 then do (Math.random()*2)+5;
Related
I was going through some coding exercises, and had some trouble with this question:
From 5 dice (6-sided) rolls, generate a random number in the range [1 - 100].
I implemented the following method, but the returned number is not random (called the function 1,000,000 times and several numbers never show up in 1 - 100).
public static int generator() {
Random rand = new Random();
int dices = 0;
for(int i = 0; i < 5; i++) {
dices += rand.nextInt(6) + 1;
}
int originalStart = 5;
int originalEnd = 30;
int newStart = 1;
int newEnd = 100;
double scale = (double) (newEnd - newStart) / (originalEnd - originalStart);
return (int) (newStart + ((dices - originalStart) * scale));
}
Ok, so 5 dice rolls, each with 6 options. if they are un-ordered you have a range of 5-30 as mentioned above - never sufficient for 1-100.
You need to assume an order, this gives you a scale of 1,1,1,1,1 - 6,6,6,6,6 (base 6) assuming 1 --> 0 value, you have a 5 digit base 6 number generated. As we all know 6^5 = 7776 unique possibilities. ;)
For this I am going to give you a biased random solution.
int total = 0;
int[] diceRolls;
for (int roll : diceRolls) {
total = total*6 + roll - 1;
}
return total % 100 + 1;
thanks to JosEdu for clarifying bracket requirement
Also if you wanted to un-bias this, you could divide range by the maxval given in my description above, and subsequently multiply by your total (then add offset), but you would still need to determine what rounding rules you used.
Rolling a 6 sided die 5 times results in 6^5 = 7776 possible sequences, all equally probable. Ideally you'd want to partition those sequences into 100 groups of equal size and you'd have your [1 - 100] rng, but since 7776 isn't evenly divisible by 100 this isn't possible. The best you can do to minimize the bias is 76 groups mapped to by 78 sequences each and 24 groups mapped to by 77 sequences each. Encode the (ordered) dice rolls as a base 6 number n, and return 1 + (n % 100).
Not only is there no way to remove the bias with 5 dice rolls, there is no number of dice rolls that will remove the bias entirely. There is no value of k for which 6^k is evenly divisible by 100 (consider the prime factorizations). That doesn't mean there's no way to remove the bias, it just means you can't remove the bias using a procedure that is guaranteed to terminate after any specific number of dice rolls. But you could for example do 3 dice rolls producing 6^3 = 216 sequences encoded as the base 6 number n, and return 1 + (n % 100) if n < 200. The catch is that if n >= 200 you have to repeat the procedure, and keep repeating until you get n < 200. That way there's no bias but there's also no limit to how long you might be stuck in the loop. But since the probability of having to repeat is only 16/216 each time, from a practical standpoint it's not really much of a problem.
The problem is there aren't enough random values in 5-30 to map one to one to 1-100 interval. This means certain values are destined to never show up; the amount of these "lost" values depends on the size ratio of the two intervals.
You can leverage the power of your dice in a way more efficient way, however. Here's how I'd do it:
Approach 1
Use the result of the first dice to choose one subinterval from the
6 equal subintervals with size 16.5 (99/6).
Use the result of the second dice to choose one subinterval from the 6 equal sub-subintervals of the subinterval you chose in step one.
Use... I guess you know what follows next.
Approach 2
Construct your random number using digits in a base-6 system. I.E. The first dice will be the first digit of the base-6 number, the second dice - the second digit, etc.
Then convert to base-10, and divide by (46656/99). You should have your random number. You could in fact only use 3 dice, of course, the rest two are just redundant.
I am creating a random number from 1-100, I was looking at some Stackoverflow questions to look for the proper way and I got confused by the many different suggestions.
What is the difference between using this:
int random= (int)(Math.random()*((100-1)+1));
this:
int random= (int)(Math.random()*(100);
and this:
int random= 1+ (int)(Math.random()*((100-1)+1));
int random = (int)(Math.random()*(x);
This sets random equal to any integer between 0 and x - 1.
int random = 1 + (int)(Math.random()*(x);
Adding 1 to the overall expression simply changes it to any integer between 1 and x.
(int)(Math.random()*((100-1)+1))
is redundant and equivalent to
(int)(Math.random()*(100)
So take note that:
1 + (int)(Math.random()*(x) returns an int anywhere from 1 to x + 1
but
(int)(Math.random()*(x + 1) returns an int anywhere from 0 to x + 1.
I recommend that you use Random and nextInt(100) like so,
java.util.Random random = new java.util.Random();
// 1 to 100, the 100 is excluded so this is the correct range.
int i = 1 + random.nextInt(100);
it has the added benefit of being able to swap in a more secure random generator (e.g. SecureRandom). Also, note that you can save your "random" reference to avoid expensive (and possibly insecure) re-initialization.
The first is equivalent to the second. Both will give a random integer between 0 and 99 (inclusive, because Math.random() returns a double in the range [0, 1)). Note that (100-1)+1 is equivalent to 100.
The third, will give an integer between 1 and 100 because you are adding 1 to the result above, i.e. 1 plus a value in the range [0, 100), which results in the range [1, 101).
So I need to output Random numbers between -50 and +50 using Java Standard Collections
These are then to be input into an ArrayList and sorted first ascending and 2nd time descending.
The Input and Sorting I Know how to do, but How can I generate random numbers between -50 and 50, considering these have to be input by a for do loop, one-by-one into an ArrayList
N.B. this question was for an exam, so even though the for-do loop (as an iterative) causes overheads I still need to follow these guidelines.
Thanks a lot
You can generate random number within a range start with 0 like this :
Random randomGenerator = new Random();
int randomInt=randomGenerator.nextInt(101); //This function generate number in range 0 to 100.
randomInt -= 50; //It will give you number between -50 to 50
If you want integers, then Random.nextInt will do it.
myRandom.nextInt(101) - 50
should produce a pseudo-rangom integer using a flat distribution in [-50, 50].
public int nextInt(int n)
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 n possible int values are produced with (approximately) equal probability.
another way as well
to produce a random double :
Math.random()
to produce a random double less than 100 for example
Math.random()*100
you can cast the result to any numerical type yu want
you can produce a your result like this
double r = Math.random()*50
if(r % (int)(Math.random()*10 == 2){
r = r*-1;
}
something like that
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Java: generating random number in a range
I want to generate a random int in a logical range. So, say for example, I'm writing a program to "roll" a dice with a specified number of sides.
public int rollDice() {
Random generator = new Random();
return generator.nextInt(sides);
}
Now the problem becomes that this will return values between sides and zero, inclusive, which makes no sense because most dice go from 1 to 6, 9, etc. So how can I specify that nextInt should work between 1 and the number of sides?
To generate a random int value (uniform distribution) between from and to (inclusive) use:
from + rndGenerator.nextInt(to - from + 1)
In your case (1..sides):
1 + rndGenerator.nextInt(sides)
Why the following produce between 0 - 9 and not 10?
My understanding is Math.random() create number between 0 to under 1.0.
So it can produce 0.99987 which becomes 10 by *10, isn't it?
int targetNumber = (int) (Math.random()* 10);
Casting a double to an int in Java does integer truncation. This means that if your random number is 0.99987, then multiplying by 10 gives 9.9987, and integer truncation gives 9.
From the Math javadoc :
"a pseudorandom double greater than or equal to 0.0 and less than 1.0"
1.0 is not a posible value with Math.random. So you can't obtain 10. And (int) 9.999 gives 9
Cause (Math.random()* 10 gets rounded down using int(), so int(9.9999999) yields 9.
Because (int) rounds down.
(int)9.999 results in 9.
//integer truncation
0.99987 which becomes 10 by *10
Not when I went to school. It becomes 9.9987.
Math.floor(Math.random() * 10) + 1
Now you get a integer number between 1 and 10, including the number 10.
You could always tack on a +1 to the end of the command, allowing it to add 1 to the generated number. So, you would have something like this:
int randomnumber = ( (int)(Math.random( )*10) +1);
This would generate any integer between 1 and 10.
If you wanted any integer between 0 and 10, you could do this:
int randomnumber = ( (int)(Math.random( )*11) -1);
Hope this helps!
you can add 1 to the equation so the output will be from 1 to 10 instead of 0 to 9
like this :
int targetNumber = (int) (Math.random()* 10+1);