Generating a three-digit number Java [duplicate] - java

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 using this code to generate two digit but I want to change it and make it generate three digit.
int lottery = (int)(Math.random() * 100);
Thanks

You only have to change the multiplication from 100 to 1000 :)
int lottery = (int)(Math.random() * 1000);

Related

How do I make a randomly generated number using Math.Random() to be only two decimal places [duplicate]

This question already has answers here:
How can I truncate a double to only two decimal places in Java?
(18 answers)
How to round a number to n decimal places in Java
(39 answers)
Closed 11 months ago.
I'm trying to set random values to an array using Math.Random(), but it is printing values with 15 decimal places when I only want 2. How do I fix this?
This is what I have so far it works and gives me values like:
[9.410800457889598, 7.816378845915057, 7.4315520333161995, 7.4844070010512285, 7.6640924183174945].
public static void populateArray(double[] judge){
for(int n=0;n<=4;n++){
double score =(Math.random()*(3)+7);
judge[n] = score;
}
}

(Java) How do you generate a random integer between 1 to 4 using SecureRandom [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.
An assignment I'm working on right now needs to generate a random number from 1 to 4 using SecureRandom that will be used for a switch statement. Cases have to start from 1 so I can't just use
SecureRandom rand = new SecureRandom();
int a;
a = rand.nextInt(4);
right?
I have to use SecureRandom to generate the integer as well.
You're off by one (a very common source of errors). Of note is that rand.nextInt(n) will return a value from 0 to n - 1. You want
int a = 1 + rand.nextInt(4);
Also, make sure you reuse that SecureRandom instance (don't recreate it in a loop for example).

How to round to 1 decimal place using Java [duplicate]

This question already has answers here:
Generate a random double in a range
(7 answers)
Using Math.round to round to one decimal place?
(9 answers)
Closed 4 years ago.
I am trying to round to the nearest decimal value, however, this line of code keeps returning a number between 0 and 1, I also want the output to be between 1 and 10. Where am I going wrong?
power[i] = rng.nextDouble();
Math.round(ThreadLocalRandom.nextDouble(1,10)*10)/10.0
You can use JDK's Math.round.
A detailed example can be found here.

Java modulo doesn't work with large numbers [duplicate]

This question already has answers here:
Modulus with doubles in Java
(4 answers)
Taking Modulo of Double NUmber
(3 answers)
Closed 5 years ago.
I am creating a program to calculate Mersenne primes in Java. While doing so, I noticed that the modulo % operator doesn't function properly when dealing with large numbers.
Here is my code:
double a = 2305843009213693951d; //2^61 -1
double b = 2;
double m = a % b; //this should be 1.0
System.out.println(m); //prints 0.0
While one would expect the result to be 1, since the original number isn't even, it actually returns 0.0.
Why is this, and how can I fix it?

get random char in a specific range in java [duplicate]

This question already has answers here:
Is there functionality to generate a random character in Java?
(20 answers)
Closed 6 years ago.
Am designing wumpus hunting program using java. Where i can include three gold, 3 pits, one wumpus, one player. and the position of the game item should be random generator.How to generate random characters in 2d array .
How to specify the range for the characters in java.
Thanks.
You can use the method random.nextInt() to generate a random number for the position.
If you want to generate a random character you should use :
(char)(random.nextInt(max-min)+min). Where the max and min value are the ascii code of the max and the min char.
If you only want to generate a char in some char you should create an array with these character and use yourArray[random.nextInt(yourArray.length-1)+1];
Edit : You have to create the object random at the beginning of your
code. Random random = new Random();

Categories

Resources