how to generate positive and negative numbers in Java [duplicate] - java

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to generate random positive and negative numbers in java
Hello I am trying to create a method in Java to create negative and positive values in Java.
the problem is that I don't know how to get this programmed but I do know the logic.. here is what I tought it should be
Random generator = new Random();
for (int i = 0; i < 21; i++)
{
System.out.print(generator.nextInt(100) + 1);
System.out.println();
}
but with the code above I get only positive values and I need values between -100 and 100 but how can I accomplish something like that?

You can use:
Random generator = new Random();
int val = 100 - generator.nextInt(201);
Or, as JoachimSauer suggested in the comments:
int val = generator.nextInt(201) - 100;

The general formula is
int val = rand.nextInt(max - min + 1) + min;
Note that min and max can be negative. (max > min)

Related

How to make a random number between -1 and 1 in Java [duplicate]

This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 6 years ago.
Trying to figure this out for a while, need a random number that is either -1, 0 or 1. Any help would be great.
Random random = ThreadLocalRandom.current();
int randomNumber = random.nextInt(3) - 1;
Will do the work
If you use Java < 8 version then old approach will be great for you:
You can use java.util.Random class and its method nextInt(int bound). This method generates a random integer from 0 (inclusive) to bound (exclusive). If you want some specific range then you will need to perform some simple math operations:
Cut the range: max - min
If you want to include upper bound value then will add 1: nextInt((max - min) + 1) (optional step)
Shift the generated number to the value of lower bound: nextInt((max - min) + 1) + min
Result:
Random r = new Random();
int randomNumber = r.nextInt((max - min) + 1) + min;
If you use Java >= 8 version, then there will be easier approach for you:
Now java.util.Random class provides other methods according to Stream api like:
public IntStream ints(int randomNumberOrigin, int randomNumberBound)
public IntStream ints(long streamSize, int randomNumberOrigin, int randomNumberBound)
Now you can generate a random integer from origin (inclusive) to bound (exclusive) like that:
Random r = new Random();
r.ints(min, max).findFirst().getAsInt();
If you want to include upper bound into generating process then:
r.ints(min, (max + 1)).findFirst().getAsInt();
To prevent producing unlimited stream:
r.ints(min, (max + 1)).limit(1).findFirst().getAsInt();
or
r.ints(1, min, (max + 1)).findFirst().getAsInt();
answer 1
long l = System.currentTimeMillis();
int randomNumber = (int) (l % 3) - 1;
answer 2
int randomNumber = (int) (Math.random() * 3) - 1;
answer 3
Random random = new Random();
int randomNumber = random.nextInt(3) - 1;
Look at this one
Random r = new Random();
int n = r.nextInt((1 - -1) + 1) + -1;
System.out.println(n);
it will generate random between the range you want. output will be 1 or 0 or -1.
You can use int arrays with Random class. Store your values in an integer array, and generate random number for the index of the array.
Sample Code:
final int[] arr = new int[] { -1, 0, 1 };
Random number = new Random();
int n = number.nextInt(arr.length);
System.out.println(arr[n]); //arr[n] will give you random number

how to create Random numbers [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.
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.

How to create a random number with range? [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 am trying to create a random phone number with a range. The format being (xxx)-xxx-xxx and the area code not starting with 0,8, or 9 and the next set of three being in a range from 100-742 and then the last set of 4 can be any digit. How would i create the first two parts? Any help would be appreciated. Thanks!
import java.util.*;
import java.text.*;
public class PhoneNumber{
public static void main(String[] arg){
Random ranNum = new Random();
//int areaCode = 0;
//int secSet = 0;
//int lastSet = 0;
DecimalFormat areaCode = new DecimalFormat("(000)");
DecimalFormat secSet = new DecimalFormat("-000");
DecimalFormat lastSet = new DecimalFormat("-0000");
//DecimalFormat phoneNumber = new DecimalFormat("(###)-###-####");
int i = 0;
//areaCode = (ranNum.nextInt()); //cant start with 0,8,9
//secSet = (ranNum.nextInt()); // not greater than 742 and less than 100
//lastSet = (ranNum.nextInt(999)) + 1; // can be any digits
i = ranNum.nextInt();
System.out.print(areaCode.format(i));
i = ranNum.nextInt();
System.out.print(secSet.format(i));
i = ranNum.nextInt();
System.out.print(lastSet.format(i));
}
}
well, basically, you need to generate numbers in two ranges
[1; 7]
[100; 742]
To have random integer in range [m; n] you could write:
updated (remove Math.random())
int numberInRange = m + new Random().nextInt(n - m + 1);
HTH
1,2,3,4,5,6,7
makes 7 different values
ranNum.nextInt(7)+1; //So 1 is your lowest number and 7 is the number of different solutions
nexInt will range between 0 and intPassed exclusive,
So ranNum.nextInt(7) will run between 0 and 6, + 1 makes 1 .. 7
This will range between 1 and 7
You can take the same principal for the second range
You can try the next:
int sec = java.util.concurrent.ThreadLocalRandom.current().nextInt(100, 743);
And so on with the other parts of the phone number.
This method returns a pseudorandom, uniformly distributed value between the given least value (inclusive) and bound (exclusive).
Just make a random number greater than 99 and less than 800, then the next would be about the same way.
Random rand = new Random();
// add 1 to make it inclusive
max min
int firstRandomSet = rand.nextInt((799 - 100) + 1) + 100;
//none starts with 0,8, or 9
int secondRandomSet = rand.nextInt((742 - 100) + 1) + 100;
//produces anything from 100-742
to get the numbers 0001 - 9999 you'll have to be creative.
int maxValues= 9999;
int thirdRandomSet = rand.nextInt(maxValues);
System.out.printf("%04d\n", thirdRandomSet);

Java - Generating Random Numbers in java between 0.5 and 6.28 [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Generating random number in a range with Java
My code is generating random numbers between 0 and 1.
I need to generate a random number between 0.5 and 6.28.
My current Code:
public class Random_Number_Generator
{
double randomGenerator()
{
Random generator = new Random();
double num = generator.nextDouble();
return num;
}
}
double num = generator.nextDouble()*(6.28-0.5) + 0.5;
As nextDouble returns a number in [0, 1[, you generally have to do
double num = generator.nextDouble()*(max-min) + min;
when you want a number in [min, max[.

Get random integer in range (x, y]? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Java: generating random number in a range
How do I generate a random integer i, such that i belongs to (0,10]?
I tried to use this:
Random generator = new Random();
int i = generator.nextInt(10);
but it gives me values between [0,10).
But in my case I need them to be (0,10].
Random generator = new Random();
int i = generator.nextInt(10) + 1;
How about:
Random generator = new Random();
int i = 10 - generator.nextInt(10);
Just add one to the result. That turns [0, 10) into (0,10] (for integers). [0, 10) is just a more confusing way to say [0, 9], and (0,10] is [1,10] (for integers).

Categories