This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 5 years ago.
How do I get random number in the range st 1 to 45 only (not included zero). Here is my code so far:
int number;
Random randomNum = new Random();
number = randomNum.nextInt(45)+1;
for (int y = 0; y < 10; y++) {
System.out.println("");
for (int i = 1; i <=6; i++) {
number= randomNum.nextInt(45);
if (i==6) {
System.out.printf("%d",number);
}
else {
System.out.printf("%d-",number);
}
}
}
randomNum.nextInt(45) + 1 will generate a number from 0 to 44 and add 1, thus generating numbers from 1 to 45.
Related
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);
}
}
This question already has answers here:
Often big numbers become negative
(4 answers)
Closed 2 years ago.
I have this code for fibonacci sequence :
public class Fibonacci {
private List<Integer> fibonacci;
public void fillFibonacci(){
fibonacci = new ArrayList<>();
int n1 = 1 , n2 = 1 , n3;
fibonacci.add(n1);
fibonacci.add(n2);
for(int i = 2 ; i < 4000 ; i ++){
n3 = n1 + n2;
fibonacci.add(n3);
n1=n2;
n2=n3;
}
}
public void printFibonacci(){
for(int i = 0 ; i < fibonacci.size() ; i ++){
System.out.print(fibonacci.get(i) + " ");
System.out.println(i);
}
}
}
this code show negative number what's wrong with it?
You cannot go up to index= 4000 in this loop, you have integer overflowed. Maximum you can go to is 49. Change the loop as such.
for(int i = 2 ; i < 49 ; i ++){
n3 = n1 + n2;
fibonacci.add(n3);
n1=n2;
n2=n3;
}
The 4000-th Fibonacci number has 836 digits. The java int type has 32-bits and can fit numbers up to 10 digits. After that it overflows and may go negative. If you want big numbers, change your code to use BigInteger.
This question already has answers here:
How to get a random between 1 - 100 from randDouble in Java?
(6 answers)
Closed 2 years ago.
So I am trying to make 100 random integers from 1-100 print out. I can make doubles of this but not integers. Please show me how to make this into integers, here is the code.
for(i = 1; i <= 100; i++) {
double r = Math.random();
double in = r * 100;
System.out.print(in + ", ");
}
Please show me what I messed up or if I need to use an alternate technique for this
You can use Random object instead. from java.util package
Random random = new Random();
int value = random.nextInt(100);
Use ThreadLocalRandom:
for (int i = 1; i <= 100; i++) {
System.out.print(ThreadLocalRandom.current().nextInt(100) + ", ");
}
Import the class java.util.Random
Make the instance of the class Random, i.e., Random rand = new Random()
Invoke one of the following methods of rand object:
nextInt(upperbound) generates random numbers in the range 0 to upperbound-1.
nextFloat() generates a float between 0.0 and 1.0.
nextDouble() generates a double between 0.0 and 1.0
For example if you want to generate a random number between 1 to 100 the the code will be.
import java.util.Random;
Random r = new Random();
Int number = r.nextInt(100);
So variable number will be a random number between 1 to 100
Whats wrong is the statement double in = r * 100;
It must be
int b = (int) (Math.random()*100)%100; //range [0-99]
Just add 1 to b if you strictly want b in the range [1-100]
I am trying to make a program where I can input the size of a 2D array, the highest number in a 2D array, and the most amount of a certain number in the 2D array, and then fill it with random numbers in between 1 and the highest number. In my code, I specify that the max amount of times a number should repeat is 4, yet my output doesn't match that. Any suggestions?
This is my code:
class Main {
public static void main(String[] args) {
System.out.println(fill(6, 9, 4));
}
public static String fill(int size, int max, int most) {
int[][] list = new int[size][size];
int count = 0;
for (int i = 0; i < list.length; i++) {
for (int j = 0; j < list[i].length; j++) {
int x = (int)((Math.random()* max) + 1);
int y = 0;
count = 0;
for (int k = 0; k < list.length; k++) {
for (int l = 0; l < list[k].length; l++) {
if(list[k][l] == x) count++;
}
}
if(count < most) {
list[i][j] = x;
} else {
while(true) {
y = (int)((Math.random()* max) + 1);
if(y != x) break;
}
list[i][j] = y;
}
System.out.print(list[i][j] + " ");
}
System.out.println();
}
return "";
}
}
And this is my output:
9 4 6 1 9 1
7 1 4 4 3 2
6 1 4 2 7 9
5 9 4 7 2 5
3 5 3 5 7 4
3 8 8 6 2 6
Problem: There are 6 "4"s and 2 "8"s
You generate a random number.
You then check if this random number is 'invalid', in the sense that it's been used too many times.
Then, you generate a new random number, check that this isn't the same as your previous number, and then just roll with that. You are failing to check if this number, too, is 'overloaded'. So, what could have happened here is that your algorithm picked '9', counts 9s, finds 4 of them, rolls up a new random number, 9 again, so it rolls yet another number, 4, and just puts 4 in, without checking again.
Rejigger your while loops.
Or, better yet, make a utility class to offload the job of generating a random number, but not a number that's already been returned N times, to a separate class, so that you can untangle this messy code.
Your method
while(true) {
y = (int)((Math.random()* max) + 1);
if(y != x) break;
}
does not check that count of y did not already reached most
Your Issue is here:
while(true) {
y = (int)((Math.random()* max) + 1);
if(y != x) break;
}
list[i][j] = y;
This basically just rules out that x will be repeated more than most, but not y.
On a side note, I recommend using hash maps to keep track of the occurrences instead of iterating over the whole array over and over.
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;