how to create Random numbers [duplicate] - java

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.

Related

Java arraylist throwing dice

I'm new to Java and i'm trying to make a arraylist.
I made a small program that asks the user for a amount of dices to roll :
System.out.println("How many dices do you want to throw?");
int diceAmount = input.nextInt();
then I made a loop to print the dices but I can't get it to make the amount of dices to be random. I have to count the total dices with the random results also:
for (int i = 1; i <= diceAmount; i++) {
System.out.print(i + "-");
Random rand = new Random();
(int i = 1; i <= diceAmount; i++) {
// roll the dice once
int roll1 = rand.nextInt(6) + 1;
System.out.print(i + "-" + roll1);
}
UPDATE:
Here is the way to sum up the numbers. So let's say you roll 2 dice every time.
Random rand = new Random();
// roll the dice once
int roll1 = rand.nextInt(6) + 1;
int roll2 = rand.nextInt(6) + 1;
sum = roll1 + roll2;
System.out.println("You got " + sum + ". Not bad!");
For each die roll you want a random number (presumably 1-6, if its a traditional die). So your loop is correct, but the body of the loop needs fixing:
for(int i = 0; i < diceAmount; i++){ //repeats diceAmount times
//Do loop stuff.
}
To get a random number, start with Math.random(). This will return a random double in the range [0 .. 1). This means that 0 is a valid return, but 1 is not. From there we want to stretch the range to go up to 6.
Math.random() * 6
Returns a random double in the range [0 ..6). We need integers, not doubles, so let's cast that.
(int)(Math.random() * 6)
Returns a random int in the range [0 .. 6) -> [0 .. 5]. From there, just add 1.
(int)(Math.random() * 6) + 1
Will return a random int in the range [1 .. 6], which is precisely your goal. So all together:
for(int i = 0; i < diceAmount; i++){
int dieRoll = (int)(Math.random() * 6) + 1;
System.out.println(dieRoll);
}
Use Math.random() to randomise your number of dice. There are lots of overloaded version of random() method. Read about Java.Math in Oracle documentation.

How to get the random value that can be positive or negative? [duplicate]

This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 8 years ago.
I have an array that can hold 5 integers.In the for loop I use Math.random() to fill the array with random integer values between 0 and 10 that can be positive or negative. How can I receive the negative value? Someone recommended me to multiply by -1 the formula to fill out array with positive and negative values but when I do this all my values in the array are negative. I think the problem is in this line
int r = 0 + (int) (Math.random() * 10 *(-1));
This is the code:
public class Random
{
public static void main(String [] args)
{
int [] arr= new int[5];
for(int k=0; k<arr.length; k++)
{
int r = 0 + (int) (Math.random() * 10 *(-1));
arr[k] = r;
}
int j = 0;
while(i<arr.length) {
System.out.print(arr[i] + " ");
j++;
}
}
}
Now my output is -7 -3 -3 -5 -6
I want my output to be 7 -3 3 -5 6
If you want numbers between -10 and 10 :
int r = (int) (Math.random() * 21) - 10;
Since Math.random() never returns 1.0, (int) (Math.random() * 21) would return integers between 0 and 20, and after substracting 10, you'll get what you want.
An alternative is to use java.util.Random :
Random rand = new Random();
int r = rand.nextInt(21) - 10;
You could produce a random number between 0 and 20 and subtract 10 from it. Doing this you wiil get a random number between -10 and +10:
int r = (int) ( Math.random() * 20 ) - 10;
You just need to modify your code as below
public class Random
{
public static void main(String[] args) {
int[] arr = new int[5];
for(int k=0; k<arr.length; k++)
{
int r = 0 +(int)(Math.random()*10*(k % 2 == 0? 1:-1));
arr[k] = r;
}
int j = 0;
while(j<arr.length)
{
System.out.print(arr[j]+ " ");
j++;
}
System.out.println();
}
}

How to create random number between 2 numbers but it just change in a range?

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;

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 How do I create a random number mod 5?

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

Categories