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);
}
}
Related
This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Guess a number program with Java
(6 answers)
Closed 2 years ago.
I'm new to programming and while working on the Head First Java book I wanted to make a simple program, but there are some points I can't do. What I want the program to do is keep an integer number between 0 and 100 randomly and the user tries to find it. If the user finds the number, congratulations will appear on the screen. If not, it will ask for a new prediction. The code I wrote is below:
package Intro;
import java.util.Scanner;
public class GuessingGame {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a number: ");
int prediction = input.nextInt();
int number = (int) (Math.random());
if (number==prediction) {
System.out.println("Congratulations!");
}
else {
System.out.println("Wrong answer! Try a new number: ");
}
}
}
The point I stuck is that I can't make the randomly stored number integer and a number between 0 and 100. Another point is that if the answer is wrong, the program does not want a new prediction.
Very simple read this documentation on Random:
https://www.geeksforgeeks.org/java-math-random-method-examples/
// define the range
int max = 100;
int min = 1;
int range = max - min + 1;
// generate random numbers within 1 to 10
for (int i = 0; i < 10; i++) {
int rand = (int)(Math.random() * range) + min;
// Output is different everytime this code is executed
System.out.println(rand);
}
Output:
6
8
10
10
5
3
6
10
4
2
This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 4 years ago.
I just want to generate 6 digit random number, and the range should be start from 000000 to 999999.
new Random().nextInt(999999) is returning me number but it is not in 6 digit.
Its as simple as that, you can use your code and just do one thing extra here
String.format("%06d", number);
this will return your number in string format, so the "0" will be "000000".
Here is the code.
public static String getRandomNumberString() {
// It will generate 6 digit random Number.
// from 0 to 999999
Random rnd = new Random();
int number = rnd.nextInt(999999);
// this will convert any number sequence into 6 character.
return String.format("%06d", number);
}
If you need a six digit number it has to start at 100000
int i = new Random().nextInt(900000) + 100000;
Leading zeros do not have effect, 000000 is the same as 0. You can further simplify it with ThreadLocalRandom if you are on Java 7+:
int i = ThreadLocalRandom.current().nextInt(100000, 1000000)
1 + nextInt(2) shall always give 1 or 2. You then multiply it by 10000 to satisfy your requirement and then add a number between [0..9999].
already solved here
public int gen()
{
Random r = new Random( System.currentTimeMillis() );
return ((1 + r.nextInt(2)) * 10000 + r.nextInt(10000));
}
i know it’s very difficult but you can do something like this:
create a class for BinaryNumber;
create a constructor that generate a char[] of 6 character where every single one is generated with a randomiser from 0 to 1
override the toStrig() method so that it will return the digits char[] as a string if you want to display it. then crate a method toInt() that esaminate the string char by char with a for and turn it in a decimal base number by multiplying current digit to 10 to the pow of i:
char[] digits = {‘1’ , ‘0’ , ‘1’ , ‘1’ , ‘0’ , ‘1’};
//random
int result = 0;
for( int i = 0; i < digits.length; i++) {
result += Integer.parseInt(digits[i]) * Math.pow(10, i);
}
return result;
This is the code in java which generate a 6 digit random code.
import java.util.*;
public class HelloWorld{
public static void main(String []args)
{
Random r=new Random();
HashSet<Integer> set= new HashSet<Integer>();
while(set.size()<1)
{
int ran=r.nextInt(99)+100000;
set.add(ran);
}
int len = 6;
String random=String.valueOf(len);
for(int random1:set)
{
System.out.println(random1);
random=Integer.toString(random1);
}
}
}
This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 4 years ago.
I want to generate 500 random numbers and display them next to their indexes. I want the numbers to be 1 though 10. But when I run my code it only generates 13 random numbers and the numbers are greater than 10. Please help.
public class RandomStats {
public static void main(String[] args) {
int [] stats = new int [501];
int numRolls;
int outcome;
for (int i = 0; i <=500; i++ ){
outcome = (int) ( 10 * Math.random() + 1)
+ (int) ( 10 * Math.random () + 1);
stats[outcome] += 1;
}
for ( int i = 2; i <=500; i++) {
System.out.println(i + ": " + stats[i]);
}
}
}
While saving in the array, you are doing stats[outcome] += 1;, where your array is updated with same index again and again and thats the reason you only see ~13 values and rest as 0. I believe it should be stats[i] =outcome+ 1;
This question already has answers here:
How to sum digits of an integer in java?
(22 answers)
Closed 5 years ago.
I'm trying to write as loop in java that takes a 3 digit number and adds the numbers together. For example 123 would equal 6. I know that n % 10 will get me the first digit 3 and then n/10 will get me 23 which can than then can be % 10 again to get me the second number. That doesn't work for the last number however. I can't figure out how to write the loop. Any help would be greatly appreciated.
public static void main(String[] args) {
int num = 321;
int sum = 0;
while (num > 0) {
sum = sum + num % 10;
num = num / 10;
}
System.out.println(sum);
}
Duplicate: How to sum digits of an integer in 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;