How can I get a random number between two ranges? [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I need to show the numbers 1-10 in a random order. An example outpue while executing first time would be: 5,4,8,7,9,1,2,3. An example while executing second time would be: 7,6,5,1,2,3,4,9,8
Will the following code print all date between ranges in random?
Random r = new Random();
int i1 = r.nextInt(80 - 65) + 65;

You can use the Collections.shuffle() method. (more info on this SO question)
In your example:
List<int> numbers = new ArrayList<int>();
int min = 65;
int max = 85;
for (int i = min; i <= max; i++) {
numbers.add(i);
}
Collections.shuffle(numbers);

Using shuffle works too. This is how you would manually do it.
Random r = new Random();
List<Integer> list = new ArrayList<Integer>();
for (int i=min ; i<= max ; i++){
list.add(i); // adding your data
}
List<Integer> list2 = new ArrayList<Integer>().addAll(list);
//you don't need to use
//list2 if you are ok with losing list.
//As here list2 is being emptied ..
while(list2.size() > 0){
int randomIndex = r.nextInt(0, list2.size);
System.out.println(list.get(randomIndex));
list2.remove(randomIndex);
}

Related

Finding greatest difference between elements of two lists [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
My code is:
int diff = 0;
for (int i = 0; i<listOne.size(); i++)
{
for (int j = 0; j<listTwo.size(); j++)
{
if (listOne.get(i)-listTwo.get(j)>diff)
diff = listOne.get(i)-listTwo.get(j);
if (listTwo.get(j)-listOne.get(i)>diff)
diff = listTwo.get(j)-listOne.get(i);
}
}
return diff;
The task is to find the greatest difference between any two numbers in two inputted lists (the difference must be between a number from list one and a number from list two).
I cannot tell what is wrong with my code.
You may be missing Math.abs executed on the diff. Difference is an absolute value, so difference between 5 to 7 and 7 to 5 is same - 2.
int diff = 0;
for (int i = 0; i<listOne.size(); i++) {
for (int j = 0; j<listTwo.size(); j++) {
int elementDiff = Math.abs(listOne.get(i)-listTwo.get(j));
if (elementDiff>diff) {
diff = elementDiff;
}
}
}
return diff;
So both below lines will produce same results:
int elementDiff = Math.abs(listOne.get(i)-listTwo.get(j));
int elementDiff = Math.abs(listTwo.get(i)-listOne.get(j));

How to transform a number into array. Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to transform a number into array.
For example...
num = 7
to
list = [1,2,3,4,5,6,7]
How do I do that?
Try this :
int []list = new int[num];
for (int i = 0; i < list.length; i++) {
list[i] = i + 1;
}
Try this:
Integer[] ints = new Integer[x];
for (int i = 0; i < ints.length; i++) {
ints[i] = i + 1;
}
I'm assuming you're want to create an array from 1 to the number.
Code:
for(int i = 0; i < num; i++) {
list[i] = i+1;
}
In java you have to declare the variables first.
This means that variables are strongly typed.
You cannot convert a variable into an array.
However you can create a new variable that is an array.
int num = 7;
int[] arr = {num}; // arr is an array containing num

List the factors of an integer [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
public int[] factors(int n) {
int count[] = new int[n];
for (int i = 1; i <= count.length; i++) {
count[i] = (count.length % i);
}
return count;
}
}
Anyone have an idea how to list all the factors?
set int i = 0; as array starts from 0
Factor can only be considered if value % i==0 so add if condition
You need one more variable to increment array Index as you can't use i for that.So add one more variable to increment array index to add factor.
Think more on your code, debug especially if possible,check out the values ,add print statements if needed to see what's going on and that's it you will definitely win the task.
Other than that if you can use SOF you can use Google as well :)
You should use a List instead of array. Since you can't initialize the array without knowing the size.
Then you can try something like following
public List<Integer> factors(int n) {
List<Integer> list = new ArrayList<>(); // initialize the list
for (int i = 1; i <= n; i++) {
if (n % i == 0) { // decide the factor.
list.add(i); // now i is a factor, needs to add to list
}
}
return list; // return list contains factors.
}

How to find size/length of an array without using library functions/variables [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
int arr[] = new int[10];
int size=0;
while(???)
{
i++;
}
System.out.println(size); // Should print 10
How can I loop over the array without using arr.length or other library functions?
int arr[] = new int[100];
int sum = 0;
int i = 0;
while (true) {
try {
sum += arr[i];
} catch (ArrayIndexOutOfBoundsException e) {
break;
}
i++;
}
System.out.println("Array is of size " + i);
I'm assuming array is of ints, but the idea is the same.
Under the hood you'll get a .length (unless you use a strange compiler), but since the question's kinda weird...
int[] array = new int[100];
int size = 0;
for(int i : array){
++size;
}
System.out.println("Size: " + size);
I still don't understand the point

Populate int array with for loop in Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an array named numbers that I want to populate with a for loop:
int[] numbers;
for ( int i = 0; i <=10; i++)
{
// want to populate the array with a sequence of 0-10
}
How can I populate the 11 values generated from the above for loop into my array?
If you are using Java 7 or lower, do this:
int[] numbers = new int[11];
for ( int i = 0; i <=10; i++)
{
numbers[i] = i;
}
For Java 8, there is a more concise way to do this:
int[] numbers = IntStream.rangeClosed(0, 10).toArray()
First you need to define what numbers is, you have only declared it.
int[] numbers = new int[11];
Then insert the values you want.
for ( int i = 0; i <=10; i++)
{
numbers[i] = i;
}

Categories